1, LPUSH key value # (left head portion press-fitted)
2, RPUSH key value # (right tail pressed)
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"
Three to see | set list elements
# 1. View (LRANGE) LRANGE Start Key STOP # view a list of all the elements LRANGE Key 0 -1 # 2. Get the specified location element (LINDEX) LINDEX Key index # 3. Set the value of the specified position of the element (the LSET) the LSET Key index value # 4. Get list length (LLEN) LLEN Key
Four head and tail pop-up element (LPOP | RPOP)
2.RPOP key: Pops an element from the end of the list
3.RPOPLPUSH source destination: from a pop-up element is pressed into the tail of the list to another list header
4.rpoplpush head pressed into the tail pop
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"
Five remove the specified element (LREM)
LREM key count value
count> 0: indicates the search from the head end of the table, with the value equal to the removal elements, the number of count
count <0: header indicates the start of the search, and removed from the tail element value equal to the number of count
count = 0: all value equal to the value in the table is removed
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"
Six removed outside a specified range of elements (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"
For example: Save the last 500 micro-blog comment
LTRIM user001:comments 0 499
Seven the list of insert values (LINSERT)
and pivot key does not exist, no operation is performed
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"
Eight blocked pop-up (BLPOP | BRPOP)
BRPOP key timeout
1, if the pop-up list does not exist or is empty, it will clog
2, the timeout is set to 0, that is, permanently block until there is data to eject
3, if multiple clients on the same list of blocked again, the principle of using First First Service In, first come first served
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 # 阻塞了
Nine a list of frequently used commands summary
# Increase . 1 , LPUSH Key VALUE1 value2 2 , RPUSH Key VALUE1 value2 . 3 , RPOPLPUSH Source Where do you want . 4, linsert Key After | before value newValue # investigation . 5 , LRANGE Key Start STOP . 6 , LLEN Key # deleted . 7 , LPOP Key . 8 , RPOP Key . 9 , timeout BLPOP Key 10 , Key BRPOP timeout . 11, LREM Key COUNT value # (depending on what deleted) 12 is, LTRIM Start Key STOP # (the reserved range of elements) # change 13, LSET key index newvalue
Python list of actions
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'] ,'pylist' R.linsert ('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')
Exercise:
1, view all keys
keys *
2, to the list RPUSH spider :: urls to put these several elements: 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, view a list of all the elements
LRANGE spider::urls 0 -1
4 for a list of length
LLEN spider::urls
5, the list 01_baidu.com changed 01_tmall.com
LSET spider::urls 0 01_tmall.com
6, in the list after 04_jd.com plus an element 02_taobao.com
LINSERT spider::urls after 04_jd.com 02_taobao.com
7, pop the last element in the list
RPOP spider::urls
8, delete the list of all the 02_taobao.com
LREM spider::urls 0 02_taobao.com
9, excluding other elements in the list, only three former
LTRIM spider::urls 0 2