decorator for view caching per user and per language
Project description
django-cache-page-per-user
Extending the Django's cache_page
decorator.
Supports caching per user and per language.
Support custom settings and clearing cache.
Support of caches
- django_redis.cache.RedisCache
- django.core.cache.backends.locmem.LocMemCache
Installation
Run pip install django-cache-page-per-user
Usage
Use prefixes to group views by the common models they depend on.
# constants.py
class CachePrefixes:
AUTHOR = 'AUTHOR'
ARTICLE = 'ARTICLE'
COMMENT = 'COMMENT'
Use @cache_page_per_user like @cache_page, this decorator will cache responses with only safe methods and with status 200
# view.py
from cache_page_per_user.cache import cache_page_per_user
from django.utils.decorators import method_decorator
from constants import CachePrefixes
@cache_page_per_user(60*60, CachePrefixes.ARTICLE)
def article_view(request, pk):
...
@cache_page_per_user(60*60, CachePrefixes.ARTICLE)
def article_some_info_view(request, pk):
...
@cache_page_per_user(60*60, CachePrefixes.AUTHOR)
def author_view(request, pk):
...
# for ViewSet
class CommentViewSet(ViewSet):
...
@method_decorator(cache_page_per_user(60*60, CachePrefixes.COMMENT))
def retrieve(self, request, pk=None):
...
Create signals on the model, when changed, the cache will be reset
# signals.py
...
from cache_page_per_user.utils import clear_cache
from cache_page_per_user.utils import get_cache_key
from constants import CachePrefixes
from models.py import Article
...
@receiver(post_save, sender=Article)
@receiver(post_delete, sender=Article)
def create_slug_wrapper(sender, instance, **kwargs):
query = get_cache_key(CachePrefixes.ARTICLE, instance.author_id)
clear_cache(query)
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
Close
Hashes for django-cache-page-per-user-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ebc59a5173b1dc83cb2dfd00282166eae621fc0453295f6faaf8e794382d321 |
|
MD5 | 96a099ac6840f1e1ddcb6205fa5a297e |
|
BLAKE2b-256 | d739df07731b40d1f42f7ad8c86c7dd45b7f735d4fb6422bb752e9fe0995482c |