Redis is a key-value storage System . Similar to memcached, it supports storing more value types, including string (string), list ( linked list), set (set), Zset (sorted set-ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis.
1.1 Redis Data type
String string, K--"value" list, K---[11,11,22,33,44]set K, {"K1": xxx}k, [a], 22]k, [(11, 1), (13,2),]
1.2 Installation Configuration
Waiting to be mended
1.3 Operating mode
Redis-py provides two classes of Redis and Strictredis for implementing Redis commands, Strictredis is used to implement most of the official commands, and using official syntax and commands, Redis is a subclass of Strictredis, Used for backwards compatibility with older versions of Redis-py.
1.4 Connection Pool
Redis-py uses connection pool to manage all connections to a Redis server, avoiding the overhead of each establishment and release of the connection. By default, each Redis instance maintains its own pool of connections. You can create a connection pool directly, and then as a parameter Redis, you can implement multiple Redis instances to share a single connection pool.
Import Redispool = Redis. ConnectionPool (host= ' 192.168.101.11 ', port=6379) R = Redis. Redis (Connection_pool=pool) r.set (' foo ', ' Bar ') print (R.get (' foo ')) # B ' Bar '
1.5 Redis Operation--string String
1.5.1 Set (name, value, Ex=none, Px=none, Nx=false, Xx=false)
Set the value in Redis, default, not present, create, modify if present
Parameters:
EX, expiry time (seconds)
PX, Expiration Time (ms)
NX, if set to true, the current set operation executes only if name does not exist
XX, if set to true, the pre-post set operation is performed only if name exists
Format:
Set (name, value, Ex=none, Px=none, Nx=false, Xx=false)
1.5.2 Get (name)
Take value
1.5.3 setnx (name, value)
Set the value to perform the set operation (add) only if name does not exist
Import REDISR = Redis. Redis (host= ' 192.168.101.11 ', port=6379) r.set (' K1 ', ' v1 ') r.setnx (' K1 ', ' v2 ') #设置已有namer. Setnx (' K2 ', ' v2 ') # Set No Nameprint (R.get (' K1 ')) print (R.get (' K2 ')) # b ' v1 ' # K1 value not changed, name exists without operation # B ' v2 ' # K2 name does not exist, create add
1.5.4 Setex (name, value, time)
Parameter: time, expiration (number of seconds or Timedelta object)
Import Redisimport timer = Redis. Redis (host= ' 192.168.101.11 ', port=6379) R.setex (' K1 ', ' value ', 5,) print (R.get (' K1 ')) Time.sleep (5) Print (R.get (' K1 ') # b ' Value ' # expires before output # None #过期后输出, empty
1.5.5 Psetex (name, Time_ms, value)
Parameters: Time_ms, Expiration time (numeric milliseconds or Timedelta object)
Import Redisimport timer = Redis. Redis (host= ' 192.168.101.11 ', port=6379) R.psetex (' K2 ', ' Ten, ' V2 ') print (R.get (' K2 ')) Time.sleep (0.05) print (R.get (' K2 ') ) # B ' v2 ' # expired before output # None #过期后输出, empty
1.5.6 Mset (*args, **kwargs)
# Bulk Set Value R.mset (k1= ' v1 ', k2= ' v2 ', k3= ' v3 ') print (R.mget (' K1 ', ' K2 ', ' K3 ',)) # ordered # [b ' v1 ', B ' v2 ', B ' V3 ']r.mset ({' Key1 '): ' value1 ', ' key2 ': ' value2 ', ' Key3 ': ' Value3 ',} ' print (R.mget ({' K1 ', ' K2 ', ' K3 ',}) # unordered # [B ' v2 ', b ' v1 ', B ' v3 ']
1.5.7 mget (keys, *args)
# Batch Fetch value R.mset (k1= ' v1 ', k2= ' v2 ', k3= ' v3 ') print (R.mget (' K1 ', ' K2 ', ' K3 ',)) # ordered # [b ' v1 ', B ' v2 ', B ' V3 ']r.mset ({' key1 ': ') Value1 ', ' key2 ': ' value2 ', ' Key3 ': ' Value3 ',} ' print (R.mget ({' K1 ', ' K2 ', ' K3 ',}) # unordered # [B ' v2 ', b ' v1 ', B ' v3 ']
1.5.8 Getset (name, value)
# Getset (name, Vlaue) sets the new value and gets the original value r.set (' K1 ', ' v1 ') # Print (R.get (' K1 ')) print (R.getset (' K1 ', ' value1 ')) print (R.get (' K1 ') ‘))
1.5.9 GetRange (key, start, end)
# Gets the subsequence (obtained by Byte, non-character) # parameter: # Name,redis name # Start, start position (bytes) # end, end position (bytes) r.set (' K1 ', ' ABCDEFG ') print ( R.getrange (' K1 ', 2, 4) # b ' CDE ' R.set (' K1 ', ' Big Dragon ') print (R.getrange (' K1 ', 3, 6)) # B ' \xe9\xbe\x99 ' Print (str (R.getrange (' K1 ', 3, 6), encoding= ' Utf-8 ') # Dragon
1.5.10 SETRANGE (name, offset, vallue)
# Modify string contents, start backward substitution from the specified string index (if the new value is too long, add it backwards) # parameter: # Offset, string index, byte (three bytes for a kanji) # value, to set the value of R.set (' name ', ' Dalongge ') r.setrange (' name ', 2, ' Hu ') print (R.get (' name ')) # b ' Dahungge ' R.setrange (' name ', 4, ' DIIIIIIIIIII ') Print (R.get (' name ')) # output: B ' dahudiiiiiiiiiii ' R.set (' name ', ' Great Dragon Brother ') r.setrange (' name ', 3, ' Tiger ') print (str (r.get (' Name '), encoding= ' UTF8 ') # big Tiger Brother
1.5.11 setbit (name, offset, value)
# setbit (name, offset, value) # "to operate on binary representation bits Name:redis name Offset, index of bits (converts the ASCII code corresponding to the value into binary before indexing) Value can only be 1 or 0 "source =" 345 "r.set (' name ', source) for I in Source: print (i, Ord (i), Bin (ord (i))) #输出 value, corresponding value in ASCII code, Binary ' output of the corresponding value conversion: 3 0b110011 4 0b110100 5 0b110101 "r.setbit (' name ', 6, 0) #把第7位改为0, That is, 3 corresponds to 0b110001print (r.get (' name ')) #输出: 145# extension, conversion binary representation: Source = "foo" r.set (' name ', source) for I in Source: num = Ord (i) print (I,bin (num). replace (' B ', ')) # output "' F 01100110o 01101111o 01101111" ' R.setbit (' name ', 6, 0) # Change the 7th bit to 0, which means that 3 corresponds to 01100100print (r.get (' name ')) #输出: Doosource = "Big dragon" R.set (' name ', source) for I in Source: num = Ord (i) print (I, bin (num). replace (' B ', ')) # Special, if the source is the Chinese character "Big dragon" how to do? # A: For utf-8, each kanji account is 3 bytes, then "Wu Jianzi" has 9 bytes # for Chinese characters, the For loop is iterated by byte, then each byte is converted to decimal number at iteration, then the decimal number is converted to binary
1.5.12 getbit (name, offset)
# getbit (name, offset) # Gets the value of name corresponding to the value of a bit in the binary representation (0 or 1) r.set (' K1 ', "7") num = str (r.get (' K1 '), encoding= ' Utf-8 ') print (Bin ( Ord (num)) # output binary 0b110111print (r.getbit (' K1 ', 2)) #输出第2位print (r.getbit (' K1 ', 6)) #输出第6位print ( R.getbit (' K1 ', 5)) #输出第5位
1.5.13 Bitcount (Key, Start=none, End=none)
R.set (' K1 ', "7") num = str (r.get (' K1 '), encoding= ' Utf-8 ') print (Bin (ord (num))) # output binary 0b110111print (R.bitcount (' K1 ', 0, 1) # 5
1.5.14 Bitop (operation, dest, *keys)
# get multiple values and do a bitwise operation to save the final result to the new name corresponding to the value # parameter: # Operation,and (and), or (or), not (non), XOR (XOR) # dest, new redis name # *keys, to find the name# of Redis such as: Bitop ("and", ' new_name ', ' N1 ', ' N2 ', ' N3 ') # Get the value of N1,n2,n3 in Redis, and then say all the values do bit operations (seek the set), The result is then saved new_name the corresponding value
1.5.15 strlen (name)
# returns the byte length of the name corresponding value (a kanji 3 bytes) r.set (' K1 ', "Big Dragon") print (R.strlen ("K1")) # 6
1.5.16 incrbyfloat (self, name, amount=1.0)
# incrbyfloat (self, Name, amount=1.0) # The value corresponding to the increment name, when name does not exist, creates the Name=amount, otherwise, the increment. # # Parameters: # Name,redis name# amount, self-increment (float) r.set (' K1 ', 2.0) r.incrbyfloat (' K1 ', amount=1.1) print (R.get (' K1 ') # B ' 3.1 '
1.5.17 decr (self, name, amount=1)
# self-Subtract the value of name, when name does not exist, create name=amount, otherwise, subtract. # parameters: # Name,redis name # amount, self-meiosis (integer) r.set (' K1 ', 2) r.decr (' k1 ', amount = 1) print (R.get (' K1 ')) # B ' 1 '
1.5.18 Append (key, value)
# Append (key, value) # append content after Redis name Value # parameter: # key, Redis name# value, string to append R.set (' K1 ', ' v1 ') r.append (' K1 ', ' v2 ') print (R.get (' K1 ')) # B ' V1v2 '
Python operation Cache of Redis