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.6 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 --group=development
uv run pre-commit install --install-hooks
uv run 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.4.0.tar.gz (27.2 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.4.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for drf_caching-1.4.0.tar.gz
Algorithm Hash digest
SHA256 87bbce99c1a1a505696bafffbd6f2b115e52f9762de7f381efab4ca800c854cb
MD5 b6fe2ad2ca9cae75bcf0f2cf85c3e80b
BLAKE2b-256 8e5220cee1f49ee44a4ec1a840ee7e4124338c827c10ea3437ae3a427823f05b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for drf_caching-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b82675e0c042ca54cfc5c6dec354894fedb69ddb4b8695fc16fd3191cd13cffd
MD5 213694e3f03e50e3d4a646e1055e8467
BLAKE2b-256 096e56c55587e3ebb07bf5176227b18cb2485d3776bd84cab1d6b9c9ffa71c62

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