Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save unicode in redis but fetch error

Tags:

python

redis

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

like image 867
goofansu Avatar asked May 15 '12 10:05

goofansu


People also ask

Can UTF-8 data be stored and retrieved with the string data type without any loss or corruption of the data Redis?

Redis supports arbitrary bytes for values and UTF-8 is not a problem at all (if your client is properly converting the entered glyphs to the associated byte sequence.)

What encoding does Redis use?

The default encoding is utf-8, but this can be customized by specifying the encoding argument for the redis. Redis class. The encoding will be used to automatically encode any strings passed to commands, such as key names and values.


1 Answers

I think I've discovered the problem. After reading this, I had to explicitly decode from redis which is a pain, but works.

I stumbled across a blog post where the author's output was all unicode strings which was obv different to mine.

Looking into the StrictRedis.__init__ there is a parameter decode_responses which by default is False. https://github.com/andymccurdy/redis-py/blob/273a47e299a499ed0053b8b90966dc2124504983/redis/client.py#L446

Pass in decode_responses=True on construct and for me this FIXES THE OP'S ISSUE.

like image 104
jmoz Avatar answered Nov 15 '22 08:11

jmoz