Skip to main content

Handle views caching in Django Rest Framework.

Project description

drf-caching

Handle views caching in Django Rest Framework.

Installation

pip install 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 to 0.

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
        }

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.3.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

drf_caching-1.3.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: drf_caching-1.3.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.7

File hashes

Hashes for drf_caching-1.3.0.tar.gz
Algorithm Hash digest
SHA256 63d19afb18c3edaa25b049015d87128852635369969d8779d3d0288192b1072c
MD5 4e6b96316fdd9336076a9415fa54ad58
BLAKE2b-256 612a88c18866af9647899b95819c3c235e2514d6a37040d43300b19fd78f757d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: drf_caching-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.7

File hashes

Hashes for drf_caching-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c8dae12f58be5c8a00a202547b186635784ec98beb17c190259757620bb425b6
MD5 a23e5490a182fae917c7ee9380ab1b73
BLAKE2b-256 5e509a84edef42ef165940f3935ec648b5faf97fbcdb38c4a7ed44bdcbec3926

See more details on using hashes here.

Supported by

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