Skip to main content

Drop-in replacement for Django's template fragment caching. Provides automatic cache invalidation.

Project description

Django Ultracache

Drop-in replacement for Django’s template fragment caching. Provides automatic Django cache invalidation and reverse caching proxy purging.

Travis

Installation

  1. Install or add django-ultracache to your Python path.

  2. Add ultracache to your INSTALLED_APPS setting.

  3. Ensure django.core.context_processors.request is in TEMPLATE_CONTEXT_PROCESSORS setting.

Usage

django-ultracache provides a template tag {% ultracache %} that functions like Django’s standard cache template tag, with these exceptions.

  1. It takes the sites framework into consideration, allowing different caching per site.

  2. It allows undefined variables to be passed as arguments, thus simplifying the template.

  3. Crucially, it is aware of model objects that are subjected to its caching. When an object is modified all affected cache key are automatically expired. This allows the user to set longer expiry times without having to worry about stale content.

  4. The cache invalidation can be extended to issue purge commands to Varnish, Nginx or other reverse caching proxies.

Simplest use case:

{% load ultracache_tags %}
{% ultracache 3600 'my_identifier' object 123 undefined 'string' %}
    {{ object.title }}
{% endultracache %}

The tag can be nested. ultracache is aware of all model objects that are subjected to its caching. In this example cache keys outer and inner_one are expired when object one is changed but cache key inner_two remains unaffected:

{% load ultracache_tags %}
{% ultracache 1200 'outer' %}
    {% ultracache 1200 'inner_one' %}
        title = {{ one.title }}
    {% endultracache %}
    {% ultracache 1200 'inner_two' %}
        title = {{ two.title }}
    {% endultracache %}
{% endultracache %}

django-ultracache also provides a decorator cached_get to cache your views. The parameters follow the same rules as the ultracache template tag except they must all resolve. request.get_full_path() is always implicitly added to the cache key:

from ultracache.decorators import cached_get


class CachedView(TemplateView):
    template_name = "ultracache/cached_view.html"

    @cached_get(300, "request.is_secure()", 456)
    def get(self, *args, **kwargs):
        return super(CachedView, self).get(*args, **kwargs)

The cached_get decorator can be used in an URL pattern:

from ultracache.decorators import cached_get

url(
    r"^cached-view/$",
    cached_get(3600)(TemplateView.as_view(
        template_name="myproduct/template.html"
    )),
    name="cached-view"
)

Do not indiscriminately use the cached_get decorator. It only ever operates on GET requests but cannot know if the code being wrapped retrieves data from eg. the session. In such a case it will cache things it is not supposed to cache.

You can create custom reverse caching proxy purgers. See purgers.py for examples:

ULTRACACHE = {
    'purge': {'method': 'myproduct.purgers.squid'}
}

Automatic invalidation defaults to true. To disable automatic invalidation set:

ULTRACACHE = {
    'invalidate': False
}

How does it work?

django-ultracache monkey patches django.template.base.Variable._resolve_lookup to make a record of model objects as they are resolved. The ultracache template tag inspects the list of objects contained within it and keeps a registry in Django’s caching backend. A post_save signal handler monitors objects for changes and expires the appropriate cache keys.

Tips

  1. If you arre running a cluster of Django nodes then ensure that they use a shared caching backend.

  2. Expose objects in your templates. Instead of passing object_title to a template rather have the template dereference object.title.

Authors

Praekelt Consulting

  • Hedley Roos

Changelog

0.2

  1. Do not automatically add request.get_full_path() if any of request.get_full_path(), request.path or request.path_info is an argument for cached_get.

0.1.6

  1. Also cache response headers.

0.1.5

  1. Explicitly check for GET and HEAD request method and cache only those requests.

0.1.4

  1. Rewrite decorator to be function based instead of class based so it is easier to use in urls.py.

0.1.3

  1. cached_get decorator now does not cache if request contains messages.

0.1.2

  1. Fix HTTPResponse caching bug.

0.1.1

  1. Handle case where a view returns an HTTPResponse object.

0.1

  1. Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-ultracache-0.2.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_ultracache-0.2-py2.7.egg (30.1 kB view details)

Uploaded Egg

File details

Details for the file django-ultracache-0.2.tar.gz.

File metadata

File hashes

Hashes for django-ultracache-0.2.tar.gz
Algorithm Hash digest
SHA256 c6d280542aa6f7d4b9274b15aef985b2e7780540195e04bf6b71632d1d760258
MD5 ec734d8b59998c8d1bbb88e214b2746a
BLAKE2b-256 fd37843a5e869357667fe711f5b5232bba074db820bc0d1e84a0abde81725648

See more details on using hashes here.

File details

Details for the file django_ultracache-0.2-py2.7.egg.

File metadata

File hashes

Hashes for django_ultracache-0.2-py2.7.egg
Algorithm Hash digest
SHA256 c79b382591c024bfdf8a57629eda340f474a6bbe4dd61514af01f0c3616085bc
MD5 681d107fcb26ea56fc9ed49c10267e6f
BLAKE2b-256 4b7a1b66a31b9aaf1abbf24bba9fd874dea65a02b64d16d6a62787a77ae0c2e9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page