python redis客户端无法使用.hgetall(key)获取现有哈希值

时间:2013-05-21 19:07:35

标签: python hash redis

我遇到的情况是,当我们使用.hgetall(key)请求时,我们的redis缓存的db2中已知的已知散列会死亡。我希望有一些见解!谢谢。

是的,所以......首先,一小段代码:

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)

导致:

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

以下是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) ""

2 个答案:

答案 0 :(得分:1)

查看你的Python堆栈跟踪:它失败了“return self.execute_command('GET',name)”。这意味着:

  • hgetall命令失败(可能是因为之前未建立连接)
  • 引发了一个异常,并在您的方法中被捕获
  • 建立Redis连接(我想通过调用connect_to_cache())
  • 然后你尝试运行“self.redis.get(cachekey)”
  • 当然失败了因为cachekey的内容是哈希的关键(不是字符串) (我想在这里你应该使用hgetall)
  • 引发了另一个异常 - Redis错误是一个类型错误(对一个持有错误值的键的操作)

使用redis-cli,尝试运行“GET fax:1112223333”,您将遇到同样的错误。

答案 1 :(得分:1)

设置和设置时有不同类型的数据。在hset中,您的值是哈希映射。当你尝试获取哈希映射时,你有相同的错误。只需尝试hgetall,redis将返回hashmap,或者尝试使用cachekey和任何设置的hashmap键进行hget并享受