Skip to main content

Lazy Django fragment cache sweeping

Project description

Fragment cache invalidation by using a per model version token to prefix the cache keys. The version token can either be an internal memcached counter or a timestamped attribute from the model, such as updated_at.

Installation

Install with pip or with python setup.py install and add ‘cachesweeper’ to your settings.INSTALLED_APPS

post_save cache sweeper

An example setup; an article has many comments, each comment is cached, a single vote should invalidate the comment’s specific cached fragment as well as the total article’s page.

Template fragment caching

{% cachesweeper %} takes a Django ORM model as its first argument, the expiry time as its second and any following arguments are used to construct the rest of the cache key

{% load markup %}
{% load cachesweeper_tags %}
{% cachesweeper comment 500 "comment.xml" %}
<p>
    <strong>{{comment.user}}</strong> said at {{comment.created_at}}:<br/>
    {{comment.content|markdown}}
    <br/>
    <a href={% url like article_id=article.pk,comment_id=comment.pk %}>Like ({{comment.likes.count}})</a>
    <a href={% url dislike article_id=article.pk,comment_id=comment.pk %}>Dislike ({{comment.dislikes.count}})</a>
</p>
{% endcachesweeper %}

Invalidating the fragment when the model changes

On a post_save invalidate the cache for the given model. There are two options, either have Memcached keep an internal version counter for each model or using the keyword using as a means of versioning the cache.

from cachesweeper.utils import invalidate_cache_for

# using Memcached's internal counter
def invalidate_vote_cache(sender, **kwargs):
    vote = kwargs.get('instance')
    invalidate_cache_for(vote.comment)

post_save.connect(invalidate_vote_cache, sender=Vote)


# using a model's attribute
def invalidate_article_cache(sender, **kwargs):
    article = kwargs.get('instance')
    invalidate_cache_for(article, using='updated_at')

post_save.connect(invalidate_article_cache, sender=Article)

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-cache-sweeper-0.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

django_cache_sweeper-0.1-py2.6.egg (13.3 kB view details)

Uploaded Source

File details

Details for the file django-cache-sweeper-0.1.tar.gz.

File metadata

File hashes

Hashes for django-cache-sweeper-0.1.tar.gz
Algorithm Hash digest
SHA256 66df0356fbbd287e51d3902bb3fdd80a470565356695a5d31968da02e4d8877a
MD5 151af4617ec36708c234b76302127e03
BLAKE2b-256 c8ea33394376c9ae52ca81f365eac042861413d3e5c29dd24b3a476ede4432e3

See more details on using hashes here.

File details

Details for the file django_cache_sweeper-0.1-py2.6.egg.

File metadata

File hashes

Hashes for django_cache_sweeper-0.1-py2.6.egg
Algorithm Hash digest
SHA256 b461a6a5783cdf501e879824c968ce81815f77b03886f493119a5721cde666a4
MD5 615068d3066c63cc497a72f104e31166
BLAKE2b-256 c4a19ac5b5becbbb492b7c7f9ec3bc7c2cdb6e8c4f018f9c1288ee235425923b

See more details on using hashes here.

Supported by

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