mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-24 20:47:54 +00:00
feat: make request and response optional
This commit is contained in:
43
README.md
43
README.md
@@ -7,7 +7,8 @@
|
||||
|
||||
## Introduction
|
||||
|
||||
`fastapi-cache` is a tool to cache fastapi response and function result, with backends support `redis`, `memcache`, and `dynamodb`.
|
||||
`fastapi-cache` is a tool to cache fastapi response and function result, with backends support `redis`, `memcache`,
|
||||
and `dynamodb`.
|
||||
|
||||
## Features
|
||||
|
||||
@@ -70,13 +71,13 @@ async def get_cache():
|
||||
|
||||
@app.get("/")
|
||||
@cache(expire=60)
|
||||
async def index(request: Request, response: Response):
|
||||
async def index():
|
||||
return dict(hello="world")
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
redis = aioredis.from_url("redis://localhost", encoding="utf8", decode_responses=True)
|
||||
redis = aioredis.from_url("redis://localhost", encoding="utf8", decode_responses=True)
|
||||
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
||||
|
||||
```
|
||||
@@ -87,7 +88,8 @@ Firstly you must call `FastAPICache.init` on startup event of `fastapi`, there a
|
||||
|
||||
### Use `cache` decorator
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
Parameter | type, description
|
||||
------------ | -------------
|
||||
@@ -96,48 +98,49 @@ 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
|
||||
|
||||
|
||||
And if you want use `ETag` and `Cache-Control` features, you must pass `response` param also.
|
||||
|
||||
You can also use `cache` as decorator like other cache tools to cache common function result.
|
||||
|
||||
### Custom coder
|
||||
|
||||
By default use `JsonCoder`, you can write custom coder to encode and decode cache result, just need inherit `fastapi_cache.coder.Coder`.
|
||||
By default use `JsonCoder`, you can write custom coder to encode and decode cache result, just need
|
||||
inherit `fastapi_cache.coder.Coder`.
|
||||
|
||||
```python
|
||||
@app.get("/")
|
||||
@cache(expire=60,coder=JsonCoder)
|
||||
async def index(request: Request, response: Response):
|
||||
@cache(expire=60, coder=JsonCoder)
|
||||
async def index():
|
||||
return dict(hello="world")
|
||||
```
|
||||
|
||||
### Custom key builder
|
||||
|
||||
By default use builtin key builder, if you need, you can override this and pass in `cache` or `FastAPICache.init` to take effect globally.
|
||||
By default use builtin key builder, if you need, you can override this and pass in `cache` or `FastAPICache.init` to
|
||||
take effect globally.
|
||||
|
||||
```python
|
||||
def my_key_builder(
|
||||
func,
|
||||
namespace: Optional[str] = "",
|
||||
request: Request = None,
|
||||
response: Response = None,
|
||||
*args,
|
||||
**kwargs,
|
||||
func,
|
||||
namespace: Optional[str] = "",
|
||||
request: Request = None,
|
||||
response: Response = None,
|
||||
*args,
|
||||
**kwargs,
|
||||
):
|
||||
prefix = FastAPICache.get_prefix()
|
||||
cache_key = f"{prefix}:{namespace}:{func.__module__}:{func.__name__}:{args}:{kwargs}"
|
||||
return cache_key
|
||||
|
||||
|
||||
@app.get("/")
|
||||
@cache(expire=60,coder=JsonCoder,key_builder=my_key_builder)
|
||||
async def index(request: Request, response: Response):
|
||||
@cache(expire=60, coder=JsonCoder, key_builder=my_key_builder)
|
||||
async def index():
|
||||
return dict(hello="world")
|
||||
```
|
||||
|
||||
### InMemoryBackend
|
||||
|
||||
`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.
|
||||
`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.
|
||||
|
||||
## License
|
||||
|
||||
|
||||
Reference in New Issue
Block a user