Redis list pop without deleting

I am using RedisTemplate (from Spring) in my Java application. I need to pop from a list of elements matching the values, but without deleting it. Any suggestions?

+8
source share
4 answers

You can easily peek into an element rather than click it using the range command.

With Spring, from an instance of RedisTemplate, you can get an instance of ListOperations using the opsForList () method, and then:

  • listOp.range (key, 0, 0) will return the first (left) element without clicking it

  • listOp.range (key, -1, -1) will return the last (right) element without clicking it

See documentation at:

http://static.springsource.org/spring-data/data-keyvalue/docs/1.0.x/api/org/springframework/data/keyvalue/redis/core/RedisTemplate.html

http://static.springsource.org/spring-data/data-keyvalue/docs/1.0.x/api/org/springframework/data/keyvalue/redis/core/ListOperations.html

+12
source

You do not know how to do this using RedisTemplate, but to get a value from a list, you can use the redis command:

LRANGE <LIST> 0 0

to get the first value, where <LIST> is the name of your list.

Is there something similar in RedisTemplate?

0
source

Is there any method in Redis to pull an item without deleting it, but keep it in sleep mode for the duration? After the expiration date (and it is not deleted), this element wakes up and may appear again.

http://redis.io/commands/rpoplpush

Pattern: A robust Redis queue is often used as a messaging server to implement processing of background jobs or other messaging tasks. The simple form of the queue is often obtained by pushing the values ​​to the list on the producer side and waiting for these values ​​on the consumer side using RPOP (using polling) or BRPOP if the client is better served by the blocking operation. However, in this context, the received queue is not reliable, because messages can be lost, for example, in the event of a network problem or if the consumer falls after the message, but it should still be processed. RPOPLPUSH (or BRPOPLPUSH for the blocking option) offers a way to avoid this problem: the consumer retrieves the message and at the same time pushes it into the processing list. He will use the LREM command to remove the message from the processing list after processing the message. An additional client can keep track of the processing list for items that remain there for too long, and if necessary, pull these timeouts back into the queue.

0
source

I recommend rpoplpush to push an item from the list and lpush it to another (can also be the same) key, very useful. It can be used as a reliable queue or as a round list. Here is the official doc: https://redis.io/commands/rpoplpush

0
source

Source: https://habr.com/ru/post/917372/


All Articles