1、LPUSH key value #(left頭部壓入)
2、RPUSH key value #(right尾部壓入)
127.0.0.1:6379> LPUSH mylist1 0 1 2 3 4 (integer) 5 127.0.0.1:6379> LRANGE mylist1 0 -1 1) "4" 2) "3" 3) "2" 4) "1" 5) "0" 127.0.0.1:6379> RPUSH mylist2 0 1 2 3 4 (integer) 5 127.0.0.1:6379> LRANGE mylist2 0 -1 1) "0" 2) "1" 3) "2" 4) "3" 5) "4"
三查看|設置 列表元素
#1.查看(LRANGE) LRANGE key start stop # 查看列表中所有元素 LRANGE key 0 -1 #2.獲取指定位置元素(LINDEX) LINDEX key index #3.設置指定位置元素的值(LSET) LSET key index value #4.獲取列表長度(LLEN) LLEN key
四頭尾彈出元素(LPOP | RPOP)
2.RPOP key : 從列表尾部彈出一個元素
3.RPOPLPUSH source destination : 從一個列表尾部彈出元素壓入到另一個列表頭部
4.rpoplpush 尾部彈出 頭部壓入
127.0.0.1:6379> LRANGE mylist1 0 -1 1) "4" 2) "3" 3) "2" 4) "1" 5) "8" 127.0.0.1:6379> LPOP mylist1 "4" 127.0.0.1:6379> RPOP mylist1 "8" 127.0.0.1:6379> LRANGE mylist1 0 -1 1) "3" 2) "2" 3) "1" 127.0.0.1:6379> RPOPLPUSH mylist1 mylist2 "1" 127.0.0.1:6379> LRANGE mylist1 0 -1 1) "3" 2) "2" 127.0.0.1:6379> LRANGE mylist2 0 -1 1) "1" 2) "0" 3) "1" 4) "2"
五移除指定元素(LREM)
LREM key count value
count>0:表示從頭部開始向表尾搜索,移除與value相等的元素,數量為count
count<0:表示從尾部開始向表頭搜索,移除與value相等的元素,數量為count
count=0:移除表中所有與value相等的值
127.0.0.1:6379> LRANGE mylist1 0 -1 1) "3" 2) "2" 127.0.0.1:6379> LPUSH mylist1 3 2 (integer) 4 127.0.0.1:6379> LRANGE mylist1 0 -1 1) "2" 2) "3" 3) "3" 4) "2" 127.0.0.1:6379> LREM mylist1 1 2 (integer) 1 127.0.0.1:6379> LRANGE mylist1 0 -1 1) "3" 2) "3" 3) "2" 127.0.0.1:6379> LREM mylist1 1 3 (integer) 1 127.0.0.1:6379> LRANGE mylist1 0 -1 1) "3" 2) "2"
六去除指定范圍外元素(LTRIM)
LTRIM key start stop
127.0.0.1:6379> LRANGE mylist2 0 -1 1) "1" 2) "0" 3) "1" 4) "2" 5) "3" 6) "4" 127.0.0.1:6379> LTRIM mylist2 0 -2 OK 127.0.0.1:6379> LRANGE mylist2 0 -1 1) "1" 2) "0" 3) "1" 4) "2" 5) "3"
例如:保存微博評論最后500條
LTRIM user001:comments 0 499
七列表中插入值(LINSERT)
key和pivot不存在,不進行任何操作
127.0.0.1:6379> LRANGE mylist2 0 -1 1) "0" 2) "1" 3) "2" 4) "3" 5) "4" 127.0.0.1:6379> LINSERT mylist2 after 2 666 (integer) 6 127.0.0.1:6379> LINSERT mylist2 before 4 888 (integer) 7 127.0.0.1:6379> LRANGE mylist2 0 -1 1) "0" 2) "1" 3) "2" 4) "666" 5) "3" 6) "888" 7) "4"
八阻塞彈出(BLPOP | BRPOP)
BRPOP key timeout
1、如果彈出的列表不存在或者為空,就會阻塞
2、超時時間設置為0,就是永久阻塞,直到有數據可以彈出
3、如果多個客戶端阻塞再同一個列表上,使用First In First Service原則,先到先服務
127.0.0.1:6379> BLPOP mylist2 0 1) "mylist2" 2) "3" 127.0.0.1:6379> BLPOP mylist2 0 1) "mylist2" 2) "2" 127.0.0.1:6379> BLPOP mylist2 0 1) "mylist2" 2) "1" 127.0.0.1:6379> BLPOP mylist2 0 # 阻塞了
九列表常用命令總結
# 增 1、LPUSH key value1 value2 2、RPUSH key value1 value2 3、RPOPLPUSH source destination 4、LINSERT key after|before value newvalue # 查 5、LRANGE key start stop 6、LLEN key # 刪 7、LPOP key 8、RPOP key 9、BLPOP key timeout 10、BRPOP key timeout 11、LREM key count value#(根據什么刪除) 12、LTRIM key start stop#(保留范圍內元素) # 改 13、LSET key index newvalue
Python操作列表
import redis r = redis.Redis(host='192.168.43.49',port=6379,db=0) # ['mysql','redis'] r.lpush('pylist','redis','mysql') # ['mysql','redis','django','spider'] r.rpush('pylist','django','spider') # ['mysql','redis','django','spider','AI'] r.linsert('pylist','after','spider','AI') # 5 print(r.llen('pylist')) # ['redis','django','spider'] r.lpop('pylist') r.rpop('pylist') # ['redis','django','spider'] print(r.lrange('pylist',0,-1)) # ['redis','spider'] r.lrem('pylist',0,'django') # 返回True,['redis'] r.ltrim('pylist',0,0) # 返回True,['spiderman'] r.lset('pylist',0,'spiderman') r.delete('pylist')
練習:
1、查看所有的鍵
keys *
2、向列表 spider::urls 中以RPUSH放入如下幾個元素:01_baidu.com、02_taobao.com、03_sina.com、04_jd.com、05_xxx.com
RPUSH spider::urls 01_baidu.com 02_taobao.com 03_sina.com 04_jd.com 05_xxx.com
3、查看列表中所有元素
LRANGE spider::urls 0 -1
4、查看列表長度
LLEN spider::urls
5、將列表中01_baidu.com 改為 01_tmall.com
LSET spider::urls 0 01_tmall.com
6、在列表中04_jd.com之后再加1個元素 02_taobao.com
LINSERT spider::urls after 04_jd.com 02_taobao.com
7、彈出列表中的最后一個元素
RPOP spider::urls
8、刪除列表中所有的 02_taobao.com
LREM spider::urls 0 02_taobao.com
9、剔除列表中的其他元素,只剩前3條
LTRIM spider::urls 0 2