检查列表中是否存在一个值已经 Redis [英] Checking if a value exists in a list already Redis

查看:42
本文介绍了检查列表中是否存在一个值已经 Redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法检查 redis 列表中是否已存在某个键?

I'm wondering if there's a way to check if a key already exists in a redis list?

我不能使用集合,因为我不想强制执行唯一性,但我确实希望能够检查字符串是否确实存在.

I can't use a set because I don't want to enforce uniqueness, but I do want to be able to check if the string is actually there.

推荐答案

您的选项如下:

  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.

这篇关于检查列表中是否存在一个值已经 Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