Dynamically attaching SITE_ID to Django Caching

By : Anoop Thomas Mathew

It would be useful and convenient, if you have an automatic way to add the SITE_ID, especially, when you have multiple sites running on the same deployment. Django provides a cache prefix function KEY_FUNCTION in settings which can be used to achieve this.

Just follow the following steps, and your cache, automatically prepends SITE_ID to the cache key, making it unique across multiple sites.

  1. Put the following into the settings file.

         CACHES = {
         'default': {
          'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
           'LOCATION': 'cache_table',
           KEY_FUNCTION = projectname.appname.modulename.functionname,
  2. Write a function to get current site id, say, get_current_site(), which returns current ...

more info..
© Agiliq, 2009-2012