Redis fetch all value of list without iteration and without popping

Redis

Redis Problem Overview


I have simple redis list key => "supplier_id"

Now all I want it retrieve all value of list without actually iterating over or popping the value from list

Example to retrieve all the value from a list Now I have iterate over redis length

element = []
0.upto(redis.llen("supplier_id")-1) do |index| 
  element << redis.lindex("supplier_id",index)
 end

can this be done without the iteration perhap with better redis modelling . can anyone suggest

Redis Solutions


Solution 1 - Redis

To retrieve all the items of a list with Redis, you do not need to iterate and fetch each individual items. It would be really inefficient.

You just have to use the LRANGE command to retrieve all the items in one shot.

elements = redis.lrange( "supplier_id", 0, -1 )

will return all the items of the list without altering the list itself.

Solution 2 - Redis

I'm a bit unclear on your question but if the supplier_id is numeric, why not use a ZSET?

Add your values like so:

ZADD suppliers 1 "data for supplier 1"  
ZADD suppliers 2 "data for supplier 2"  
ZADD suppliers 3 "data for supplier 3"  

You could then remove everything up to (but not including supplier three) like so:

ZREMRANGEBYSCORE suppliers -inf 2

or

ZREMRANGEBYSCORE suppliers -inf (3

That also gives you very fast access (by supplier id) if you just want to read from it.

Hope that helps!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVirenView Question on Stackoverflow
Solution 1 - RedisDidier SpeziaView Answer on Stackoverflow
Solution 2 - RedismkgrunderView Answer on Stackoverflow