因为是 List
,所以所有相关的命令都以 L
开头。
LPUSH / RPUSH
LRANGE list 0 -1
-1
表示末尾
127.0.0.1:6379> LPUSH list a
1
127.0.0.1:6379> LRANGE list 0 -1
a
127.0.0.1:6379> LPUSH list b
2
127.0.0.1:6379> LRANGE list 0 -1
b
a
127.0.0.1:6379> LPUSH list c d e
5
127.0.0.1:6379> LRANGE list 0 -1
e
d
c
b
a
127.0.0.1:6379> RPUSH list f
6
127.0.0.1:6379> LRANGE list 0 -1
e
d
c
b
a
f
LPOP / RPOP
127.0.0.1:6379> RPOP list
f
127.0.0.1:6379> LRANGE list 0 -1
e
d
c
b
a
127.0.0.1:6379> LPOP list 2
ERR wrong number of arguments for 'lpop' command
LLEN
127.0.0.1:6379> LLEN list
5
127.0.0.1:6379> LRANGE list 0 -1
e
d
c
b
a
LTRIM key start end
只保留 start, end 以内的元素(删除 start, end 之外的元素)
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> lpush list a b c d e
5
127.0.0.1:6379> lrange list 0 -1
e
d
c
b
a
127.0.0.1:6379> LTRIM list 1 3
OK
127.0.0.1:6379> lrange list 0 -1
d
c
b