skip to content
Alvin Lucillo

First and last items of a list

/ 1 min read

You can query the first and last value of a list with LINDEX, providing 0 for the first value and -1 for the last.

  ~ docker exec -it redis redis-cli
127.0.0.1:6379> LPUSH msg_queue msg1
(integer) 1
127.0.0.1:6379> RPUSH msg_queue msg2
(integer) 2
127.0.0.1:6379> RPUSH msg_queue msg3
(integer) 3
127.0.0.1:6379> LRANGE msg_queue 0 2
1) "msg1"
2) "msg2"
3) "msg3"
127.0.0.1:6379> LINDEX msg_queue 0
"msg1"
127.0.0.1:6379> LINDEX msg_queue -1
"msg3"
127.0.0.1:6379>