Skip to main content

Django Smart Cache

A Django utility for caching successful GET responses with configurable timeouts and optional model-based cache invalidation.

Features

  • Cache successful, non-streaming GET responses.
  • Configure the default cache timeout.
  • Generate cache keys from request URLs.
  • Separate cached responses by authenticated user by default.
  • Bypass caching for anonymous users by default, with an option for shared public caching.
  • Associate cached responses with Django models for cache invalidation.
  • Automatically invalidate registered model cache entries when a model instance is saved or deleted.

Requirements

  • Python 3.10 or later
  • Django 4.2 or later

Installation

Install the package:

pip install django-smart-cache

Add the application configuration to INSTALLED_APPS in your Django settings:

INSTALLED_APPS = [
    # Existing applications
    "django_smart_cache.apps.SmartCacheConfig",
]

Configuration

The default cache timeout is 300 seconds.

To change it, add the following setting to your Django project's settings file:

SMART_CACHE_TIMEOUT = 600

The value must be a positive integer.

Django Smart Cache uses Django's default cache backend. Configure the CACHES setting in your Django project as appropriate for your application.

Usage

Basic caching

Import the decorator and apply it to a view:

from django_smart_cache.decorators import smart_cache


@smart_cache(timeout=60)
def article_list(request):
    ...

This caches successful, non-streaming HTTP 200 GET responses for 60 seconds.

If timeout is omitted, the value of SMART_CACHE_TIMEOUT is used.

Separate cache entries by user

By default, vary_on_user=True. Authenticated users receive separate cache entries based on their user ID, while anonymous requests bypass the cache.

@smart_cache(timeout=60)
def my_articles(request):
    ...

For public views that return identical content to all users, set vary_on_user=False to enable shared caching, including for anonymous users:

@smart_cache(timeout=60, vary_on_user=False)
def public_articles(request):
    ...

Only use shared caching when the response does not contain user-specific or sensitive content.

Invalidate cache entries when a model changes

Pass a Django model class using the model argument:

from .models import Article
from django_smart_cache.decorators import smart_cache


@smart_cache(timeout=60, model=Article)
def article_list(request):
    ...

When a response is cached, its cache key is registered for the specified model.

When an instance of that model is saved or deleted, the registered cache entries for that model are invalidated.

Cache Behavior

  • Only HTTP 200 responses are cached.
  • Only non-streaming GET responses are stored.
  • HEAD requests bypass

Metadata

Release files for django-smart-cache-313 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-smart-cache-313 0.1.0
File Size Uploaded Provenance Actions
django_smart_cache_313-0.1.0.tar.gz 6.2 kB Without provenance Details for django_smart_cache_313-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-smart-cache-313 0.1.0
File Interpreter ABI Platform Actions
django_smart_cache_313-0.1.0-py3-none-any.whl Python 3 none any Details for django_smart_cache_313-0.1.0-py3-none-any.whl

Total release size: 11.7 kB

Release files / django_smart_cache_313-0.1.0.tar.gz

Download URL django_smart_cache_313-0.1.0.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3a4b14971b62dd13ad233c59d2b02ba9c7f3fa6920de8656374d7369abf060ba
BLAKE2b-256 checksum
How to use checksums
31cc7248399ed5c6d3f3c45188763fe144d5f9f8e1422d96270659c90545a282
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.2

Release files / django_smart_cache_313-0.1.0-py3-none-any.whl

Download URL django_smart_cache_313-0.1.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3fef9e3fbd6370b6d3575d4b096590a7e8306c169a52e600f441897556c1593c
BLAKE2b-256 checksum
How to use checksums
1d98ca91ce329e8254c0c9daf58ac52c5b88d659c707d9ed813c3b8b62225cd3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.2

Security for django-smart-cache-313 0.1.0

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page