drf-caching


Namedrf-caching JSON
Version 1.2.0 PyPI version JSON
download
home_pageNone
SummaryHandle views caching in Django Rest Framework.
upload_time2024-10-20 16:09:04
maintainerNone
docs_urlNone
authorNone
requires_python>=3.12
licenseNone
keywords
VCS
bugtrack_url
requirements annotated-types asgiref beautifulsoup4 cfgv distlib django django-redis djangorestframework filelock identify lxml nodeenv platformdirs pre-commit pydantic pydantic-core pyyaml redis ruff soupsieve sqlparse typing-extensions virtualenv
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # drf-caching

Handle views caching in Django Rest Framework.

## Installation

```bash
pip install drf-caching
```

## Usage

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

```python
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](https://github.com/MattBroach/DjangoRestMultipleModels)
- `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:

```python
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.

```python
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](https://github.com/chibisov/drf-extensions).

## Contributing

Contributions are welcome! To get started, please refer to our [contribution guidelines](https://github.com/stefanofusai/drf-caching/blob/main/CONTRIBUTING.md).

## Issues

If you encounter any problems while using this package, please open a new issue [here](https://github.com/stefanofusai/drf-caching/issues).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "drf-caching",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.12",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "Stefano Fusai <stefanofusai@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/25/82/97ebeaefaedf7a99845d796af5ed78325de0ba95b455faa69324058ee352/drf_caching-1.2.0.tar.gz",
    "platform": null,
    "description": "# drf-caching\n\nHandle views caching in Django Rest Framework.\n\n## Installation\n\n```bash\npip install drf-caching\n```\n\n## Usage\n\nTo setup caching for a view, you can use the `@cache_view` decorator.\n\n```python\nfrom drf_caching import cache_view, GetQuerysetKey, PaginationKey, QueryParamsKey\n\nclass MyView(APIView):\n    @cache_view(\n        GetQuerysetKey(),\n        PaginationKey(),\n        QueryParamsKey(\"ordering\", \"search\"),\n        timeout=60,\n    )\n    def get(self, request):\n        return Response({\"message\": \"Hello, world!\"})\n```\n\nYou can pass multiple keys to the decorator, and the cache key will be built using all of them.\nYou can pass the following keyword arguments to the `@cache_view` decorator:\n\n- `timeout`: the cache timeout in seconds (can also be set on a global level using the `DRF_CACHING` setting)\n\nThe following keys, available in the `drf_caching.keys` module, can be used:\n\n- `GetObjectKey`: the cache key will be built using the view's get_object method\n- `GetQuerylistKey`: the cache key will be built using the view's get_querylist method from [django-rest-multiple-models](https://github.com/MattBroach/DjangoRestMultipleModels)\n- `GetQuerysetKey`: the cache key will be built using the view's get_queryset method\n- `LookupFieldKey`: the cache key will be built using the view's kwarg matching the lookup field\n- `RequestDataKey`: the cache key will be built using the request's data\n- `RequestHeadersKey`: the cache key will be built using the request's headers\n- `RequestKwargsKey`: the cache key will be built using the request's kwargs\n- `RequestPaginationKey`: the cache key will be built using the request's pagination parameters\n- `RequestQueryParamsKey`: the cache key will be built using the request's query parameters\n- `RequestUserKey`: the cache key will be built using the request's user\n\nIf no keys are passed, the cache key will be built using the view name and the request's format.\n\nThe settings can be customized as such:\n\n```python\nDRF_CACHING = {\n    \"CACHE\": \"default\",\n    \"HEADERS\": [\"age\", \"x-cache\"],\n    \"TIMEOUT\": 60,\n}\n```\n\nTo disable caching for a specific view, or even globally, you can set `timeout` to `0`.\n\nThe following settings are available:\n\n- `CACHE`: the cache to use (defaults to `default`)\n- `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`)\n- `TIMEOUT`: the default cache timeout in seconds\n\nTo create your own cache key, you can subclass the `BaseKey` and `BaseKeyWithFields` classes and implement the `_get_data` method.\n\n```python\nfrom drf_caching import BaseKey, BaseKeyWithFields\n\nclass CustomKey(BaseKey):\n    def _get_data(self, view_instance, view_method, request, *args, **kwargs):\n        return {\n            \"key\": \"value\"\n        }\n\nclass CustomKeyWithFields(BaseKeyWithFields):\n    def _get_data(self, view_instance, view_method, request, *args, **kwargs):\n        return {\n            field: ...\n            for field in self.fields\n        }\n```\n\n## Acknowledgments\n\nThis project was strongly inspired by [drf-extensions](https://github.com/chibisov/drf-extensions).\n\n## Contributing\n\nContributions are welcome! To get started, please refer to our [contribution guidelines](https://github.com/stefanofusai/drf-caching/blob/main/CONTRIBUTING.md).\n\n## Issues\n\nIf you encounter any problems while using this package, please open a new issue [here](https://github.com/stefanofusai/drf-caching/issues).\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Handle views caching in Django Rest Framework.",
    "version": "1.2.0",
    "project_urls": {
        "Homepage": "https://github.com/stefanofusai/drf-caching",
        "Repository": "https://github.com/stefanofusai/drf-caching"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c3d27218852d9f6e89d0dc89268ae00b4bdc067ea61646b5fdd535b2f5a9b241",
                "md5": "3eeba3e98fef06ff314e6510d0f6b96a",
                "sha256": "551c346e9bec6508149be4b7cd0ae5638cd8e0710790e21fb4da1dfe272f8a91"
            },
            "downloads": -1,
            "filename": "drf_caching-1.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3eeba3e98fef06ff314e6510d0f6b96a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.12",
            "size": 9646,
            "upload_time": "2024-10-20T16:09:02",
            "upload_time_iso_8601": "2024-10-20T16:09:02.996844Z",
            "url": "https://files.pythonhosted.org/packages/c3/d2/7218852d9f6e89d0dc89268ae00b4bdc067ea61646b5fdd535b2f5a9b241/drf_caching-1.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "258297ebeaefaedf7a99845d796af5ed78325de0ba95b455faa69324058ee352",
                "md5": "18241bbf24b3e7a4b68a18b7ee3980a2",
                "sha256": "162d89b9272524b8977d1bbcd357ef860249d201e1856a022506b825e61c6025"
            },
            "downloads": -1,
            "filename": "drf_caching-1.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "18241bbf24b3e7a4b68a18b7ee3980a2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.12",
            "size": 9498,
            "upload_time": "2024-10-20T16:09:04",
            "upload_time_iso_8601": "2024-10-20T16:09:04.682144Z",
            "url": "https://files.pythonhosted.org/packages/25/82/97ebeaefaedf7a99845d796af5ed78325de0ba95b455faa69324058ee352/drf_caching-1.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-20 16:09:04",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "stefanofusai",
    "github_project": "drf-caching",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "annotated-types",
            "specs": [
                [
                    "==",
                    "0.7.0"
                ]
            ]
        },
        {
            "name": "asgiref",
            "specs": [
                [
                    "==",
                    "3.8.1"
                ]
            ]
        },
        {
            "name": "beautifulsoup4",
            "specs": [
                [
                    "==",
                    "4.12.3"
                ]
            ]
        },
        {
            "name": "cfgv",
            "specs": [
                [
                    "==",
                    "3.4.0"
                ]
            ]
        },
        {
            "name": "distlib",
            "specs": [
                [
                    "==",
                    "0.3.8"
                ]
            ]
        },
        {
            "name": "django",
            "specs": [
                [
                    "==",
                    "5.1.2"
                ]
            ]
        },
        {
            "name": "django-redis",
            "specs": [
                [
                    "==",
                    "5.4.0"
                ]
            ]
        },
        {
            "name": "djangorestframework",
            "specs": [
                [
                    "==",
                    "3.15.2"
                ]
            ]
        },
        {
            "name": "filelock",
            "specs": [
                [
                    "==",
                    "3.15.4"
                ]
            ]
        },
        {
            "name": "identify",
            "specs": [
                [
                    "==",
                    "2.5.36"
                ]
            ]
        },
        {
            "name": "lxml",
            "specs": [
                [
                    "==",
                    "5.3.0"
                ]
            ]
        },
        {
            "name": "nodeenv",
            "specs": [
                [
                    "==",
                    "1.9.1"
                ]
            ]
        },
        {
            "name": "platformdirs",
            "specs": [
                [
                    "==",
                    "4.2.2"
                ]
            ]
        },
        {
            "name": "pre-commit",
            "specs": [
                [
                    "==",
                    "4.0.1"
                ]
            ]
        },
        {
            "name": "pydantic",
            "specs": [
                [
                    "==",
                    "2.9.2"
                ]
            ]
        },
        {
            "name": "pydantic-core",
            "specs": [
                [
                    "==",
                    "2.23.4"
                ]
            ]
        },
        {
            "name": "pyyaml",
            "specs": [
                [
                    "==",
                    "6.0.1"
                ]
            ]
        },
        {
            "name": "redis",
            "specs": [
                [
                    "==",
                    "5.0.7"
                ]
            ]
        },
        {
            "name": "ruff",
            "specs": [
                [
                    "==",
                    "0.6.9"
                ]
            ]
        },
        {
            "name": "soupsieve",
            "specs": [
                [
                    "==",
                    "2.5"
                ]
            ]
        },
        {
            "name": "sqlparse",
            "specs": [
                [
                    "==",
                    "0.5.0"
                ]
            ]
        },
        {
            "name": "typing-extensions",
            "specs": [
                [
                    "==",
                    "4.12.2"
                ]
            ]
        },
        {
            "name": "virtualenv",
            "specs": [
                [
                    "==",
                    "20.26.3"
                ]
            ]
        }
    ],
    "lcname": "drf-caching"
}
        
Elapsed time: 1.29531s