简体   繁体   中英

python redis client fails to get existing hash values using .hgetall(key)

I'm encountering a circumstance where a demonstrably known hash in db2 of our redis cache dies when being requested with .hgetall(key). I'm hoping for some insight! Thank you.

Right, so... first, a sliver of code:

def from_cache(self, cachekey):
    """ pull oft needed material from our persistent redis memory cache, ensuring of course that we have a connection """

    try:
        log.debug('trying to get \'%s\' from cache' % cachekey)
        return self.redis.hgetall(cachekey)
    except Exception, e:
        self.connect_to_cache()
        return self.redis.get(cachekey)

resulting in:

2013-05-21 14:45:26,035 23202 DEBUG trying to get 'fax:1112223333' from cache
2013-05-21 14:45:26,036 23202 DEBUG initializing connection to redis/cache memory localhost, port 6379, db 2...
2013-05-21 14:45:26,039 23202 ERROR stopping with an exception
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/simpledaemon/base.py", line 165, in start
    self.run()
  File "newgov.py", line 51, in run
    if self.ready_for_queue(fax):
  File "newgov.py", line 61, in ready_for_queue
    if self.too_many_already_queued(fax):
  File "newgov.py", line 116, in too_many_already_queued
    rules = self.from_cache(key)
  File "newgov.py", line 142, in from_cache
    return self.redis.get(cachekey)
  File "/usr/lib/python2.6/site-packages/redis/client.py", line 588, in get
    return self.execute_command('GET', name)
  File "/usr/lib/python2.6/site-packages/redis/client.py", line 378, in execute_command
    return self.parse_response(connection, command_name, **options)
  File "/usr/lib/python2.6/site-packages/redis/client.py", line 388, in parse_response
    response = connection.read_response()
  File "/usr/lib/python2.6/site-packages/redis/connection.py", line 309, in read_response
    raise response
ResponseError: Operation against a key holding the wrong kind of value

And here is what is in redis:

$ redis-cli
redis 127.0.0.1:6379> SELECT 2
OK
redis 127.0.0.1:6379[2]> type fax:1112223333
hash
redis 127.0.0.1:6379[2]> hgetall fax:1112223333
1) "delay"
2) "0"
3) "concurrent"
4) "20"
5) "queued"
6) "20"
7) "exclude"
8) ""

Look at your Python stack trace: it fails on "return self.execute_command('GET', name)". It means that:

  • the hgetall command failed (probably because the connection was not established before)
  • an exception was raised and caught in your method
  • the Redis connection is established (I suppose by calling connect_to_cache())
  • then you try to run "self.redis.get(cachekey)"
  • it fails of course because the content of cachekey is the key of a hash (not a string) (here I imagine you should use hgetall instead)
  • an other exception is raised - the Redis error is a type error (Operation against a key holding the wrong kind of value)

With redis-cli, try to run "GET fax:1112223333", you will have the same error.

There are different types of data when you set and hset. In hset your value is hash map. And when you try get hash map you have same Error. Just try hgetall and redis will return hashmap, or try hget with cachekey and any key of setted hashmap and enjoy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM