简体   繁体   English

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

[英]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). 我遇到一种情况,当使用.hgetall(key)进行请求时,redis缓存的db2中一个明显可知的哈希值消失了。 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中的内容:

$ 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)". 查看您的Python堆栈跟踪:“返回self.execute_command('GET',name)”失败。 It means that: 这意味着:

  • the hgetall command failed (probably because the connection was not established before) hgetall命令失败(可能是因为之前未建立连接)
  • an exception was raised and caught in your method 引发异常并陷入您的方法中
  • the Redis connection is established (I suppose by calling connect_to_cache()) Redis连接已建立(我想通过调用connect_to_cache())
  • then you try to run "self.redis.get(cachekey)" 然后您尝试运行“ 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) 它当然会失败,因为cachekey的内容是哈希的键(而不是字符串)(在这里我想应该使用hgetall代替)
  • an other exception is raised - the Redis error is a type error (Operation against a key holding the wrong kind of value) 引发另一个异常-Redis错误是类型错误(针对具有错误类型值的键进行的操作)

With redis-cli, try to run "GET fax:1112223333", you will have the same error. 使用redis-cli,尝试运行“ GET Fax:1112223333”,您将遇到相同的错误。

There are different types of data when you set and hset. 设置和设置时有不同类型的数据。 In hset your value is hash map. 在hset中,您的值是哈希映射。 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 只需尝试hgetall和redis将返回哈希图,或尝试使用带有cachekey和已设置哈希图的任何键的hget并享受

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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