Skip to main content

drf-caching

Handle views caching in Django Rest Framework.

This package uses uv for project management. To get started, ensure that uv is installed on your machine and updated to the 0.5.13 version. Detailed installation instructions for uv can be found here.

Installation

uv add drf-caching

Usage

To setup caching for a view, you can use the @cache_view decorator.

from drf_caching import cache_view, GetQuerysetKey, PaginationKey, QueryParamsKey

class MyView(APIView):
    @cache_view(
        GetQuerysetKey(),
        PaginationKey(),
        QueryParamsKey("ordering", "search"),
        timeout=60,
    )
    def get(self, request):
        return Response({"message": "Hello, world!"})

You can pass multiple keys to the decorator, and the cache key will be built using all of them. You can pass the following keyword arguments to the @cache_view decorator:

  • timeout: the cache timeout in seconds (can also be set on a global level using the DRF_CACHING setting)

The following keys, available in the drf_caching.keys module, can be used:

  • GetObjectKey: the cache key will be built using the view's get_object method
  • GetQuerylistKey: the cache key will be built using the view's get_querylist method from django-rest-multiple-models
  • GetQuerysetKey: the cache key will be built using the view's get_queryset method
  • LookupFieldKey: the cache key will be built using the view's kwarg matching the lookup field
  • RequestDataKey: the cache key will be built using the request's data
  • RequestHeadersKey: the cache key will be built using the request's headers
  • RequestKwargsKey: the cache key will be built using the request's kwargs
  • RequestPaginationKey: the cache key will be built using the request's pagination parameters
  • RequestQueryParamsKey: the cache key will be built using the request's query parameters
  • RequestUserKey: the cache key will be built using the request's user

If no keys are passed, the cache key will be built using the view name and the request's format.

The settings can be customized as such:

DRF_CACHING = {
    "CACHE": "default",
    "HEADERS": ["age", "x-cache"],
    "TIMEOUT": 60,
}

To disable caching for a specific view, or even globally, you can set timeout or TIMEOUT to 0. To make it so that cache keys never expire for a specific view, or even globally, you can set timeout or TIMEOUT to None.

The following settings are available:

  • CACHE: the cache to use (defaults to default)
  • HEADERS: a list of lowercase headers to include in the cache key (by default the following headers are included: age, cache-control, etag, expires, x-cache)
  • TIMEOUT: the default cache timeout in seconds

To create your own cache key, you can subclass the BaseKey and BaseKeyWithFields classes and implement the _get_data method.

from drf_caching import BaseKey, BaseKeyWithFields

class CustomKey(BaseKey):
    def _get_data(self, view_instance, view_method, request, *args, **kwargs):
        return {
            "key": "value"
        }

class CustomKeyWithFields(BaseKeyWithFields):
    def _get_data(self, view_instance, view_method, request, *args, **kwargs):
        return {
            field: ...
            for field in self.fields
        }

Development

uv sync --frozen --group=development
uv run --frozen pre-commit install --install-hooks
uv run --frozen pre-commit install --hook-type=commit-msg

Acknowledgments

This project was strongly inspired by drf-extensions.

Contributing

Contributions are welcome! To get started, please refer to our contribution guidelines.

Issues

If you encounter any problems while using this package, please open a new issue here.

Metadata

Release files for drf-caching 1.6.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 drf-caching 1.6.0
File Size Uploaded Provenance Actions
drf_caching-1.6.0.tar.gz 24.0 kB Without provenance Details for drf_caching-1.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for drf-caching 1.6.0
File Interpreter ABI Platform Actions
drf_caching-1.6.0-py3-none-any.whl Python 3 none any Details for drf_caching-1.6.0-py3-none-any.whl

Total release size: 36.2 kB

Release files / drf_caching-1.6.0.tar.gz

Download URL drf_caching-1.6.0.tar.gz
Size 24.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b4fb8f2428a979e30b43450ab170ef47898b70106170c732b7adafced2c166bc
BLAKE2b-256 checksum
How to use checksums
884c31e88f0aafd67196025be6d5bf447747269608fcf9c01c24fa169c832947
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.5.13

Release files / drf_caching-1.6.0-py3-none-any.whl

Download URL drf_caching-1.6.0-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c3010a67bc57243a993a7565a71fb21b665534c489d0041aaaff611236c566ed
BLAKE2b-256 checksum
How to use checksums
5286240c5b134160051decb76451de8ce5af5a41233bb82343ade6c171c8411f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.5.13

Security for drf-caching 1.6.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

1.6.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