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.
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.
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.
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.
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.
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 betweenstart
andend
.start
andend
can be negative numbers just like Python slicing notation
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With