Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform lrange + (delete the result of lrange from list) on Redis list

Tags:

caching

redis

I am aware that rpop removes the last element of the Redis list.How to pop items of list between certain range (eg: 0 to 100)?

I hope this is the combination of lrange + rpop.

like image 946
Prem Avatar asked Sep 27 '16 05:09

Prem


People also ask

How do you delete specific data on Redis?

There are two major commands to delete the keys present in Redis: FLUSHDB and FLUSHALL. We can use the Redis CLI to execute these commands. The FLUSHDB command deletes the keys in a database. And the FLUSHALL command deletes all keys in all databases.

What is Lrange in Redis?

Redis LRANGE command is used to return the specified elements of the list stored at key. The offsets start and stop are zero-based indexes, with 0 being the first element of the list, 1 being the next element and so on.

What is Ltrim in Redis?

Redis LTRIM command is used to trim an existing list so that it will contain only the specified range of elements specified. Both start and stop are zero-based indexes, where 0 is the first element of the list (the head), 1 the next element and so on.

How do I check Redis list?

To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.


4 Answers

To select and remove the first 100 elements (from the left):

LRANGE list 0 99
LTRIM list 100 -1

To select and remove the last 100 elements (from the right):

LRANGE list -1 -100
LTRIM list 0 -99

From an older version of the redis documentation for LTRIM:

Trim the list name, removing all values not within the slice between start and end. start and end can be negative numbers just like Python slicing notation

like image 58
LuciferJack Avatar answered Oct 22 '22 12:10

LuciferJack


You need to combine lrange and ltrim to achieve this.

http://redis.io/commands/ltrim

Edit : As suggested in the comments the right answer is

lrange list 0 99
ltrim list 100 -1
like image 41
Karthikeyan Gopall Avatar answered Oct 22 '22 11:10

Karthikeyan Gopall


remove the first top 100 element,should be:

lrange list 0 99 ltrim list 100 -1

remove the tail top 100 element,should be:

lrange list -100 -1 ltrim list 0 -101

like image 42
David Avatar answered Oct 22 '22 11:10

David


As far as I know, there is no predefined command to pop a range of items from a list. You have to send a series of rpop commands to redis. It's usual with Redis to send many short commands, so it's not a problem to send 100 (or even 1000) rpop commands in a row. To improve performances you can send multiple command in one operation using "pipelining". The exact way of doing this depends on your driver.

Another way would be to write a Lua script and send it using eval

like image 40
Pascal Le Merrer Avatar answered Oct 22 '22 12:10

Pascal Le Merrer