如何将字符集应用于Redis ConnectionPool?

时间:2018-10-16 07:53:03

标签: python redis

Redis连接池的字符集出现问题(想使用字符串而不是字节)。

如果我这样连接Redis:

r = redis.StrictRedis(host="localhost", port=6379, charset="utf-8", decode_responses=True)
r.set('foo', ['eggs', 'spam'])
r.set('awa', 'ororor')
print(r)
print(r['foo'])
print(r['awa'])

一切正常:

StrictRedis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
['eggs', 'spam']
ororor

但是,如果我尝试使用ConnectionPool:

pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
r = redis.StrictRedis(connection_pool=pool, charset="utf-8", decode_responses=True)
r.set('foo', ['eggs', 'spam'])
r.set('awa', 'ororor')
print(r)
print(r['foo'])
print(r['awa'])

我得到了字节:

StrictRedis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
b"['eggs', 'spam']"
b'ororor'

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

也许 池= redis.ConnectionPool(主机='本地主机',端口= 6379,db = 0,decode_responses = True) r = redis.StrictRedis(connection_pool = pool,charset =“ utf-8”)