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

2024-04-26 07:02:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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'

我做错什么了?你知道吗


Tags: redislocalhosthostdbfooportspameggs
1条回答
网友
1楼 · 发布于 2024-04-26 07:02:12

也许。。 池=redis.ConnectionPool(host='localhost',port=6379,db=0,decode\u responses=True) r=雷迪斯。雷迪斯(connection_pool=pool,charset=“utf-8”)

相关问题 更多 >