redis基本操做 —— list

1、將一個或多個值插入到列表頭部spa

  命令:lpushcode

  格式:lpush key value1 [value2] ...blog

127.0.0.1:6379> lpush list_key 1
(integer) 1
127.0.0.1:6379> lpush list_key 2 3 4
(integer) 4

2、獲取列表指定範圍內的元素索引

  命令:lrangerem

  格式:lrange key begin_index end_indexget

127.0.0.1:6379> lpush list_key 1
(integer) 1
127.0.0.1:6379> lpush list_key 2 3 4
(integer) 4
127.0.0.1:6379> lrange list_key 0 1
1) "4"
2) "3"
127.0.0.1:6379> lrange list_key 0 0
1) "4"
127.0.0.1:6379> lrange list_key 2 3
1) "2"
2) "1"
127.0.0.1:6379> lrange list_key 0 10
1) "4"
2) "3"
3) "2"
4) "1"

3、將一個值插入到已存在的列表頭部class

  命令:lpushxim

  格式:lpushx key value客戶端

127.0.0.1:6379> del list_key
(integer) 1
127.0.0.1:6379> lrange list_key 0 10
(empty list or set)
127.0.0.1:6379> lpushx list_key 1
(integer) 0
127.0.0.1:6379> lrange list_key 0 10
(empty list or set)
127.0.0.1:6379> lpush list_key 1
(integer) 1
127.0.0.1:6379> lrange list_key 0 10
1) "1"
127.0.0.1:6379> lpushx list_key 2
(integer) 2
127.0.0.1:6379> lrange list_key 0 10
1) "2"
2) "1"

4、將一個或多個值插入到列表尾部top

  命令:rpush

  格式:rpush key value1 [value2] ...

127.0.0.1:6379> lrange list_key 0 10
1) "2"
2) "1"
127.0.0.1:6379> lpush list_key 3
(integer) 3
127.0.0.1:6379> lrange list_key 0 10
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> rpush list_key 4
(integer) 4
127.0.0.1:6379> lrange list_key 0 10
1) "3"
2) "2"
3) "1"
4) "4"

5、將一個值插入到已存在的列表尾部

  命令:rpushx

  格式:rpushx key value

127.0.0.1:6379> del list_key
(integer) 1
127.0.0.1:6379> lrange list_key 0 10
(empty list or set)
127.0.0.1:6379> rpushx list_key 1
(integer) 0
127.0.0.1:6379> lrange list_key 0 10
(empty list or set)
127.0.0.1:6379> rpush list_key 1
(integer) 1
127.0.0.1:6379> lrange list_key 0 10
1) "1"
127.0.0.1:6379> rpushx list_key 2
(integer) 2
127.0.0.1:6379> lrange list_key 0 10
1) "1"
2) "2"

6、移出並獲取列表的第一個元素

  命令:lpop

  格式:lpop key

127.0.0.1:6379> del list_key
(integer) 1
127.0.0.1:6379> lpush list_key 1 2 3 4 5 
(integer) 5
127.0.0.1:6379> lrange list_key 0 10
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> lpop list_key
"5"
127.0.0.1:6379> lrange list_key 0 10
1) "4"
2) "3"
3) "2"
4) "1"

7、移出並獲取列表的最後一個元素

  命令:rpop

  格式:rpop key

127.0.0.1:6379> lrange list_key 0 10
1) "4"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> rpop list_key
"1"
127.0.0.1:6379> lrange list_key 0 10
1) "4"
2) "3"
3) "2"

8、獲取列表長度

  命令:llen

  格式:llen key

127.0.0.1:6379> lrange list_key 0 10
1) "4"
2) "3"
3) "2"
127.0.0.1:6379> llen list_key
(integer) 3
127.0.0.1:6379> lpush list_key 5 6 7 
(integer) 6
127.0.0.1:6379> lrange list_key 0 10
1) "7"
2) "6"
3) "5"
4) "4"
5) "3"
6) "2"
127.0.0.1:6379> llen list_key
(integer) 6

9、經過索引獲取列表中的元素

  命令:lindex

  格式:lindex key index

127.0.0.1:6379> lrange list_key 0 10
1) "7"
2) "6"
3) "5"
4) "4"
5) "3"
6) "2"
127.0.0.1:6379> lindex list_key 4
"3"
127.0.0.1:6379> lindex list_key 0
"7"
127.0.0.1:6379> lindex list_key 7
(nil)
127.0.0.1:6379> lindex list_key 6
(nil)
127.0.0.1:6379> lindex list_key 5
"2"

10、移出並獲取列表的第一個元素, 若是列表沒有元素會阻塞列表直到等待超時或發現可彈出元素爲止

  命令:blpop

  格式:blpop key1 [key2] timeout

對於有元素的list:

127.0.0.1:6379> lpush l1 1 2 3 4
(integer) 4
127.0.0.1:6379> lpush l2 a b c d
(integer) 4
127.0.0.1:6379> lrange l1 0 10
1) "4"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> lrange l2 0 10
1) "d"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> blpop l1 l2 1
1) "l1"
2) "4"
127.0.0.1:6379> blpop l1 l2 1
1) "l1"
2) "3"
127.0.0.1:6379> blpop l1 l2 1
1) "l1"
2) "2"
127.0.0.1:6379> blpop l1 l2 1
1) "l1"
2) "1"
127.0.0.1:6379> blpop l1 l2 1
1) "l2"
2) "d"
127.0.0.1:6379> blpop l1 l2 1
1) "l2"
2) "c"
127.0.0.1:6379> blpop l1 l2 1
1) "l2"
2) "b"
127.0.0.1:6379> blpop l1 l2 1
1) "l2"
2) "a"
127.0.0.1:6379> blpop l1 l2 1
//1s後
(nil)
(1.05s)

