Redis List, pop without removing
Asked Answered
D

4

20

I'm using RedisTemplate(from Spring) in my Java app. I need to do pop from a list of elements correspondenting for values, but without removing it. Any suggestions?

Duplicate answered 4/6, 2012 at 14:7 Comment(1)
The term pop is always used in the context of "remove and return"... (at least it should)Ectoparasite
T
25

You can easily peek at an item rather than popping it by using the range command.

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

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

  • listOp.range(key, -1, -1) will return the last (right) item without popping 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

Touched answered 4/6, 2012 at 14:57 Comment(0)
M
3

Is there any method in Redis to pop an item without removing it but keep it hibernate in an expire period? After the expire period (and it not deleted), this item wake up and can pop again.

http://redis.io/commands/rpoplpush

Pattern: Reliable queue Redis is often used as a messaging server to implement processing of background jobs or other kinds of messaging tasks. A simple form of queue is often obtained pushing values into a list in the producer side, and waiting for this values in the consumer side using RPOP (using polling), or BRPOP if the client is better served by a blocking operation. However in this context the obtained queue is not reliable as messages can be lost, for example in the case there is a network problem or if the consumer crashes just after the message is received but it is still to process. RPOPLPUSH (or BRPOPLPUSH for the blocking variant) offers a way to avoid this problem: the consumer fetches the message and at the same time pushes it into a processing list. It will use the LREM command in order to remove the message from the processing list once the message has been processed. An additional client may monitor the processing list for items that remain there for too much time, and will push those timed out items into the queue again if needed.

Monmouth answered 8/3, 2013 at 6:30 Comment(0)
I
2

Not sure 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 to this in RedisTemplate?

Indianapolis answered 4/6, 2012 at 14:32 Comment(2)
This is great, but returns the element at the HEAD of the queue, not the TAIL. When doing RPOP you pop the element at the tail, so a solution is needed to pop that element without removing it from the queue.Formal
You have to combine LRANGE with LLEN. Get the n number of elements of the queue with LLEN and use that number in LRANGE n nFormal
B
1

You can get a list of values from Redis using RedisTemplate as below.

Long size = redisTemplate.opsForList().size(key); Object object = hashOperationsList.range(key, 0, size );

This way values won't be removed from the cache.

Bluing answered 17/7, 2022 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.