Python连接redis时要注意的点

时间:2021-12-11 12:47:47

一、一般连接redis情况

  

 from redis import Redis
# 实例化redis对象
rdb = Redis(host='localhost', port=6379, db=0)
rdb.set('name', 'root')
5 name = rdb.get('name')
6 print(name)

  这种情况连接数据库,对数据的存取都是字节类型,存取时还得转码一下,一般不推荐这种方法

二、连接池连接redis

  

 from redis import ConnectionPool, Redis
pool = ConnectionPool(host='localhost', port=6379, db=0)
rdb = Redis(connection_pool=pool)
rdb.get('name')

  这种连接池连接redis时也会有上述情况出现,所以一般也不推荐

三、redis连接的推荐方式

  为了避免上述情况,redis在实例化的时候给了一个参数叫decode_response,默认值是False,如果我们把这个值改为True,则避免了转码流程,直接对原数据进行操作

 from redis import ConnectionPool, Redis
pool = ConnectionPool(host='localhost', port=6379, db=0, decode_responses=True)
rdb = Redis(connection_pool=pool)
4 rdb.set('name2', 'rooter')
5 name2 = rdb.get('name2')
6 print(name2)