Redis 的数据类型 - List 列表类型

LPUSH:向列表左端添加元素

  语法:LPUSH key value value...

    LPUSH testList1 a b c

    LPUSH testList1 c d e

    LRANGE testList1 0 -1

127.0.0.1:6379> LPUSH testList1 a b c
(integer) 3

127.0.0.1:6379> LPUSH testList1 c d e
(integer) 6

127.0.0.1:6379> LRANGE testList1 0 -1
1) "e"
2) "d"
3) "c"
4) "c"
5) "b"
6) "a"

 

RPUSH:向列表右端添加元素

  语法:RPUSH key value ...

    RPUSH testList1 test1 test2 test3

    RPUSH testList1 test3 test4

    LRANGE testList1 0 -1

127.0.0.1:6379> RPUSH testList1 test1 test2 test3
(integer) 9

127.0.0.1:6379> RPUSH testList1 test3 test4
(integer) 11

127.0.0.1:6379> LRANGE testList1 0 -1
 1) "e"
 2) "d"
 3) "c"
 4) "c"
 5) "b"
 6) "a"
 7) "test1"
 8) "test2"
 9) "test3"
10) "test3"
11) "test4"

  #如果列表不存在,则新建一个列表再插入,如果所选 key 不是列表类型,则会报错#

 

LPUSHX:向列表头部添加元素,只有 key 存在才可以添加

  语法:LPUSHX key value

    LPUSHX testList2 a

    LPUSH testList2 a

    LPUSHX testList2 b

    LRANGE testList2 0 -1

127.0.0.1:6379> LPUSHX testList2 a
(integer) 0

127.0.0.1:6379> LPUSH testList2 a
(integer) 1

127.0.0.1:6379> LPUSHX testList2 b
(integer) 2

127.0.0.1:6379> LRANGE testList2 0 -1
1) "b"
2) "a"

 

RPUSHX:向列表尾部添加元素,只有 key 存在在来添加

  语法:RPUSHX key value

    # RPUSHX 和 LPUSHX 原理相同,这里就不举例说明了#

 

LPOP:将列表头部的元素弹出

RPOP:弹出列表尾部的元素

  语法:LPOP key

       RPOP key

    LPOP testList1

    RPOP testList1

127.0.0.1:6379> LPOP testList1
"e"

127.0.0.1:6379> RPOP testList1
"test4"

 

LLEN:得到列表的长度

  语法:LLEN key

    LLEN testList2

    LLEN testList1

127.0.0.1:6379> LLEN testList2
(integer) 2

127.0.0.1:6379> LLEN testList1
(integer) 9

 

LRANGE:获取列表片段

  语法:LRANGE key start stop

    LRANGE testList1 0 -1   # 0 开始,-1 结束#

    LRANGE testList1 3 5

    LRANGE testList1 99 6  #如果 start 下标比列表的最大下标 end 大,返回的空列表#

    LRANGE testList1 3 99  #如果 stop 比列表长度大,返回到列表的末尾#

    LRANGE testList1 6 6  #这里 start 和 stop 相同表示取 key 下标为 6 这一个 key 的值#

127.0.0.1:6379> LRANGE testList1 0 -1
1) "d"
2) "c"
3) "c"
4) "b"
5) "a"
6) "test1"
7) "test2"
8) "test3"
9) "test3"

127.0.0.1:6379> LRANGE testList1 3 5
1) "b"
2) "a"
3) "test1"

127.0.0.1:6379> LRANGE testList1 99 6
(empty list or set)

127.0.0.1:6379> LRANGE testList1 3 99
1) "b"
2) "a"
3) "test1"
4) "test2"
5) "test3"
6) "test3"

127.0.0.1:6379> LRANGE testList1 6 6
1) "test2"

 

LREM:删除列表中指定的值

  语法:LREM key count value

    count > 0: 从列表的头开始,向尾部搜索,移除与 value 相等的元素,移除 count 个

    count <= 0: 从列表尾部向头搜索,移除与 value 相等的元素,移除 count 个

    count = 0, 移除列表中所有与 count 相等的值

    LPUSH testList2 m a r i o

    LREM testList2 2 b

    LREM testList2 -2 a

    LREM testList2 0 i

    LRANGE testList2 0 -1

127.0.0.1:6379> LPUSH testList2 m a r i o
(integer) 7

127.0.0.1:6379> LREM testList2 2 b
(integer) 1

127.0.0.1:6379> LRANGE testList2 0 -1
1) "o"
2) "i"
3) "r"
4) "a"
5) "m"
6) "a"

127.0.0.1:6379> LREM testList2 -2 a
(integer) 2

127.0.0.1:6379> LREM testList2 0 i
(integer) 1

127.0.0.1:6379> LRANGE testList2 0 -1
1) "o"
2) "r"
3) "m"

 

LINDEX:获得指定索引元素的值

  语法:LINDEX key index

    LINDEX testList1 3

    LINDEX testList1 -3

    LINDEX testList2 3   #超出范围会返回 nil #

127.0.0.1:6379> LINDEX testList2 3
(nil)

127.0.0.1:6379> LINDEX testList1 3
"b"

127.0.0.1:6379> LINDEX testList1 -3
"test2"

 

LSET:设置指定索引元素的值

  语法:LSET key index value

    LSET testList2 0 tom

    LRANGE testList2 0 -1

    LSET testList2 10 tom  #超出范围会报错#

    LSET testList22 0 tom  # key 不存在会报错#

127.0.0.1:6379> LSET testList2 0 tom
OK

