how to use django-redis hset operation in redis cache

Viewed 1473

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)

2 Answers

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

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")
Related