python - 我如何使用python-redis检查redis数据库中是否存在值

标签 python redis

这是我的python代码:

def check(request):
    import redis
    R = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
    R.set("name","srk")
    r = R.HSET("name","srk")
    print(r)

遇到错误并显示此错误消息:

Internal Server Error: /get_user/
Traceback (most recent call last):
  File "/home/soubhagya/.local/share/virtualenvs/pipenv-r-zifbiy/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/soubhagya/.local/share/virtualenvs/pipenv-r-zifbiy/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/soubhagya/.local/share/virtualenvs/pipenv-r-zifbiy/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/soubhagya/Desktop/Dev/rango/backend/access/views.py", line 103, in get_user
    r = R.HSET("name","srk")
AttributeError: 'StrictRedis' object has no attribute 'HSET'

在 py-redis 中,我如何检查一个值是否存在于 redis 数据库中。 原始查询会有所帮助。 请查看我的代码。

最佳答案

StrictRedis 没有HSET 函数,但是它有hset 函数来设置Redis Hashes 中的字段。这就是您收到错误的原因。

要使用 redis-py 检查 Redis 中是否存在键,请使用 r = R.exists("name")

关于python - 我如何使用python-redis检查redis数据库中是否存在值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53251047/

相关文章:

python - 在上部子图上显示 xtick 标签

django - 在 Redis 上设置 requirepass 会抛出 'NOAUTH Authentication required'

redis - 如何使用 redis-cli --latency 命令设置样本大小?

lua - 在redis中加载lua脚本文件

amazon-web-services - 无法使用 redis-cli 连接到 AWS Redis 端点。连接超时

node.js - 多个 NodeJS 服务器的多个 redis 实例

python - 导入 scrapy.conf.settings 会生成错误

python - starpy、asterisk 接口(interface)库的备用配置文件

python - 使用 Pandas 合并数据框

Python OpenCV - 网络摄像头捕获返回空白屏幕