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

标签 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) ""

最佳答案

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

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

使用redis-cli,尝试运行“GET fax:1112223333”,你会得到同样的错误。

关于python redis 客户端无法使用.hgetall(key) 获取现有哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16677641/

相关文章:

python - Boost Python,Visual Studio 链接到错误的 boost dll

Python win32com - 安装了 32 位和 64 位版本的应用程序

python - 我收到以下错误需要超过 1 个值才能解包

java - 哈希如何推送新内容和更新当前内容

java - 如何在 Java 中将 md5 哈希转换为整数类型?

python - 为什么我需要 "sys.argv"才能在 PyQt 中启动 QApplication?

delphi - 在 Delphi 中获取 PDF MD5 哈希值

php - 寻找适合我们案例的 NoSQL 数据库

laravel-echo-server 用户未加入,未订阅套接字服务器

java - 如何捕获 redis.serializer.SerializationException