Checking if a value exists in a list already Redis

后端 未结 5 1712
深忆病人
深忆病人 2021-02-01 13:14

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 ab

5条回答
  •  不思量自难忘°
    2021-02-01 13:35

    Your options are as follows:

    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 lists are implemented as a http://en.wikipedia.org/wiki/Linked_list, hence the limitations.

    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.

提交回复
热议问题