连接Redis数据库:
import redis,time
conn_pool = redis.ConnectionPool(host="******",db=0, port=6379)
r = redis.Redis(connection_pool=conn_pool)
keys = r.keys() #获取所有的key
key-value数据类型,如下,获取value:
num = r.get('***FlowContrl')
print(num,type(num),int(num))
结果:
Redis 数据库hash数据类型 key-Hash Key-Hash Value,如下,获取键值:
print(r.hgetall("***key"))#获取key所有Hash Key-Hash Value键值对
picCount = r.hget("***key","picCount") #获取key中Hash Key为"picCount"的Hash Value
print(picCount,type(picCount))
结果:
从上边的结果可看出获取value值数据类型都是'bytes',有时需要bytes与str需要相互转换,可用如下方式:
# str to bytes
sb = bytes(s, encoding = "utf8")
# bytes to str
bs = str(b, encoding = "utf8")