Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis List, pop without removing

Tags:

java

spring

redis

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?

like image 349
Zhivko Draganov Avatar asked Jun 04 '12 14:06

Zhivko Draganov


People also ask

How do you get all the values in from the list in Redis?

To retrieve all the items of a list with Redis, you do not need to iterate and fetch each individual items. It would be really inefficient. You just have to use the LRANGE command to retrieve all the items in one shot. will return all the items of the list without altering the list itself.

How do I read a list in Redis?

To get elements in a Redis, use the LRANGE command. This command takes the name of the list and the index range of the element you wish to access. The command should return the values of the elements in the specified range.

How do I clear Redis list?

Delete the key, and that will clear all items. Not having the list at all is similar to not having any items in it. Redis will not throw any exceptions when you try to access a non-existent key.

What is LPOP in Redis?

Redis and PHP Redis LPOP command removes and returns the first element of the list stored at the key.


1 Answers

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

like image 194
Didier Spezia Avatar answered Sep 18 '22 14:09

Didier Spezia