Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6595393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:56:04+00:00 2026-05-25T17:56:04+00:00

I have a Django app set up to use multiple caches (I hope). Is

  • 0

I have a Django app set up to use multiple caches (I hope). Is there a way to set the session to use a specific cache, or is it stuck on ‘default’?

Here’s what i have now:

CACHES = {
    'default': {
        'BACKEND': 'util.backends.SmartMemcachedCache',
        'LOCATION': '127.0.0.1:11211',
        'TIMEOUT': 300,
        'ANONYMOUS_ONLY': True
    },
    'some_other_cache': {
        'BACKEND': 'util.backends.SmartMemcachedCache',
        'LOCATION': '127.0.0.1:11211',
        'TIMEOUT': 300,
        'ANONYMOUS_ONLY': True,
        'PREFIX': 'something'
    },

}

SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:56:05+00:00Added an answer on May 25, 2026 at 5:56 pm

    The cached_db and cache backends don’t support it, but it’s easy to create your own:

    from django.contrib.sessions.backends.cache import SessionStore as CachedSessionStore
    from django.core.cache import get_cache
    from django.conf import settings
    
    class SessionStore(CachedSessionStore):
        """
        A cache-based session store.
        """
        def __init__(self, session_key=None):
            self._cache = get_cache(settings.SESSION_CACHE_ALIAS)
            super(SessionStore, self).__init__(session_key)
    

    No need for a cached_db backend since Redis is persistent anyway 🙂


    When using Memcached and cached_db, its a bit more complex because of how that SessionStore is implemented. We just replace it completely:

    from django.conf import settings
    from django.contrib.sessions.backends.db import SessionStore as DBStore
    from django.core.cache import get_cache
    
    class SessionStore(DBStore):
        """
        Implements cached, database backed sessions.  Now with control over the cache!
        """
    
        def __init__(self, session_key=None):
            super(SessionStore, self).__init__(session_key)
            self.cache = get_cache(getattr(settings, 'SESSION_CACHE_ALIAS', 'default'))
    
        def load(self):
            data = self.cache.get(self.session_key, None)
            if data is None:
                data = super(SessionStore, self).load()
                self.cache.set(self.session_key, data, settings.SESSION_COOKIE_AGE)
            return data
    
        def exists(self, session_key):
            return super(SessionStore, self).exists(session_key)
    
        def save(self, must_create=False):
            super(SessionStore, self).save(must_create)
            self.cache.set(self.session_key, self._session, settings.SESSION_COOKIE_AGE)
    
        def delete(self, session_key=None):
            super(SessionStore, self).delete(session_key)
            self.cache.delete(session_key or self.session_key)
    
        def flush(self):
            """
            Removes the current session data from the database and regenerates the
            key.
            """
            self.clear()
            self.delete(self.session_key)
            self.create()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty standard django app, and am wondering how to set the
I have a Django app that use a django-tagging. I need to port this
I have written a Django app that makes use of Python threading to create
Is it possible to set DEBUG=False for only a specific app in Django? Celery
I have an app that makes use of 'django-registration' a custom built model named
I have a Django app that gets it's data completely from an external source
I have a Django app written in Python 2.5 and I plan to upgrade
I have a Django app where most of the search is driven by foreign
I have a django app with models as follows: A Question model An Answer
This is probably a setting error somewhere. I have a django app that works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.