Django Fragment Cache Invalidation
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)
Metadata
Release files for django-cache-sweeper 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-cache-sweeper-0.1.2.tar.gz | 6.2 kB | Without provenance | Details for django-cache-sweeper-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_cache_sweeper-0.1.2-py2.6.egg | Legacy Egg format | - | - | Details for django_cache_sweeper-0.1.2-py2.6.egg |
Total release size: 21.0 kB
Release files / django-cache-sweeper-0.1.2.tar.gz
| Download URL | django-cache-sweeper-0.1.2.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f2c89a3ffce1cb92e317c24d65fabff2ce00e7ee44b5cffc9e5fc8d07ec4d2f6
|
|
BLAKE2b-256 checksum How to use checksums |
716ff570d5c1b4069e8b07873810a058a26bb4d768433b6e86d6c1340691978e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_cache_sweeper-0.1.2-py2.6.egg
| Download URL | django_cache_sweeper-0.1.2-py2.6.egg |
|---|---|
| Size | 14.8 kB |
| Tags | Egg |
|
SHA-256 checksum How to use checksums |
12e03eb3a666aff8f3b707b3f292a6d0d72f46dd4cc7a4a821ceb6b7c4a53cbb
|
|
BLAKE2b-256 checksum How to use checksums |
98152b75cfb8b17281e269cdc7601113b6a03a814add3b681dd2119dc8b93db5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django-cache-sweeper 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.