redis入门到精通 - 04 link链表

lpush/rpush

语法:
lpush key value [value ...]  # 把值插入到链表头部
rpush key value [value ...]  # 把值插入到链表尾部

示例:
127.0.0.1:6379> lpush charactor a
(integer) 1
127.0.0.1:6379> rpush charactor b
(integer) 2
127.0.0.1:6379> rpush charactor c d e f g
(integer) 7
127.0.0.1:6379> lpush charactor 0
(integer) 8

lrange

语法:
lrange key start stop  # 返回链表中[start ,stop]中的元素,左数从0开始,右数从-1开始

示例:
127.0.0.1:6379> lrange charactor 2 5
1) "b"
2) "c"
3) "d"
4) "e"
127.0.0.1:6379> lrange charactor 0 -1
1) "0"
2) "a"
3) "b"
4) "c"
5) "d"
6) "e"
7) "f"
8) "g"
127.0.0.1:6379> lrange charactor 5 -1
1) "e"
2) "f"
3) "g"
127.0.0.1:6379> lrange charactor 5 6
1) "e"
2) "f"

lpop/rpop

语法:
lpop key  # 返回并删除链表尾元素
rpop key  # 返回并删除链表尾元素

示例:
127.0.0.1:6379> lpop charactor
"0"
127.0.0.1:6379> rpop charactor
"g"
127.0.0.1:6379> lrange charactor 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"

lrem

语法:
lrem key count value   # 从key链表中删除value值

注: 删除count的绝对值个value后结束
Count>0 从表头删除
Count<0 从表尾删除

示例:
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> rpush charactor a b c a d e b d a c a b
(integer) 12
127.0.0.1:6379> lrem charactor 2 a
(integer) 2
127.0.0.1:6379> lrange charactor 0 -1
 1) "b"
 2) "c"
 3) "d"
 4) "e"
 5) "b"
 6) "d"
 7) "a"
 8) "c"
 9) "a"
10) "b"
127.0.0.1:6379> lrem charactor -2 b
(integer) 2
127.0.0.1:6379> lrange charactor 0 -1
1) "b"
2) "c"
3) "d"
4) "e"
5) "d"
6) "a"
7) "c"
8) "a"

ltrim

语法:
ltrim key start stop   # 剪切key对应的链接,切[start,stop]一段,并把该段重新赋给key

示例:
127.0.0.1:6379> ltrim charactor 3 6
OK
127.0.0.1:6379> lrange charactor 0 -1
1) "e"
2) "d"
3) "a"
4) "c"

lindex

语法:
lindex key index   # 返回index索引上的值

示例:
127.0.0.1:6379> lindex charactor 2
"a"

llen

语法:
llen key  # 计算链接表的元素个数

示例:
127.0.0.1:6379> llen charactor
(integer) 4

linsert

语法:
linsert key BEFORE|AFTER pivot value  # 在key链表中寻找pivot,并在pivot值之前|之后插入value
# 注: 一旦找到一个search后,命令就结束了,因此不会插入多个value

示例:
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> rpush num 1 3 5 7 9
(integer) 5
127.0.0.1:6379> linsert num before 5 4
(integer) 6
127.0.0.1:6379> lrange num 0 -1
1) "1"
2) "3"
3) "4"
4) "5"
5) "7"
6) "9"
127.0.0.1:6379> linsert num after 5 6
(integer) 7
127.0.0.1:6379> lrange num 0 -1
1) "1"
2) "3"
3) "4"
4) "5"
5) "6"
6) "7"
7) "9"

rpoplpush

语法:
rpoplpush source destination  # 把source的尾部拿出,放在dest的头部,并返回该单元值

示例:
127.0.0.1:6379> rpush task a b c d
(integer) 4
127.0.0.1:6379> rpoplpush task job
"d"
127.0.0.1:6379> lrange task 0 -1
1) "a"
2) "b"
3) "c"
127.0.0.1:6379> lrange job 0 -1
1) "d"

blpop/brpop

语法:30
blpop key [key ...] timeout  # 等待弹出key的头元素
brpop key [key ...] timeout  # 等待弹出key的头元素
# 注:Timeout为等待超时时间,如果timeout为0,则一直等待
# 场景: 长轮询Ajax,在线聊天时,能够用到

示例:
# 会话1
127.0.0.1:6379> lrange job 0 -1
(empty list or set)

127.0.0.1:6379> brpop job 30   # 处于等待状态

# 会话2
127.0.0.1:6379> rpush job a
(integer) 1

# 会话1
127.0.0.1:6379> brpop job 30
1) "job"
2) "a"
(10.02s)
文章目录
  1. 1. lpush/rpush
  2. 2. lrange
  3. 3. lpop/rpop
  4. 4. lrem
  5. 5. ltrim
  6. 6. lindex
  7. 7. llen
  8. 8. linsert
  9. 9. rpoplpush
  10. 10. blpop/brpop