Metadata-Version: 2.4
Name: patera-caching
Version: 0.13.4
Summary: Response caching for Patera with in-memory, Redis and SQLite backends
Keywords: patera,cache,redis
Author: Marko Šterk
Author-email: Marko Šterk <marko_sterk@hotmail.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Typing :: Typed
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
Requires-Dist: cachetools>=7.0.5
Requires-Dist: patera>=0.121
Requires-Dist: redis>=5.0 ; extra == 'all'
Requires-Dist: aiosqlite>=0.20 ; extra == 'all'
Requires-Dist: redis>=5.0 ; extra == 'redis'
Requires-Dist: aiosqlite>=0.20 ; extra == 'sqlite'
Requires-Python: >=3.13
Project-URL: Homepage, https://github.com/MarkoSterk/Patera
Project-URL: Documentation, https://github.com/MarkoSterk/Patera#readme
Project-URL: Repository, https://github.com/MarkoSterk/Patera
Project-URL: Issues, https://github.com/MarkoSterk/Patera/issues
Project-URL: Changelog, https://github.com/MarkoSterk/Patera/blob/main/CHANGELOG.md
Provides-Extra: all
Provides-Extra: redis
Provides-Extra: sqlite
Description-Content-Type: text/markdown

# patera-caching

Response caching for Patera with in-memory, Redis and SQLite backends. Part of [Patera](https://github.com/MarkoSterk/Patera), a modular async-first Python web framework.

## Installation

```sh
pip install patera-caching
# or, with uv
uv add "patera[caching]"
```

Requires Python 3.13 or newer and `patera>=0.121`.

## Usage

Only GET and HEAD are cached by default. Opt into POST explicitly with
`@cache("PUBLIC_CACHE", methods=("POST",))` for read-only RPC operations; cache
hits skip the handler. Opted-in methods include a SHA-256 body digest in the key.
Default keys contain the handler's module and qualified name, method, path and
query. Existing entries with the former short-name keys expire naturally; update
manual invalidation patterns to include the qualified name.

`key` accepts a string or a sync/async callable taking `Request`. For example,
`key=lambda req: f"page:{req.path}:{req.headers.get('accept-language', '')}"`
separates language variants. A custom key replaces the default URL/handler key,
so include every relevant variation. Body digests and default user partitioning
still apply. User partitioning alone does not account for headers or other
request-dependent data.

`patera-caching` caches responses of endpoints:

```python
from patera.caching import Caching, CachingConfig, cache


class PublicCache(Caching):
    pass                                 # in-memory backend by default


class Config(BaseConfig):
    ...
    PUBLIC_CACHE: CachingConfig = CachingConfig(DURATION=60, MAX_SIZE=1000)


@path("/stats")
class StatsApi(Controller[App]):
    @get("/")
    @cache("PUBLIC_CACHE", duration=15)  # registered name of the extension
    async def stats(self, req: Request) -> Response:
        return req.res.json(await compute_stats())
```

See the [Caching section of the Patera documentation](https://github.com/MarkoSterk/Patera#caching) for the full guide.

## License

MIT. See [LICENSE](https://github.com/MarkoSterk/Patera/blob/main/LICENSE).
