Redis List, pop without removing

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


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.


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?

Tags:

Java

Spring

Redis