I’m using mongodb and redis, redis is my cache.
I’m caching mongodb objects with redis-py:
obj in mongodb: {u'name': u'match', u'section_title': u'\u6d3b\u52a8', u'title':
u'\u6bd4\u8d5b', u'section_id': 1, u'_id': ObjectId('4fb1ed859b10ed2041000001'), u'id': 1}
the obj fetched from redis with hgetall(key, obj) is:
{'name': 'match', 'title': '\xe6\xaf\x94\xe8\xb5\x9b', 'section_title':
'\xe6\xb4\xbb\xe5\x8a\xa8', 'section_id': '1', '_id': '4fb1ed859b10ed2041000001', 'id': '1'}
As you can see, obj fetched from cache is str instead of unicode, so in my app, there is error s like :’ascii’ codec can’t decode byte 0xe6 in position 12: ordinal not in range(128)
Can anyone give some suggestions? thank u
Update, for global setting, check jmoz’s answer.
If you’re using third-party lib such as
django-redis, you may need to specify a customizedConnectionFactory:Assuming you’re using redis-py, you’d better to pass
strinstead ofunicodeto Redis, or else Redis will encode it automatically for*setcommands, normally in UTF-8. For the*getcommands, Redis has no idea about the formal type of a value and has to just return the value instrdirectly.Thus, As Denis said, the way that you storing the object to Redis is critical. You need to transform the value to
strto make the Redis layer transparent for you.Also, set the default encoding to UTF-8 instead of using
ascii