Skip to main content

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


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-page-per-user-0.1.0.tar.gz (6.2 kB view hashes)

Uploaded Source

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