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 theDRF_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 methodGetQuerylistKey
: the cache key will be built using the view's get_querylist method from django-rest-multiple-modelsGetQuerysetKey
: the cache key will be built using the view's get_queryset methodLookupFieldKey
: the cache key will be built using the view's kwarg matching the lookup fieldRequestDataKey
: the cache key will be built using the request's dataRequestHeadersKey
: the cache key will be built using the request's headersRequestKwargsKey
: the cache key will be built using the request's kwargsRequestPaginationKey
: the cache key will be built using the request's pagination parametersRequestQueryParamsKey
: the cache key will be built using the request's query parametersRequestUserKey
: 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 todefault
)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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file drf_caching-1.2.0.tar.gz
.
File metadata
- Download URL: drf_caching-1.2.0.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 162d89b9272524b8977d1bbcd357ef860249d201e1856a022506b825e61c6025 |
|
MD5 | 18241bbf24b3e7a4b68a18b7ee3980a2 |
|
BLAKE2b-256 | 258297ebeaefaedf7a99845d796af5ed78325de0ba95b455faa69324058ee352 |
File details
Details for the file drf_caching-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: drf_caching-1.2.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 551c346e9bec6508149be4b7cd0ae5638cd8e0710790e21fb4da1dfe272f8a91 |
|
MD5 | 3eeba3e98fef06ff314e6510d0f6b96a |
|
BLAKE2b-256 | c3d27218852d9f6e89d0dc89268ae00b4bdc067ea61646b5fdd535b2f5a9b241 |