Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis fetch all value of list without iteration and without popping

Tags:

redis

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

like image 985
Viren Avatar asked May 22 '12 13:05

Viren


1 Answers

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.

like image 193
Didier Spezia Avatar answered Sep 20 '22 23:09

Didier Spezia