First, Redis Introduction
Non-relational database: MongoDB, Redis
Redis data is all in memory.
Redis itself supports 30w reads and writes per second, and its performance is very good,
After the Redis restarts, the data disappears and is not persistent, but you can set the data inside the Redis to disk before restarting, and then fetch it again after rebooting.
Second, Redis usage
Usage of String type:
1 ImportRedis2R = Redis. Redis (host='xxx.xxx.xx.xxx', password='123456', db=0)#Port number Default 6379, redis default has 16 libraries, for 1-153 #key has uniqueness.4R.set ('6j','Handsome! ')#a new value in the database5R.set ('Nhy','Handsome! ')#a new value in the database6 #modification is also set7R.set ('Nhy','Cow! ')#a new value in the database8 #Delete:9R.delete ('Nhy')#DeleteTen OneR.setex ('6j','ha ha haha', 20)#timer, set key expiration time 20s, unit is seconds A - Print(R.get ('6j'))#The result is a byte format, a binary format, and a change back string with decode - Print(R.get ('6j'))#gets the nonexistent key that returns none theLj= R.get ('6j')#the printed result is malformed and is in byte format and needs to be decode a bit - Print(Lj.decode ()) - Print(R.get ('6j'). Decode ()) -s ='hehe' +S.encode ()#turn the string into binary -Nhy = b'SDFSEREWF' +Nhy.decode ()#turn the bytes type into a string A at - Print(R.keys ())#get all the keys - Print(R.keys ('N'))#get all n-Beginning keys - - Set folder: -R.set ('Scorpio: LJ','hehehe')#generate Scorpio folder, key is Lj,value to hehehe inR.set ('session:6j','1 cents')#Generate session folder, key is 6j,value for 1 cents - to #Remove all keys + forKinchR.keys (): -R.delete (k)
Usage of hash Type:
In addition to string types, There are hash type hashes, which are the same as nested dictionaries.
# # {{zhangsan:xxx},{lisi:yyy}}
1R.hset ('Stu_info', 6j,'1M6 100w Deposit')#Increase2R.hset ('Stu_info','Zhangsan','Lang for homework')#Increase3R.hset ('Stu_info','Lisi','find a dog to Kit Zhangsan')#Increase4 5 Print(R.hget ('Stu_info','Zhangsan'). Decode ())#specify big key and small key to get corresponding data6 Print(R.hgetall ('Stu_info'))#get all the K and V inside, can't use decode, because the dictionary has no decode method7 #to get a normal dictionary, you can look like this:8Stu_info = R.hgetall ('Stu_info')9New_stu_info = {}Ten #print (Stu_info.items ()) One forKvinchStu_info.items (): ANew_stu_info[k.decode ()] =V.decode () - #print (K.decode) - #print (V.decode) the Print(New_stu_info) - -R.hdel ('Stu_info','Zhangsan')#Delete the specified key -R.delete ('Stu_info')#Delete entire key + - #to view the type of key: + Print(R.type ('Stu_info')) A Print(R.type ('6j')) at - Print(R.ttl ('Stu_info'))#get Expiration time, none is permanent, show as-1 in the Redis Visual interface tool - #r.expire (' aaa ', +) #第一个key设置失效时间, valid for string and hash
View Code
Python Learning Note _redis