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?
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:
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?
LRANGE
with LLEN
. Get the n
number of elements of the queue with LLEN
and use that number in LRANGE n n
–
Formal 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.
© 2022 - 2024 — McMap. All rights reserved.
pop
is always used in the context of "remove and return"... (at least it should) – Ectoparasite