List 是基本的数据类型,列表。redis命令不区分大小写。
在redis中,可以把List用作 栈、队列、阻塞队列。
所有的list命令都是以“l”开头的
127.0.0.1:6379> lpush list one (integer) 1 127.0.0.1:6379> lpush list two (integer) 2 127.0.0.1:6379> lpush list three (integer) 3 127.0.0.1:6379>
127.0.0.1:6379> lrange list 0 -1 1) "three" 2) "two" 3) "one" 127.0.0.1:6379>
127.0.0.1:6379> lrange list 0 1 1) "three" 2) "two" 127.0.0.1:6379>
127.0.0.1:6379> rpush list righr
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "righr"
127.0.0.1:6379>
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "righr"
127.0.0.1:6379> lpop list
"three"
127.0.0.1:6379>
127.0.0.1:6379> lrange list 0 -1 1) "three" 2) "two" 3) "one" 4) "righr" 127.0.0.1:6379> lpop list "three" 127.0.0.1:6379> rpop list "righr" 127.0.0.1:6379>
127.0.0.1:6379> lindex list 1 "one" 127.0.0.1:6379> lindex list 0 "two" 127.0.0.1:6379>
127.0.0.1:6379> llen list
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379> lrem list 1 one
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "three"
3) "two"
通过下标截取指定的长度,这个list已经被改变了,截取了只剩下截取的元素。
127.0.0.1:6379> lpush mylist "hello" (integer) 1 127.0.0.1:6379> lpush mylist "hello1" (integer) 2 127.0.0.1:6379> lpush mylist "hello2" (integer) 3 127.0.0.1:6379> lpush mylist "hello3" (integer) 4 127.0.0.1:6379> ltrim mylist 1 2 OK 127.0.0.1:6379> lrange mylist 0 -1 1) "hello2" 2) "hello1"
127.0.0.1:6379> rpush mylist "hello" (integer) 1 127.0.0.1:6379> rpush mylist "hello1" (integer) 2 127.0.0.1:6379> rpush mylist "hello2" (integer) 3 127.0.0.1:6379> lrange mylist 0 -1 1) "hello" 2) "hello1" 3) "hello2" 127.0.0.1:6379> rpoplpush mylist myotherlist "hello2" 127.0.0.1:6379> lrange mylist 0 -1 1) "hello" 2) "hello1" 127.0.0.1:6379> lrange myotherlist 0 -1 1) "hello2"
127.0.0.1:6379> exists mylist
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379> lpush list value1 (integer) 1 127.0.0.1:6379> lrange list 0 -1 1) "value1" 127.0.0.1:6379> lset list 0 item OK 127.0.0.1:6379> lrange list 0 -1 1) "item" 127.0.0.1:6379>
127.0.0.1:6379> rpush mylist hell0 (integer) 1 127.0.0.1:6379> rpush mylist world (integer) 2 127.0.0.1:6379> linsert mylist before world other (integer) 3 127.0.0.1:6379> lrange mylist 0 -1 1) "hell0" 2) "other" 3) "world" 127.0.0.1:6379>
消息排队
消息队列 (Lpush Rpop 左进右出)
栈 (Lpush Lpop 左进左出)
原文:https://www.cnblogs.com/ckfuture/p/14395092.html