Lists
List commands for storing ordered sequences of elements.
Commands
Section titled “Commands”| Command | Syntax | Description |
|---|---|---|
| LPUSH | LPUSH key element [element ...] | Prepend elements |
| RPUSH | RPUSH key element [element ...] | Append elements |
| LPOP | LPOP key [count] | Pop from front |
| RPOP | RPOP key [count] | Pop from back |
| LLEN | LLEN key | Get list length |
| LRANGE | LRANGE key start stop | Get range of elements |
| LINDEX | LINDEX key index | Get element by index |
| LSET | LSET key index element | Set element at index |
| LTRIM | LTRIM key start stop | Trim list to range |
| LREM | LREM key count element | Remove elements by value |
| LINSERT | LINSERT key BEFORE|AFTER pivot element | Insert before/after element |
Blocking Commands (Server Mode Only)
Section titled “Blocking Commands (Server Mode Only)”| Command | Syntax | Description |
|---|---|---|
| BLPOP | BLPOP key [key ...] timeout | Blocking pop from front |
| BRPOP | BRPOP key [key ...] timeout | Blocking pop from back |
Examples
Section titled “Examples”Basic Operations
Section titled “Basic Operations”# Push elements127.0.0.1:6379> RPUSH mylist "one" "two" "three"(integer) 3127.0.0.1:6379> LPUSH mylist "zero"(integer) 4
# Get all elements127.0.0.1:6379> LRANGE mylist 0 -11) "zero"2) "one"3) "two"4) "three"
# Pop elements127.0.0.1:6379> LPOP mylist"zero"127.0.0.1:6379> RPOP mylist"three"Index Operations
Section titled “Index Operations”# 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 index127.0.0.1:6379> LSET mylist 0 "first"OKRange Operations
Section titled “Range Operations”# Get subset127.0.0.1:6379> LRANGE mylist 0 11) "first"2) "two"
# Trim list (keep only elements 0-2)127.0.0.1:6379> LTRIM mylist 0 2OKRemove Elements
Section titled “Remove Elements”127.0.0.1:6379> RPUSH mylist "a" "b" "a" "c" "a"(integer) 5
# Remove 2 occurrences of "a" from head127.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) 1Insert Elements
Section titled “Insert Elements”127.0.0.1:6379> RPUSH mylist "Hello" "World"(integer) 2127.0.0.1:6379> LINSERT mylist BEFORE "World" "Beautiful"(integer) 3127.0.0.1:6379> LRANGE mylist 0 -11) "Hello"2) "Beautiful"3) "World"Blocking Operations (Server Mode)
Section titled “Blocking Operations (Server Mode)”# Wait up to 5 seconds for an element127.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"Library Mode (Rust)
Section titled “Library Mode (Rust)”use redlite::Db;
let db = Db::open("mydata.db")?;
// Push elementsdb.rpush("mylist", &[b"one", b"two", b"three"])?;db.lpush("mylist", &[b"zero"])?;
// Get rangelet elements = db.lrange("mylist", 0, -1)?; // Vec<Vec<u8>>
// Pop elementslet first = db.lpop("mylist", 1)?; // Vec<Vec<u8>>let last = db.rpop("mylist", 1)?;
// Get by indexlet elem = db.lindex("mylist", 0)?; // Option<Vec<u8>>
// List lengthlet len = db.llen("mylist")?;
// Remove elementsdb.lrem("mylist", 2, b"a")?;
// Insertdb.linsert("mylist", "BEFORE", b"World", b"Beautiful")?;Use Cases
Section titled “Use Cases”Message Queues
Section titled “Message Queues”# ProducerRPUSH tasks '{"task": "process_image", "id": 123}'
# Consumer (blocking)BLPOP tasks 0 # Wait indefinitelyRecent Items
Section titled “Recent Items”# Add to front, keep last 100LPUSH recent:user:1 "viewed:product:456"LTRIM recent:user:1 0 99RPUSH 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