python 3.x - redis-py and hgetall behavior -


i played around flask microframework, , wanted cache stats in redis. let's have dict:

mydict = {} mydict["test"] = "test11" 

i saved redis with

redis.hmset("test:key", mydict) 

however after restore

stored = redis.hgetall("test:key") print(str(stored)) 

i see weird {b'test': b'test11'} stored.get("test") gives me none

mydict str method result looks fine {'test': 'test11'}. so, why binary marker added restored data? checked in redis-cli , don't see explicit b markers there. wrong hgetall?

this intended behavior. default, strings coming out of redis don't decoded. have couple options:

  • decode data yourself.
  • create client instance decode_responses argument, e.g., strictredis(decode_responses=true). decode strings come redis based on charset argument (which defaults utf-8). you're sure every response redis has string data want decoded utf-8. if you're using same client instance binary data such pickled object, shouldn't use option. in case, i'd suggest using separate client instance binary data.

source: https://github.com/andymccurdy/redis-py/issues/463#issuecomment-41229918


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -