If you do not have Redis installed, you can see my other blog, which has resources and methods
Let’s first look at what python3 does to redis
- lrange(key , start , stop)
- lpush(key , value)
- rpush(key , value)
- lpop(key)
- rpop(key)
- blpop(key)
- brpop(key)
- brpoplpush(source,destination,timeout)
- lindex(key,index)
- linsert(key,before|after,privot,value)
- llen(key)
- lpushx(key)
- lrem(key , value , count)
- lset(key , index , value)
- ltrim(key , start , stop)
- rpoplpush(source , destination)
- rpushx(key , value)
If you want to see the full code directly, please go to github: github.com/dangsh/pyth…
The following is a specific example of detailed explanation and code:
① Lrange (key, start, stop) returns elements within the specified range in the key list. The range is specified by the offsets start and stop. The index arguments start and stop have a base of 0, that is, 0 for the first element of the list, 1 for the second element of the list, and so on.
import redis r = redis.Redis(host='localhost' , port='6379' , The db = 6, decode_responses = True) r.l push (" 0 ", 1, 2, 3, 4) print (r.l range (" 0 ", 0, 1))Copy the code
Lpush (key, value) lpush(key, value) lpush(key, value) lpush(key, value) lpush(key, value) lpush(key, value) An empty list is created and an LPUSH operation is performed and an error is returned if the key exists but is not of the list type
import redis r = redis.Redis(host='localhost' , port='6379' , db=6 ,decode_responses=True) r.lpush("1",1) print(r.lrange("1" , 0 , 1) list) # print "1" the entire contents of the r.l push (" 1 ", 1, 2, 3, 4) print (r.l range (" 1 ", 0, 1))Copy the code
③ Rpush (key, value) inserts one or more values of value into the end of the list key (rightmost). If there are multiple values, each value is inserted to the end of the table in left-to-right order. If the key does not exist, an empty list is created and an RPUSH operation is performed. An error is returned when the key exists but is not of list type.
import redis r = redis.Redis(host='localhost' , port='6379' , db=6 ,decode_responses=True) r.rpush("2",1) print(r.lrange("2" , 0 , List - 1)) # print "2" the entire contents of the r.r push (" 2 ", 1, 2, 3, 4) print (r.l range (" 2 ", 0, 1))Copy the code
④ LPOP (key) removes and returns the head element of the list key.
import redis r = redis.Redis(host='localhost' , port='6379' , Decode_responses =True) r.lpush("3",1,2,3,4) print(r.lrange("3", 0) List - 1)) # print "3" the entire contents of the r.l pop (" 3 ") print (r.l range (" 3 ", 0, 1))Copy the code
⑤ RPOP (key) removes and returns the last element of the list key.
import redis r = redis.Redis(host='localhost' , port='6379' , Decode_responses =True) r.lpush("4",1,2,3,4) print(r.lrange("4", 0) - 1)) # list printed the whole content of "4" r.r pop (" 4 ") print (r.l range (" 4 ", 0, 1))Copy the code
⑥ blPOP (key) The blpop command removes and retrieves the first element of the list. If there are no elements in the list, the list is blocked until the wait times out or an eject element is found. If the list is empty, return None. Otherwise, a list of two elements is returned, the first being the key of the ejected element and the second the value of the ejected element.
import redis r = redis.Redis(host='localhost' , port='6379' , The db = 6, decode_responses = True) r.r push,2,3,4,4,5,6 (" 6 ", 1) print (r. lpop (" 6 ")) print (r. lpop (" 6 ")) print (r. lpop (" 6 ")) Print (r.blpop("100", timeout=2)) None print(r.range ("6", 0, -1)) None print(r.range ("6", 0, -1)Copy the code
⑦brpop(key) The brpop command moves out and gets the last element of the list. If there is no element in the list, the list will be blocked until the wait times out or the eject element is found. If no elements are popped within the specified time, return None and wait time. Instead, return a list of two elements, the first being the key of the ejected element and the second the value of the ejected element.
import redis r = redis.Redis(host='localhost' , port='6379' , The db = 6, decode_responses = True) r.r push (" 7 ", 6,) print (r.l range (" 7 ", 0, List - 1)) # print "7" the entire contents of the print (r. rpop (" 7 ")) print (r. rpop (" 7 ")) print (r. rpop (" 7 ")) print (r. rpop (" 101 ", the timeout = 2)) 101 # for key None print(r.range ("7", 0, -1)) # print the entire contents of list "7"Copy the code
End brpoplpush (source, destination, a timeout) command pop-up a value from the list, will pop up the element is inserted into another list and return it; If the list has no elements, it blocks until the wait times out or a popup element is found. If no elements are popped within the specified time, return None and wait time. Instead, return a list of two elements, the first being the value of the ejected element and the second being the wait time.
import redis r = redis.Redis(host='localhost' , port='6379' , The db = 6, decode_responses = True) r.r push (" 8 ", 1, 2, 3) r.r push (" 88 ", 4 and 6) print (r.l range (8, 0, Print (r.range ("88", 0, Print (r.poplpush (SRC ="8", DST ="88",timeout=2) Print (r.poplpush (SRC ="44", DST ="22",timeout=2)) None print(r.range ("8", 0, -1)) None print(r.range ("8", 0, -1)) print(r.range ("88", 0, -1)Copy the code
Pet-name ruby lindex (key, index)
Gets the elements in the list by index. You can also use negative subscripts to indicate the last element of the list as -1
import redis r = redis.Redis(host='localhost' , port='6379' , Decode_responses =True) r.rpush("9",1,2,3) print(r.range ("9", 0, - 1)) # print a list of "8" the entire contents of the print (r.l index (9, 0)) output is the result of the # 1 print (r.l index (" 9 ", 1)) # output is the result of the 2 print (r.l index (" 9 ", 2)) output is the result of the 3 # Print (r.index ("9",3)) None print(r.index ("9",-1)) print(r.range ("9", 0,-1)Copy the code
Attending linsert (key, before | after, privot, value) is used to insert elements in a list of elements before or after. When the specified element does not exist in the list, no action is performed. When the list does not exist, it is treated as an empty list and no action is performed.
import redis r = redis.Redis(host='localhost' , port='6379' , Decode_responses =True) R.push ("10",1,2) print(r.range ("10", 0, -1)) "BEFORE", "2", 10) print (r.l range (10, 0, 1)) # results [' 1 ', '10', '2', '3'] r.r push (" 100 ", 1, 2, 3) r.l insert (" 100 ", "AFTER", "2", 10) print (r.l range (" 100 ", 0, 1)) # results [' 1 ', '2', '10', '3']Copy the code
①①llen(key) Returns the length of the list. If the list key does not exist, the key is interpreted as an empty list and 0 is returned. If the key is not a list type, an error is returned.
import redis r = redis.Redis(host='localhost' , port='6379' , Decode_responses =True) R.r push("11", 2,3) print(r.range ("11", 0, Print (r.len ("11")) print(r.len ("11")Copy the code
①② LPUSHx (key) Inserts one or more values into the head of an existing list. The operation is invalid if the list does not exist. The difference with lpush is that lpushx does not operate if the list does not exist, whereas Lpush creates a list
import redis r = redis.Redis(host='localhost' , port='6379' , db=6 ,decode_responses=True) r.rpush("12" , 1, 2, 3) r.r push (" 122 ", 1, 2, 3) print (r.l range (" 12 ", 0, 1)) # results for [' 1 ', '2', '3'] print (r.l range (" 122 ", 0. 1) # results for [' 1 ', '2', '3'] r.l push (" 123 ", 1) r.l pushx (" 1223 ", 1) print (r.l range (" 123 ", 0, 1) # results for [' 1 '] print (r.l range (" 1223 ", 0, 1)) # results for []Copy the code
①③ LREM (key, value, count) Removes elements that are equal to the value of parameter count from the list. The VALUE of COUNT can be one of the following: COUNT > 0: Searches from the top of the table to the bottom of the table and removes elements equal to VALUE. Count < 0: Searches from the end of the table to the top, removing elements equal to VALUE, the absolute VALUE of count. Count = 0: Removes all values equal to VALUE from the table. Returns the number of elements removed. Returns 0 if the list does not exist.
import redis r = redis.Redis(host='localhost' , port='6379' , db=6 ,decode_responses=True) r.rpush("13" , 'a', 'b', 'b', 'c', 'd', 'b') print (r.l range (" 13 ", 0, 1)) # results for [' a ', 'b', 'b', 'c', 'd', 'b'] r.l rem (" 13 ", "b", 2) print (r.l range (" 13 ", 0, 1)) # results for [' a ', 'c', 'd', 'b'] r.r push (" 133 ", 'a', 'b', 'b', 'c', 'd', 'b') print (r.l range (" 133 ", 0, 1)) # results for [' a ', 'b', 'b', 'c', 'd', 'b'] r.l rem (" 133 ", "b", - 2) print (r.l range (" 133 ", 0, 1)) # results for [' a ', 'b', 'c', 'd']Copy the code
①④lset(key, index, value) Sets the value of the element whose key subscript is index to value. An error is returned when the index argument is out of range, or when LSET is performed on an empty list (key does not exist).
import redis r = redis.Redis(host='localhost' , port='6379' , db=6 ,decode_responses=True) r.rpush("14" , 1, 2, 3, 4) print (r.l range (" 14 ", 0, 1)) # list printed the whole content of "14" r.l set (" 14 ", 1, 9) print (r.l range (" 14 ", 0, 1)) # results for [' 1 ', '9', '3', '4']Copy the code
①⑤ Ltrim (key, start, stop) trims a list, that is, only the elements within the specified range are kept in the list, and all elements outside the specified range are deleted. For example, run the LTRIM list 0 2 command to save only the first three elements of the list and delete the rest. The index arguments start and stop have a base of 0, that is, 0 for the first element of the list, 1 for the second element of the list, and so on. You can also use negative subscripts, -1 for the last element of the list, -2 for the next-to-last element of the list, and so on. An error is returned when key is not a list type.
import redis r = redis.Redis(host='localhost' , port='6379' , db=6 ,decode_responses=True) r.rpush("15" , 1, 2, 3, 4) print (r.l range (" 15 ", 0, 1)) # list printed the whole content of "14" r.l trim (" 15 ", 0, 1) print (r.l range (" 15 ", 0, 1)) # results in (' 1 ', '2')Copy the code
①⑥rpoplpush(source, destination) pops the last element in the source list and returns it to the client. Insert the element that pops up from source into the list Destination as the header element of the Destination list.
import redis r = redis.Redis(host='localhost' , port='6379' , The db = 6, decode_responses = True) r.r push (" 16 ", 1, 2, 3) r.r push (" 166 ", 4 and 6) print (r.l range (" 16 ", 0, 1)) # [' 1 ', '2', '3'] print(r.lrange("166" , 0 , -1)) # ['4', '5', '6'] print (r.r poplpush (SRC = "16", DST = "166")) # output is the result of the 3 print (r.l range (" 16 ", 0, 1)) # (' 1 ', '2') print (r.l range (" 166 ", 0, -1)) # ['3', '4', '5', '6']Copy the code
①⑦rpushx(key, value) insert value into the end of the list key if and only if the key exists and is a list. In contrast to RPUSH, RPUSHX does nothing when the key does not exist.
import redis r = redis.Redis(host='localhost' , port='6379' , db=6 ,decode_responses=True) # for i in range(10): # # r.l pop (" 17 ") r.l pop (" 177 ") # r.l pop (" 1777 ") r.r push (" 17 ", 1, 2, 3) r.r push (" 177 ", 1, 2, 3) print (r.l range (" 17 ", 0, 1) # results for [' 1 ', '2', '3'] print (r.l range (" 177 ", 0, 1)) # results for [' 1 ', '2', '3'] r.r push (" 177 ", 4) r.r pushx (" 1777 ", 4) print (r.l range (" 177 ", 0, 1)) # results for [' 1 ', '2', '3', '4'] print (r.l range (" 1777 ", 0, 1)) # results for []Copy the code
Full code address github.com/dangsh/pyth…