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 details)

Uploaded Source

File details

Details for the file django-cache-page-per-user-0.1.0.tar.gz.

File metadata

  • Download URL: django-cache-page-per-user-0.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0.post20201221 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.8.0

File hashes

Hashes for django-cache-page-per-user-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5ebc59a5173b1dc83cb2dfd00282166eae621fc0453295f6faaf8e794382d321
MD5 96a099ac6840f1e1ddcb6205fa5a297e
BLAKE2b-256 d739df07731b40d1f42f7ad8c86c7dd45b7f735d4fb6422bb752e9fe0995482c

See more details on using hashes here.

Supported by

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