Redis is a database of key-value structures, and the format of value can be String,set (), List,map collection (that is, Python dict), sorted set (ordered collection)
1. Connect to Redis Database
Description Episode: Redis and Strictredis, the latter being the official recommended use
Import Redis
R=redis. Redis (host= ' ID address ', port=6379,password= ', db=12) #指定连接redis的端口, port number, and which database # connection redis# Add and delete changes
Set function:
R.set (' abc ', ' 12345686 ') #50表示50s之后连接失效, when there is no key, add the value of the key, change its value when it exists, and return a value of true
Print (R.set (' abc ', ' 12345686 ')) #打印返回值为True
Print (R.get (' abc ')) #获取到key的值
Setnx function:
R.setnx (' 1314520 ', ' Jidad ') #如果值不存在则新增一个, if present, do not modify the original value
Print (R.get (' 1314520 ')) #获取到1314520的值
Delete function: Remove key and ignore if key does not exist
R.delete (' abc ') #指定一个key, delete the key and value if key exists return 1, if not present then return 0
Print (R.delete (' abc ')) #返回值0或者1
Binary: Byte type
Decode function: Turn byte type to string
Encode function:
To turn a string type to a byte type
Str= ' saksj Jadja '
Print (Str.encode ())
Keys function: Gets all keys, does not exist then returns to null
Print (R.keys (' *o ')) #获取到所有的key值
Printing results:
exists function:
Print (r.exists (' abc ')) #判断key是否存在. Exists as true and does not exist for Fasle
Flushall function:
R.flushall () #清空redis里所有的数据
Flushdb function:
R.FLUSHDB () #清空当前数据库里面的所有的key
Second, the hash type key
Hash type: Can be understood as a dictionary nested dictionary
function Python to operate Redis