且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查列表中是否存在一个值已经 Redis

更新时间:2022-05-17 10:26:38

您的选项如下:

  1. 使用 LREM 并在找到后替换它.
  2. 维护一个单独的 SET 与您的 LIST
  3. 循环遍历 LIST,直到找到项目或到达结尾.
  1. Using LREM and replacing it if it was found.
  2. Maintaining a separate SET in conjunction with your LIST
  3. Looping through the LIST until you find the item or reach the end.

Redis 列表作为 http://en.wikipedia.org/wiki/Linked_list 实现,因此存在局限性.

Redis lists are implemented as a http://en.wikipedia.org/wiki/Linked_list, hence the limitations.

我认为您***的选择是维护一个重复的 SET.这是我倾向于做的.把它想象成一个额外的索引.无论如何,请确保您的操作是使用 MULTI-EXEC 或 Lua 脚本的原子操作.

I think your best option is maintaining a duplicate SET. This is what I tend to do. Just think of it as an extra index. Regardless, make sure your actions are atomic with MULTI-EXEC or Lua scripts.