Skip to content

Lists

List commands for storing ordered sequences of elements.

CommandSyntaxDescription
LPUSHLPUSH key element [element ...]Prepend elements
RPUSHRPUSH key element [element ...]Append elements
LPOPLPOP key [count]Pop from front
RPOPRPOP key [count]Pop from back
LLENLLEN keyGet list length
LRANGELRANGE key start stopGet range of elements
LINDEXLINDEX key indexGet element by index
LSETLSET key index elementSet element at index
LTRIMLTRIM key start stopTrim list to range
LREMLREM key count elementRemove elements by value
LINSERTLINSERT key BEFORE|AFTER pivot elementInsert before/after element
CommandSyntaxDescription
BLPOPBLPOP key [key ...] timeoutBlocking pop from front
BRPOPBRPOP key [key ...] timeoutBlocking pop from back
Terminal window
# Push elements
127.0.0.1:6379> RPUSH mylist "one" "two" "three"
(integer) 3
127.0.0.1:6379> LPUSH mylist "zero"
(integer) 4
# Get all elements
127.0.0.1:6379> LRANGE mylist 0 -1
1) "zero"
2) "one"
3) "two"
4) "three"
# Pop elements
127.0.0.1:6379> LPOP mylist
"zero"
127.0.0.1:6379> RPOP mylist
"three"
Terminal window
# Get by index (0-based)
127.0.0.1:6379> LINDEX mylist 0
"one"
127.0.0.1:6379> LINDEX mylist -1 # Last element
"two"
# Set by index
127.0.0.1:6379> LSET mylist 0 "first"
OK
Terminal window
# Get subset
127.0.0.1:6379> LRANGE mylist 0 1
1) "first"
2) "two"
# Trim list (keep only elements 0-2)
127.0.0.1:6379> LTRIM mylist 0 2
OK
Terminal window
127.0.0.1:6379> RPUSH mylist "a" "b" "a" "c" "a"
(integer) 5
# Remove 2 occurrences of "a" from head
127.0.0.1:6379> LREM mylist 2 "a"
(integer) 2
# Remove all occurrences (count = 0)
127.0.0.1:6379> LREM mylist 0 "a"
(integer) 1
Terminal window
127.0.0.1:6379> RPUSH mylist "Hello" "World"
(integer) 2
127.0.0.1:6379> LINSERT mylist BEFORE "World" "Beautiful"
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "Hello"
2) "Beautiful"
3) "World"
Terminal window
# Wait up to 5 seconds for an element
127.0.0.1:6379> BLPOP myqueue 5
# Returns nil after timeout, or element if pushed by another client
# In another terminal:
127.0.0.1:6379> RPUSH myqueue "message"
use redlite::Db;
let db = Db::open("mydata.db")?;
// Push elements
db.rpush("mylist", &[b"one", b"two", b"three"])?;
db.lpush("mylist", &[b"zero"])?;
// Get range
let elements = db.lrange("mylist", 0, -1)?; // Vec<Vec<u8>>
// Pop elements
let first = db.lpop("mylist", 1)?; // Vec<Vec<u8>>
let last = db.rpop("mylist", 1)?;
// Get by index
let elem = db.lindex("mylist", 0)?; // Option<Vec<u8>>
// List length
let len = db.llen("mylist")?;
// Remove elements
db.lrem("mylist", 2, b"a")?;
// Insert
db.linsert("mylist", "BEFORE", b"World", b"Beautiful")?;
Terminal window
# Producer
RPUSH tasks '{"task": "process_image", "id": 123}'
# Consumer (blocking)
BLPOP tasks 0 # Wait indefinitely
Terminal window
# Add to front, keep last 100
LPUSH recent:user:1 "viewed:product:456"
LTRIM recent:user:1 0 99
Terminal window
RPUSH log:app "2024-01-01 12:00:00 INFO: Started"
RPUSH log:app "2024-01-01 12:00:01 DEBUG: Processing"
LRANGE log:app -10 -1 # Last 10 entries