Skip to main content

Handle views caching in Django Rest Framework.

Project description

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.

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

drf_caching-1.6.0.tar.gz (24.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_caching-1.6.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file drf_caching-1.6.0.tar.gz.

File metadata

  • Download URL: drf_caching-1.6.0.tar.gz
  • Upload date:
  • Size: 24.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.13

File hashes

Hashes for drf_caching-1.6.0.tar.gz
Algorithm Hash digest
SHA256 b4fb8f2428a979e30b43450ab170ef47898b70106170c732b7adafced2c166bc
MD5 8ef65e269d0fdb48840f651151d1a73d
BLAKE2b-256 884c31e88f0aafd67196025be6d5bf447747269608fcf9c01c24fa169c832947

See more details on using hashes here.

File details

Details for the file drf_caching-1.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_caching-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c3010a67bc57243a993a7565a71fb21b665534c489d0041aaaff611236c566ed
MD5 7af5cd7a8b129faa511390e86bb6efa8
BLAKE2b-256 5286240c5b134160051decb76451de8ce5af5a41233bb82343ade6c171c8411f

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