127.0.0.1:6379> LRANGE testList2 0 -1
1) "tom"
2) "r"
3) "m"

127.0.0.1:6379> LSET testList2 10 tom
(error) ERR index out of range

127.0.0.1:6379> LSET testList22 0 tom
(error) ERR no such key

 

LTRIM:只保留列表的片段

  语法:LTRIM key start stop

    LPUSH testList3 tom jerry mario jack lucy

    LTRIM testList3 0 1

    LRANGE testList3 0 -1

    LPUSH testList4 tom jerry mario jack lucy chris david

    LTRIM testList4 1 -1

    LRANGE testList4 0 -1

    LTRIM testList4 1 1000

    LTRIM testList4 1000 2000   #列表被清空#

    LTRIM testList4 3000 2000   #起始点比结束点大,同样列表被清空#

127.0.0.1:6379> LPUSH testList3 tom jerry mario jack lucy
(integer) 5

127.0.0.1:6379> LTRIM testList3 0 1
OK

127.0.0.1:6379> LRANGE testList3 0 -1
1) "lucy"
2) "jack"

127.0.0.1:6379> LPUSH testList4 tom jerry mario jack lucy chris david

(integer) 7
127.0.0.1:6379> LTRIM testList4 1 -1
OK

127.0.0.1:6379> LRANGE testList4 0 -1
1) "chris"
2) "lucy"
3) "jack"
4) "mario"
5) "jerry"
6) "tom"

127.0.0.1:6379> LTRIM testList4 1 1000
OK

127.0.0.1:6379> LRANGE testList4 0 -1
1) "lucy"
2) "jack"
3) "mario"
4) "jerry"
5) "tom"

127.0.0.1:6379> LTRIM testList4 1000 2000
OK

127.0.0.1:6379> LRANGE testList4 0 -1
(empty list or set)

 

LINSERT:向列表插入元素

  语法:LINSERT key BEFORE|AFTER pivot value

    LPUSH testList5 tom jerry mario jack lucy chris david

    LINSERT testList5 BEFORE 'jerry' 'anni'

    LRANGE testList5 0 -1

    LINSERT testList5 BEFORE tim nick

    LINSERT testList5 AFTER tom time

    LINSERT testList6 AFTER tom tim

127.0.0.1:6379> LPUSH testList5 tom jerry mario jack lucy chris david
(integer) 7

127.0.0.1:6379> LINSERT testList5 BEFORE 'jerry' 'anni'
(integer) 8

127.0.0.1:6379> LRANGE testList5 0 -1
1) "david"
2) "chris"
3) "lucy"
4) "jack"
5) "mario"
6) "anni"
7) "jerry"
8) "tom"

127.0.0.1:6379> LINSERT testList5 BEFORE tim nick
(integer) -1

127.0.0.1:6379> LINSERT testList5 AFTER tom tim
(integer) 9

127.0.0.1:6379> LRANGE testList5 0 -1
1) "david"
2) "chris"
3) "lucy"
4) "jack"
5) "mario"
6) "anni"
7) "jerry"
8) "tom"
9) "tim"

127.0.0.1:6379> LRANGE testList6 0 -1
(empty list or set)

127.0.0.1:6379> LINSERT testList6 AFTER tom tim
(integer) 0

 

RPOPLPUSH:将元素从一个列表转到另一个列表

  语法:RPOPLPUSH source destination

    LPUSH testList6 red green blue

    RPOPLPUSH testList6 testList5

    RPOPLPUSH testList5 testList5  #列表名相同的时候,可以做到从列表的左边取值插入到右边的效果#

    LRANGE testList5 0 -1

127.0.0.1:6379> LPUSH testList6 red green blue
(integer) 3

127.0.0.1:6379> RPOPLPUSH testList6 testList5
"red"

127.0.0.1:6379> LRANGE testList5 0 -1
 1) "red"
 2) "david"
 3) "chris"
 4) "lucy"
 5) "jack"
 6) "mario"
 7) "anni"
 8) "jerry"
 9) "tom"
10) "tim"

127.0.0.1:6379> LRANGE testList6 0 -1
1) "blue"
2) "green"

127.0.0.1:6379> RPOPLPUSH testList5 testList5
"tim"

127.0.0.1:6379> RPOPLPUSH testList5 testList5
"tom"

127.0.0.1:6379> RPOPLPUSH testList5 testList5
"jerry"

127.0.0.1:6379> LRANGE testList5 0 -1
 1) "jerry"
 2) "tom"
 3) "tim"
 4) "red"
 5) "david"
 6) "chris"
 7) "lucy"
 8) "jack"
 9) "mario"
10) "anni"

 

BLPOP:BLPOP 是LPOP的阻塞版本

  语法:BLPOP key [key...] timeout

    BLPOP testList10 testList5 testList6 0无限延长

    BLPOP testList 0

    #这里用 BLPOP 作用一个不存在的列表时,客户端会一直等待,没有返回值# 

127.0.0.1:6379> BLPOP testList10 testList5 testList6 0
1) "testList5"
2) "jerry"

127.0.0.1:6379> BLPOP testList 0
_

    LPUSH testList tom  #这个时候我们在开启一个 6380 端口的客户端,给 testList 赋个值#

127.0.0.1:6380> LPUSH testList tom
(integer) 1

    #这个时候原客户端会立刻做出回应#

127.0.0.1:6379> BLPOP testList 0
1) "tom"
(162.99s)

 

posted @ 2017-06-26 19:58  Shuo_128  阅读(175)  评论(0编辑  收藏  举报