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",
}

}

wetler
  • 374
  • 2
  • 11
  • Do you have a redis instance running on each of these ports? – Vitor Diniz Jun 13 '21 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. – wetler Jun 13 '21 at 13:19
  • Well that should be possible, what kind of error do you get? – Vitor Diniz Jun 13 '21 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 – wetler Jun 13 '21 at 14:51

1 Answers1

4

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.

wetler
  • 374
  • 2
  • 11