operating Redis Redis is a nosql type of database with data in memory, fast read and write speeds, Python operation Redis using Redis module, PIP installation
Import redis r = redis. Redis (host= 127.0.0.1 ' ,port= 6379,db=0 #指定连接redis的端口和ip以及哪个数据库 R. Set(' name ', ' value ')#set A value of type string R. Setnx(' name2 ', ' value ')#设置的name的值, set if name does not exist r. Setex ( ' Name3 ' , ' value ' , 3) #设置的name的值, and time-out time, key will automatically expire R. Mset(k1=' v1 ',k2=' v2 ')#批量设置值 R. Get(' name ')#获取值 Print(r. Mget(' K1 ',' K2 '))#批量获取key R. Delete(' name ')#删除值 R. Delete(' K1 ',' K2 ')#批量删除 #====== The following is the operation hash type R. Hset(' hname ', ' key ', ' value ')#set The value of the hash type R. Hset(' hname ', ' Key1 ', ' value2 ')#set The value of the hash type R. Hsetnx(' hname ',' Key2 ',' Value23 ')#给name为hname设置key和value, Unlike the one above, when key doesn't exist. #才会set R. Hmset(' hname ',{' k1 ':' v1 ', 'K2 ':' v2 '})# Bulk set Hash type key and value R. Hget(' name ', ' key ')#获取哈希类型的值 Print(r. Hgetall(' hname '))#获取这个name里所有的key和value R. Hdel(' hname ',' key ')#删除哈希类型的name里面指定的值 Print(r. Keys())#获取所有的key
Python Operation Redis