I played around with flask microframework, and wanted to cache some stats in redis. Let's say I have this dict:
mydict = {}
mydict["test"] = "test11"
I saved it to 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'}
so stored.get("test")
gives me None
mydict
str method result looks fine {'test': 'test11'}
. So, why this binary marker added to restored data? I also checked in redis-cli and don't see explicit b markers there. Something wrong with hgetall?
The Redis command HGETALL retrieves all the keys and their values present in a hash. The redis-py returns the keys and values as a Python dictionary. The Redis command HKEYS retrieves all the keys present in a hash. The redis-py returns the keys as a Python list.
Implementation of the Redis protocol. This abstract class provides a Python interface to all Redis commands and an implementation of the Redis protocol. Pipelines derive from this, implementing how the commands are sent and received to the Redis server.
Adding values to a Redis Hash and retrieving them using Python and Redis-Py: The HSET command adds a key-value pair to a hash. The HGET command retrieves the value for a specific key in a hash. The Redis command HGETALL retrieves all the keys and their values present in a hash. The Redis command HKEYS retrieves all the keys present in a hash.
To avoid blocking behavior the commands SCAN and HSCAN can be used which allow only a small number of elements to be retrieved on each call. The keys and values present in the Redis hash are: The Redis command HDEL removes a key-value pair identified by a specific key.
This is the intended behavior. By default, strings coming out of Redis don't get decoded. You have a couple options:
- Decode the data yourself.
- Create a client instance with the
decode_responses
argument, e.g.,StrictRedis(decode_responses=True)
. This will decode all strings that come from Redis based on thecharset
argument (which defaults to utf-8). Only do this is you're sure every response from Redis has string data that you want decoded to utf-8. If you're using the same client instance to get binary data such as a pickled object, you shouldn't use this option. In that case, I'd suggest using a separate client instance for the binary data.
Source: https://github.com/andymccurdy/redis-py/issues/463#issuecomment-41229918
POOL = redis.ConnectionPool(host='localhost', **decode_responses=True**, port=6379, db=0)
datastore = redis.StrictRedis(connection_pool=POOL)
if you use the ConnectionPool, you should move the decode_responses=True to the constructor of ConnectionPool.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With