operate redis

  Redis is also a database, a relational database, without SQL, all redis data is stored in memory. The performance of redis itself is very good, supporting 30w reads and writes per second.

  import redis

  r = redis.Redis(host='118.xx.x.xx',password='xxxxx',db=1,port=6379)#port default 6379 db=1 first database

#Add , delete, modify and check 
r.set('key value','value value') #Add a new value in the database #Modification
is also set
r.delete('key value')
r.setex('python_123','hahaha' ,20) #Set the expiration time of the key, the last parameter is seconds
hwt = r.get('hwt')
print(hwt.decode())#Conversion code
print(r.keys('*xxx*'))# Get all the keys
print(r.get('sdfsdf'))
r.set('Scorpio:mpp','Hehehe')
r.get('Scorpio:mpp' ) #The
above operations are for string type
for k in r.keys(): #delete all keys
r.delete(k)
 

 

# Hash type hash nested dictionary 
r.hset('stu_info','a','1m8 100w deposit') #Save the value
r.hset('stu_info','b','Wave, for.... ')
r.hset('stu_info','c','...then...')
print(r.hget('stu_info','a').decode()) #Specify big key and small key Get the corresponding data
print(r.hgetall('stu_info')) #Get all k and -v inside
stu_info = r.hgetall('stu_info')
r.hdel('stu_info','gyx') #Delete the specified key
r.delete('stu_info') #Delete the entire key
r.expire('aaa',100) #The first key sets the expiration time
new_stu_info = {}
for k,v in stu_info.items():
new_stu_info[k.decode ()] = v.decode()
print(new_stu_info)
print(r.type('stu_info')) #Check what type of key is
print(r.type('zll'))

#Character conversion
# s='Hehe' 
# s.encode() # Convert the string to binary
# hwt = b'sdfsdfsdf'
# hwt.decode() # Convert the bytes type to a string

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325571837&siteId=291194637