1. Redis operation
import redis r=redis.Redis(host='211.149.218.16',port=6379,password='123456',db=2) r.set('suki','suki123')#insert a value of type string The values read by print(r.get('suki').decode())#redis are all Byte types, which are converted to strings with decode r.delete ('suki') r.setex('suki','hahahaha',10)#Specify the expiration time of the key r.setex(key,value,time) #hash type r.hset('sessions','suki','123456') #rediskey:sessions rowkey:suki print(r.hget('sessions','suki'))#Get data print(r.hgetall('sessions'))#Get all data redis_data=r.hgetall('sessions') all_data={} for k,v in redis_data.items(): k=k.decode() v=v.decode() all_data[k]=v print(redis_data) print(all_data) #The following is the kind with a hierarchy and a folder r.set('txz:suki','did not pay') r.set('txz:ski:ski2','paid') print(r.keys()) print(r.keys('txz*'))#Fuzzy match, get the key at the beginning of txz print(r.type('txz'))#Get the type of key
Second, redis packaging
def op_redis(host,password,k,v=None,port=6379,db=0): r=redis.Redis(host=host,password=password,port=port,db=db) if v:#If the value is passed in, it means that it is a write operation r.set(k,v) res='ok' else: res=r.get(k) if res: #return value if there is a value res=res.decode() else: res=None return res