One, Python installs the Redis module
PIP3 install-y Redis
Two, Python connects to Redis
Import REDISR = Redis. Redis (host= ' 192.168.1.108 ', port=6379,password= ' Dasini ', db=0) # r = Redis. Strictredis (host= ' 192.168.1.108 ', port=6379,db=0) port can default to not write R.set (' name ', ' Test ') print (R.get (' name ')) # The result of the Redis output is a binary stream by default, so we can add decode_resopnses=true.import REDISR = Redis to facilitate other code processing. Redis (host= ' 192.168.1.108 ', port=6379,password= ' Dasini ', db=0,decode_responses=true) R set (' name ', ' Python ') print ( R.get (' name '))
Third, use connection pooling
Import= Redis. ConnectionPool (host='192.168.1.108', port=6379,password='dasini ', db=0,decode_responses== Redis. Redis (connection_pool=Pool) r.set ('name2','Ruby ' )print(r.get ('name2'))
Four, the operation of common data types
A,string operation
Import Redispool=redis. ConnectionPool (host= ' 192.168.1.108 ', port=6379,password= ' Dasini ', db=0,decode_responses=true) R = Redis. Redis (Connection_pool=pool) #set (name, value, Ex=none, Px=none, Nx=false, Xx=false) creates a key and the value ' ex ' indicates the expiration time in s units px Represents the expiration time, in Ms NX when the value of NX is true, if name does not exist, then the set operation is performed XX when the value of XX is true, if name exists, then the set operation ' R.set (' name ', ' Python ') print ( R.get (' name ') # Run Result: python# Create an expiration time of keyr.set (' sex ', ' male ', ex=100) print (R.get (' sex ')) #setnx, and the parameter NX in set, If key exists, the set operation is not performed. R.setnx (' name ', ' Ruby ') print (R.get (' name ')) # Run Result: Python#setex,psetex and set in the parameter ex, you can set the life cycle, where the Setex unit is S, Psetex Unit for Ms.r.setex (' address ', ' sh ', [+] R.psetex (' mail ', ' + ', ' [email protected] ') #mset Batch setup keyr.mset (name1= ' Tom ', name2= ' Jack ') print (R.mget (' name1 ', ' name2 ')) #append往 key to append content R.set (' mystring ', ' good Good Study study ') R.append (' mystring ', ' Day-up ') Print (R.get (' mystring '))
B,list Common operations
#删除testlistr. Delete (' testlist ') #从列表左边插r. Lpush (' testlist ', 1) r.lpush (' Testlist ', 2) #从列表右边插r. Rpush (' Testlist ', 3) # The contents of the output list print (R.lrange (' testlist ', 0,-1)) #计算列表的元素print (R.llen (' testlist ')) #在列表指定位置插入值r. Linsert (' testlist ', ' after ', 3,4) #在testlist列表中找到下标为3的元素然后在它后面插入4print (R.lrange (' testlist ', 0,-1)) #更改列表中的指定下标的值r. LSet (' Testlist ', 0,10000) Print (R.lrange (' testlist ', 0,-1)) #删除列表中的元素r. Lpush (' testlist ', 1) print (R.lrange (' testlist ', 0,-1)) R.lrem (' Testlist ', 1) print (R.lrange (' testlist ', 0,-1)) #删除列表中最后1个元素或者第一个元素r. Lpop (' testlist ') print (R.lrange (' testlist ', 0,- 1) r.rpop (' Testlist ') print (R.lrange (' testlist ', 0,-1)) #根据下标获取某个值r. Lindex (' testlist ', 0) print (r.lrange (' testlist ') , 0,-1))
C, unordered collection operation (set)
#sadd add elements to the collection R.sadd (' Set1 ', 1) r.sadd (' Set1 ', 2) #scard the number of elements in the output set print (R.scard (' Set1 ')) #smembers get all the elements in the dictionary print ( R.smembers (' Set1 ')) #spop Delete the last element in the collection and return it to print (R.spop (' Set1 ')) print (R.smembers (' Set1 ')) #srem Delete the elements in the collection R.sadd (' Set1 ', 4) r.srem (' Set1 ', ' 4 ') print (r.smembers (' Set1 '))
D, ordered set (sorted set), for an ordered set, each element has two values
#zadd Create add ordered collection, the default is to display the # r.delete (' Set2 ') in ascending order (' Set2 ', ' apple ', ' banana ', ' + ') print (R.zrange (' Set2 ', 0,-1)) # Reverse-order Form output print (R.zrevrange (' Set2 ', 0,-1)) #返回有序集合元素的个数print (R.zcard (' Set2 ')) #删除有序集合的元素print (R.zrem (' Set2 ', ' apple ') ) Print (R.zrange (' Set2 ', 0,-1))
E,hash operation
#hset Create a hash add element r.hset (' H1 ', ' name ', ' Dasini ') print (R.hget (' H1 ', ' name ')) #hmset Create multiple elements r.hmset (' H2 ', {' name ': ' Tom ', ' Sex ': ' Male '} ' Print (R.hmget (' H2 ', ' name ', ' sex ') #hlen determine the number of hashes (R.hlen (' H2 ')) #hkeys Gets the value of all keys in the hash set corresponding to print (R.hkeys (' H2 ')) #hvals获取set对应的hash中所有的value的值print (r.hvals (' H2 ')) # Hdel Delete print (R.hdel (' H2 ', ' name ')) print (R.hgetall (' H2 ')) from the key value of the specified key in the hash of the set corresponding to
Python Operation Redis