2020-08-26 18:04:57 +08:00
|
|
|
# fastapi-cache
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|

|
|
|
|
|

|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
## Introduction
|
|
|
|
|
|
2022-09-10 20:06:37 +08:00
|
|
|
`fastapi-cache` is a tool to cache fastapi response and function result, with backends support `redis`, `memcache`,
|
|
|
|
|
and `dynamodb`.
|
2020-08-26 18:04:57 +08:00
|
|
|
|
|
|
|
|
## Features
|
|
|
|
|
|
2021-09-29 16:22:04 +02:00
|
|
|
- Support `redis`, `memcache`, `dynamodb`, and `in-memory` backends.
|
2020-08-26 18:04:57 +08:00
|
|
|
- Easily integration with `fastapi`.
|
|
|
|
|
- Support http cache like `ETag` and `Cache-Control`.
|
|
|
|
|
|
|
|
|
|
## Requirements
|
|
|
|
|
|
|
|
|
|
- `asyncio` environment.
|
|
|
|
|
- `redis` if use `RedisBackend`.
|
|
|
|
|
- `memcache` if use `MemcacheBackend`.
|
2021-09-29 16:22:04 +02:00
|
|
|
- `aiobotocore` if use `DynamoBackend`.
|
2020-08-26 18:04:57 +08:00
|
|
|
|
|
|
|
|
## Install
|
|
|
|
|
|
2020-10-08 15:10:34 +08:00
|
|
|
```shell
|
|
|
|
|
> pip install fastapi-cache2
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
2020-08-26 18:04:57 +08:00
|
|
|
```shell
|
2021-09-23 16:10:31 -04:00
|
|
|
> pip install "fastapi-cache2[redis]"
|
2020-08-26 18:04:57 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
|
|
```shell
|
2021-09-23 16:10:31 -04:00
|
|
|
> pip install "fastapi-cache2[memcache]"
|
2020-08-26 18:04:57 +08:00
|
|
|
```
|
|
|
|
|
|
2021-09-29 16:22:04 +02:00
|
|
|
or
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
> pip install "fastapi-cache2[dynamodb]"
|
|
|
|
|
```
|
|
|
|
|
|
2020-08-26 18:04:57 +08:00
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
### Quick Start
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
from starlette.requests import Request
|
|
|
|
|
from starlette.responses import Response
|
|
|
|
|
|
|
|
|
|
from fastapi_cache import FastAPICache
|
|
|
|
|
from fastapi_cache.backends.redis import RedisBackend
|
2020-10-08 15:10:34 +08:00
|
|
|
from fastapi_cache.decorator import cache
|
2020-08-26 18:04:57 +08:00
|
|
|
|
2022-09-14 16:24:10 +08:00
|
|
|
from redis import asyncio as aioredis
|
|
|
|
|
|
2020-08-26 18:04:57 +08:00
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@cache()
|
|
|
|
|
async def get_cache():
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/")
|
2020-10-08 15:10:34 +08:00
|
|
|
@cache(expire=60)
|
2022-09-10 20:06:37 +08:00
|
|
|
async def index():
|
2020-08-26 18:04:57 +08:00
|
|
|
return dict(hello="world")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.on_event("startup")
|
|
|
|
|
async def startup():
|
2022-09-10 20:06:37 +08:00
|
|
|
redis = aioredis.from_url("redis://localhost", encoding="utf8", decode_responses=True)
|
2020-08-26 18:04:57 +08:00
|
|
|
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-12 13:04:50 +08:00
|
|
|
### Initialization
|
|
|
|
|
|
|
|
|
|
Firstly you must call `FastAPICache.init` on startup event of `fastapi`, there are some global config you can pass in.
|
|
|
|
|
|
2020-10-08 15:10:34 +08:00
|
|
|
### Use `cache` decorator
|
2020-08-26 18:04:57 +08:00
|
|
|
|
2022-09-10 20:06:37 +08:00
|
|
|
If you want cache `fastapi` response transparently, you can use `cache` as decorator between router decorator and view
|
|
|
|
|
function and must pass `request` as param of view function.
|
2020-08-26 18:04:57 +08:00
|
|
|
|
2021-01-11 17:37:39 +01:00
|
|
|
Parameter | type, description
|
|
|
|
|
------------ | -------------
|
|
|
|
|
expire | int, states a caching time in seconds
|
|
|
|
|
namespace | str, namespace to use to store certain cache items
|
|
|
|
|
coder | which coder to use, e.g. JsonCoder
|
|
|
|
|
key_builder | which key builder to use, default to builtin
|
|
|
|
|
|
2020-10-08 15:10:34 +08:00
|
|
|
You can also use `cache` as decorator like other cache tools to cache common function result.
|
2020-08-26 18:04:57 +08:00
|
|
|
|
2020-08-27 09:27:52 +08:00
|
|
|
### Custom coder
|
|
|
|
|
|
2022-09-10 20:06:37 +08:00
|
|
|
By default use `JsonCoder`, you can write custom coder to encode and decode cache result, just need
|
|
|
|
|
inherit `fastapi_cache.coder.Coder`.
|
2020-08-27 09:27:52 +08:00
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
@app.get("/")
|
2022-09-10 20:06:37 +08:00
|
|
|
@cache(expire=60, coder=JsonCoder)
|
|
|
|
|
async def index():
|
2020-08-27 09:27:52 +08:00
|
|
|
return dict(hello="world")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Custom key builder
|
|
|
|
|
|
2022-09-10 20:06:37 +08:00
|
|
|
By default use builtin key builder, if you need, you can override this and pass in `cache` or `FastAPICache.init` to
|
|
|
|
|
take effect globally.
|
2020-11-12 13:04:50 +08:00
|
|
|
|
2020-08-27 09:27:52 +08:00
|
|
|
```python
|
|
|
|
|
def my_key_builder(
|
2022-09-10 20:06:37 +08:00
|
|
|
func,
|
|
|
|
|
namespace: Optional[str] = "",
|
|
|
|
|
request: Request = None,
|
|
|
|
|
response: Response = None,
|
|
|
|
|
*args,
|
|
|
|
|
**kwargs,
|
2020-08-27 09:27:52 +08:00
|
|
|
):
|
|
|
|
|
prefix = FastAPICache.get_prefix()
|
|
|
|
|
cache_key = f"{prefix}:{namespace}:{func.__module__}:{func.__name__}:{args}:{kwargs}"
|
|
|
|
|
return cache_key
|
|
|
|
|
|
2022-09-10 20:06:37 +08:00
|
|
|
|
2020-08-27 09:27:52 +08:00
|
|
|
@app.get("/")
|
2022-09-10 20:06:37 +08:00
|
|
|
@cache(expire=60, coder=JsonCoder, key_builder=my_key_builder)
|
|
|
|
|
async def index():
|
2020-08-27 09:27:52 +08:00
|
|
|
return dict(hello="world")
|
|
|
|
|
```
|
|
|
|
|
|
2020-10-08 15:10:34 +08:00
|
|
|
### InMemoryBackend
|
|
|
|
|
|
2022-09-10 20:06:37 +08:00
|
|
|
`InMemoryBackend` store cache data in memory and use lazy delete, which mean if you don't access it after cached, it
|
|
|
|
|
will not delete automatically.
|
2020-10-08 15:10:34 +08:00
|
|
|
|
2022-10-14 21:59:51 +02:00
|
|
|
## Tests and coverage
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
coverage run -m pytest
|
|
|
|
|
coverage html
|
|
|
|
|
xdg-open htmlcov/index.html
|
|
|
|
|
```
|
|
|
|
|
|
2020-08-26 18:04:57 +08:00
|
|
|
## License
|
|
|
|
|
|
|
|
|
|
This project is licensed under the [Apache-2.0](https://github.com/long2ice/fastapi-cache/blob/master/LICENSE) License.
|