Redis的List数据类型及常用命令
List可以想成一个双向链表
所有的List命令大多都是以l开头的
创建数据 LPUSH
插入数据是倒序 ,相当于插入的值放在列表的头部
127.0.0.1:6379> LPUSH list one two three
(integer) 3
127.0.0.1:6379> LRANGE list 0 2
1) "three"
2) "two"
3) "one"
创建数据 RPUSH
插入数据是正序 ,相当于插入的值放在列表的尾部
127.0.0.1:6379> RPUSH list1 one two three
(integer) 3
127.0.0.1:6379> LRANGE list1 0 -1
1) "one"
2) "two"
3) "three"
**LRANGE获得数据 **
127.0.0.1:6379> LRANGE list 0 1
1) "three"
2) "two"
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
LPOP 移除数据 移除列表最左端的元素
127.0.0.1:6379> LRANGE list1 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> LPOP list1
"one"
127.0.0.1:6379> LRANGE list1 0 -1
1) "two"
2) "three"
RPOP 移除数据 移除列表最右端的元素
127.0.0.1:6379> LRANGE list1 0 -1
1) "two"
2) "three"
127.0.0.1:6379> RPOP list1
"three"
127.0.0.1:6379> LRANGE list1 0 -1
1) "two"
LINDEX获取指定下标的值
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> LINDEX list 1
"two"
LREM移除指定个数的值
127.0.0.1:6379> LREM list 1 one
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "one"
3) "three"
4) "two"
5) "one"
6) "three"
7) "two"
127.0.0.1:6379> LREM list 2 one
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "three"
3) "two"
4) "three"
5) "two"
llen查看列表的长度
127.0.0.1:6379> llen list
(integer) 3
**LTRIM通过下标截取指定长度的列表 **
127.0.0.1:6379> LPUSH list one two three four five(integer) 5127.0.0.1:6379> LRANGE list 0 -11) "five"2) "four"3) "three"4) "two"5) "one"127.0.0.1:6379> LTRIM list 0 3OK127.0.0.1:6379> LRANGE list 0 -11) "five"2) "four"3) "three"4) "two"
RPOPLPUSH移除当前列表的最后一个元素pusl到另一个列表
127.0.0.1:6379> RPUSH list 0 1 2(integer) 3127.0.0.1:6379> RPOPLPUSH list mylist"2"127.0.0.1:6379> LRANGE list 0 -11) "0"2) "1"127.0.0.1:6379> LRANGE mylist 0 -11) "2"127.0.0.1:6379> RPOPLPUSH list mylist"1"127.0.0.1:6379> LRANGE list 0 -11) "0"127.0.0.1:6379> LRANGE mylist 0 -11) "1"2) "2"
LSET 更新指定下标的值,如果列表不存在或指定下标值不存在会保错
127.0.0.1:6379> EXISTS lsit(integer) 0127.0.0.1:6379> lset lsit 0 item(error) ERR no such key127.0.0.1:6379> LPUSH list 0(integer) 1127.0.0.1:6379> LRANGE list 0 -11) "0"127.0.0.1:6379> LSET list 0 itemOK127.0.0.1:6379> LRANGE list 0 -11) "item"127.0.0.1:6379> LSET list 1 item2(error) ERR index out of range
LINSERT可以在指定的元素前或元素后插入值,before元素 前,after元素 后
127.0.0.1:6379> LRANGE list 0 -11) "item"127.0.0.1:6379> LINSERT list before item first(integer) 2127.0.0.1:6379> LRANGE list 0 -11) "first"2) "item"127.0.0.1:6379> LINSERT list after first ok(integer) 3127.0.0.1:6379> LRANGE list 0 -11) "first"2) "ok"3) "item"
学习参考狂神说java