2

I have a Django project and i am using django-redis where I want to implement different types of caching,

  1. Caching search query
  2. Caching static pages
  3. Cache user Data (eg: online status)

I can add different prefix for different kind of caching, but I want to use different redis server for all the different caching I have. I couldn't find anything on the docs how to do this

My current settings

CACHES = {
"default": {
    "BACKEND": "django_redis.cache.RedisCache",
    "LOCATION": "redis://localhost:6379/1",
    "OPTIONS": {
        "CLIENT_CLASS": "django_redis.client.DefaultClient",
        "PARSER_CLASS": "redis.connection.HiredisParser",
        "IGNORE_EXCEPTIONS": True,
    },
    "KEY_PREFIX": "db_cache",
}

}

What I would want

CACHES = {
"default": {
    "BACKEND": "django_redis.cache.RedisCache",
    "LOCATION": "redis://localhost:6379/",
    "OPTIONS": {
        "CLIENT_CLASS": "django_redis.client.DefaultClient",
        "PARSER_CLASS": "redis.connection.HiredisParser",
    },
    "KEY_PREFIX": "db_cache",
},
'static_page': {
    "BACKEND": "django_redis.cache.RedisCache",
    "LOCATION": "redis://localhost:6378/",
    "OPTIONS": {
        "CLIENT_CLASS": "django_redis.client.DefaultClient",
        "PARSER_CLASS": "redis.connection.HiredisParser",
        "IGNORE_EXCEPTIONS": True,
    },
    "KEY_PREFIX": "db_cache",
},
'user_data': {
    "BACKEND": "django_redis.cache.RedisCache",
    "LOCATION": "redis://localhost:6377/",
    "OPTIONS": {
        "CLIENT_CLASS": "django_redis.client.DefaultClient",
        "PARSER_CLASS": "redis.connection.HiredisParser",
    },
    "KEY_PREFIX": "db_cache",
}

}

4
  • Do you have a redis instance running on each of these ports? Commented Jun 13, 2021 at 13:09
  • Yes i have these redis servers running, while the question has nothing to do with redis server it can be a Local Memory Cache. or dummy cache, it should work for everything. Commented Jun 13, 2021 at 13:19
  • Well that should be possible, what kind of error do you get? Commented Jun 13, 2021 at 13:21
  • i dont know where to start currently i am using cache.set() and cache.get() but it is saving in the default database how do i specify database Commented Jun 13, 2021 at 14:51

1 Answer 1

5

Well I found the answer while looking for something else

Instead of using

from django.core.cache import cache
cache.set('hello', 'bye')
cache.get('hello')

which stores the data in the default caching Use something like this

from django.core.cache import caches
c = caches['static_page']
c.set('hello', 'bye')
c.get('hello')

It is such a small thing that most of the document don't mention it separately, and you might miss it when going through the documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.