bravoboy
bravoboy

Reputation: 11

how to traversal redis list

I have a list in redis, I can ensure the element in the list is ordered.Now I has a new element,I want to insert in the list and the list is also ordered. My way is traverse the list and compare the element.So how can I traverse the list? I know the list has LPOP way,but I don't want to remove element.

Upvotes: 1

Views: 403

Answers (2)

Aman Garg
Aman Garg

Reputation: 3290

If you want to fetch all the data of a list from Redis, you do not need to iterate and fetch individual items. this will be inefficient and not a good practice to do so.

You can use the LRANGE command to fetch all the items in one command only. Command is as below. Please have a look.

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

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

Upvotes: 0

yojimbo87
yojimbo87

Reputation: 68423

You can either use LRANGE command which will return all elements of the list in one operation (e.g. LRANGE mylist 0 -1) or use combination of LLEN to get the length of the list and LINDEX to navigate through each element based on the number returned by LLEN command.

Upvotes: 1

Related Questions