From 800032c46bbb867d5aeeae1f9d48836ceee43d7c Mon Sep 17 00:00:00 2001 From: long2ice Date: Thu, 27 Aug 2020 09:27:52 +0800 Subject: [PATCH] update README.md --- README.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7aa7301..dac4218 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ async def startup(): ### Use `cache_response` -If you want cache `fastapi` response transparently, you can use cache_response 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_response` as decorator between router decorator and view function and must pass `request` as param of view function. And if you want use `ETag` and `Cache-Control` features, you must pass `response` param also. @@ -79,6 +79,38 @@ And if you want use `ETag` and `Cache-Control` features, you must pass `response You can 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`. + +```python +@app.get("/") +@cache_response(expire=60,coder=JsonCoder) +async def index(request: Request, response: Response): + return dict(hello="world") +``` + +### Custom key builder + +```python +def my_key_builder( + 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_response(expire=60,coder=JsonCoder,key_builder=my_key_builder) +async def index(request: Request, response: Response): + return dict(hello="world") +``` + ## License This project is licensed under the [Apache-2.0](https://github.com/long2ice/fastapi-cache/blob/master/LICENSE) License.