Caching query results in django Caching query results in django django django

Caching query results in django


Pop your cached query into Django's cache:

from django.core.cache import cachecache.set('key', queryset)

Then create a context processor to add the value of the cache to all templates:

# myproject/myapp/context_processors.pyfrom django.core.cache import cachedef cached_queries():    return {'cache', cache.get('key')}

Then add your context processor in your Django settings file:

TEMPLATE_CONTEXT_PROCESSORS += (    'myproject.myapp.context_processors.cached_queries')

Now you will be able to access the cache variable in all generic templates and all templates which have a requests context, which a template is given if this is done in the view:

return render_to_response('my_template.html',                          my_data_dictionary,                          context_instance=RequestContext(request))

When to Set the Cache

It depends on what is contained in the cache. However a common problem is that Django only really gets to execute Python whenever a page request is sent, and this is often not where you want to do this kind of work.

An alternative is to create a custom management command for a particular app. You can then either run this manually when necessary, or more commonly set this to run as a cron job.

To create a management command you must create a class decended from Command inside of a management/commands directory located inside of an app:

# myproject/myapp/management/commands/update_cache.pyfrom django.core.management.base import NoArgsCommandfrom django.core.cache import cacheclass Command(NoArgsCommand):    help = 'Refreshes my cache'    def handle_noargs(self, **options):        cache.set('key', queryset)

The name of this file is important as this will be the name of the command. In this case you can now call this on the command line:

python manage.py update_cache


You can also use johnny-cache for automatic caching of querysets. It will (by default) cache all querysets, but you can force it not to cache some.