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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-cache-sweeper-0.1.1.tar.gz
.
File metadata
- Download URL: django-cache-sweeper-0.1.1.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee2c52abfee9691c695db8a3661ef98432fe8630ea93e86ac742ea65d94c0703 |
|
MD5 | 27e50674c323dc908ba0777627f06223 |
|
BLAKE2b-256 | f719946feacb3f757ea7e62f3c4d487054d8e94556e685536d58583d793fc589 |
Provenance
File details
Details for the file django_cache_sweeper-0.1.1-py2.6.egg
.
File metadata
- Download URL: django_cache_sweeper-0.1.1-py2.6.egg
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9df563932ce58cafe3a6bc9bc8975ced07198ccfea43430c14d2701e5482f8ea |
|
MD5 | bbadf2abb7f03013b579cb5d0acebf79 |
|
BLAKE2b-256 | 10b1200c86fe409b58a7a3d9887060c2854b1da4ae10f20176b094b1726527a3 |