對於無元素的list:

127.0.0.1:6379> blpop l1 l2 1
//1s後
(nil)
(1.06s)
127.0.0.1:6379> blpop l1 l2 10
//另外一客戶端調用 lpush l1 1
1) "l1"
2) "1"
(1.54s)
127.0.0.1:6379> blpop l1 l2 0
//一直等待

11、移出並獲取列表的最後一個元素, 若是列表沒有元素會阻塞列表直到等待超時或發現可彈出元素爲止

  命令:brpop

  格式:brpop key1 [key2] timeout

操做與blpop相同,此處略

12、移除列表的最後一個元素,並將該元素添加到另外一個列表並返回

  命令:rpoplpush

  格式:rpoplpush source target

127.0.0.1:6379> del l1 l2
(integer) 1
127.0.0.1:6379> lpush l1 1 2 3
(integer) 3
127.0.0.1:6379> lrange l1 0 10
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> lrange l2 0 10
(empty list or set)
127.0.0.1:6379> rpoplpush l1 l2
"1"
127.0.0.1:6379> lrange l1 0 10
1) "3"
2) "2"
127.0.0.1:6379> lrange l2 0 10
1) "1"
127.0.0.1:6379> rpoplpush l1 l2
"2"
127.0.0.1:6379> rpoplpush l1 l2
"3"
127.0.0.1:6379> rpoplpush l1 l2
(nil)
127.0.0.1:6379> lrange l1 0 10
(empty list or set)
127.0.0.1:6379> lrange l2 0 10
1) "3"
2) "2"
3) "1"

十3、從列表中彈出一個值,將彈出的元素插入到另一個列表中並返回它; 若是列表沒有元素會阻塞列表直到等待超時或發現可彈出元素爲止

  命令:brpoplpush

  格式:brpoplpush source target timeout

source有元素時:

127.0.0.1:6379> del l1 l2
(integer) 0
127.0.0.1:6379> lpush l1 1 2 3
(integer) 3
127.0.0.1:6379> lrange l1 0 10
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> lrange l2 0 10
(empty list or set)
127.0.0.1:6379> brpoplpush l1 l2 1
"1"
127.0.0.1:6379> lrange l1 0 10
1) "3"
2) "2"
127.0.0.1:6379> lrange l2 0 10
1) "1"
127.0.0.1:6379> brpoplpush l1 l2 1
"2"
127.0.0.1:6379> brpoplpush l1 l2 1
"3"
127.0.0.1:6379> brpoplpush l1 l2 1
//1s後
(nil)
(1.06s)
127.0.0.1:6379> lrange l1 0 10
(empty list or set)
127.0.0.1:6379> lrange l2 0 10
1) "3"
2) "2"
3) "1"

source無元素時,與blpop brpop相似

十4、在列表的第一個指定元素pivot前或者後插入元素value

  命令:linsert

  格式:linsert key before|after pivot value

127.0.0.1:6379> lpush list_key 1 2 1 3 1 4
(integer) 6
127.0.0.1:6379> lrange list_key 0 10
1) "4"
2) "1"  //第一個指定元素"1"
3) "3"
4) "1"
5) "2"
6) "1"
127.0.0.1:6379> linsert list_key after 1 5
(integer) 7
127.0.0.1:6379> lrange list_key 0 10
1) "4"
2) "1"  //第一個指定元素"1"
3) "5"
4) "3"
5) "1"
6) "2"
7) "1"
127.0.0.1:6379> linsert list_key before 1 6
(integer) 8
127.0.0.1:6379> lrange list_key 0 10
1) "4"
2) "6"
3) "1"  //第一個指定元素"1"
4) "5"
5) "3"
6) "1"
7) "2"
8) "1"

十5、從列表頭部開始,移除count個指定元素value

  命令:lrem

  格式:lrem key count value

127.0.0.1:6379> lrange list_key 0 10
1) "4"
2) "6"
3) "1"
4) "5"
5) "3"
6) "1"
7) "2"
8) "1"
127.0.0.1:6379> lrem list_key 1 1
(integer) 1
127.0.0.1:6379> lrange list_key 0 10
1) "4"
2) "6"
3) "5"
4) "3"
5) "1"
6) "2"
7) "1"
127.0.0.1:6379> lrem list_key 2 1
(integer) 2
127.0.0.1:6379> lrange list_key 0 10
1) "4"
2) "6"
3) "5"
4) "3"
5) "2"
127.0.0.1:6379> lrem list_key 1 1
(integer) 0
127.0.0.1:6379> lrange list_key 0 10
1) "4"
2) "6"
3) "5"
4) "3"
5) "2"

十6、經過索引設置列表元素的值

  命令:lset

  格式:lset key index value

127.0.0.1:6379> rpush list_key 1 2 3 4 5
(integer) 5
127.0.0.1:6379> lrange list_key 0 10
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> lset list_key 0 10
OK
127.0.0.1:6379> lrange list_key 0 10
1) "10"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> lset list_key 3 10
OK
127.0.0.1:6379> lrange list_key 0 10
1) "10"
2) "2"
3) "3"
4) "10"
5) "5"

十7、對一個列表進行修剪,刪除指定下標區間[start,stop]以外的元素

  命令:ltrim

  格式:ltrim key start stop

127.0.0.1:6379> del list_key
(integer) 1
127.0.0.1:6379> rpush list_key 1 2 3 4 5 6 7 8 9
(integer) 9
127.0.0.1:6379> lrange list_key 0 10
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
127.0.0.1:6379> ltrim list_key 3 6
OK
127.0.0.1:6379> lrange list_key 0 10
1) "4"
2) "5"
3) "6"
4) "7"
相關文章
相關標籤/搜索