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 SITE_ID.

  3. Add a function like below, as functionname at the same path as specified in KEY_FUNCTION.

    from django.utils.encoding import smart_str
    
    def prefix_site_id(key, key_prefix, version):
    
       site_id = get_current_site()
    
       return ':'.join([str(site_id), str(version), smart_str(key)])
    

That’s it. You have successfully added an automatic, dynamic, function based cache prefix for django.

Related Posts


Can we help you build amazing apps? Contact us today.

© Agiliq, 2009-2012