'how to use django-redis hset operation in redis cache
I am using django 3.0.4 and python 3.6.9. I have to use hset operation to set some values in redis cache.
- My try:
from django.core.cache import caches
cache.set(), cache.get() // these operation are working
But I am not able to use hset and hget operation using this library. There is no proper documentation about this in Django official docs.
Note: I have referred this (Not A copy)
Solution 1:[1]
This is how I resolved the issue:
- pip install django-redis-cache (3rd party redis client)
Settings.py:
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "127.0.0.1:6379/1",
"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient",},
}
}
In views.py ::
from django.core.cache import caches
redis_cache=caches['default']
redis_client=redis_cache.client.get_client()
redis_client.hset('myhash','key1', 'value1')
Hope this will help. Docs: Django-redis-cache
Solution 2:[2]
Hey @Sanu Your importing line is wrong Please import cache not caches. I am surprised how are you running with "caches".
from django.core.cache import cache
cache.set("Your key", "Your dict data")
cache.get("Your key")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | skysoft999 |
Solution 2 | Vaibhav Mishra |