Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a value exists in a list already Redis

Tags:

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.

like image 650
Reu Avatar asked Feb 16 '12 14:02

Reu


1 Answers

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.

like image 75
Fritzy Avatar answered Oct 28 '22 15:10

Fritzy