这是我的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/