From dacd7e1b0f951f7e3c844193851ff2021b0b2620 Mon Sep 17 00:00:00 2001 From: Lucca Leme Marques Date: Mon, 23 Aug 2021 11:51:28 -0300 Subject: [PATCH 1/3] add `no-cache` to cache exclusion --- fastapi_cache/decorator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastapi_cache/decorator.py b/fastapi_cache/decorator.py index 07573b1..b12a0d2 100644 --- a/fastapi_cache/decorator.py +++ b/fastapi_cache/decorator.py @@ -29,7 +29,7 @@ def cache( copy_kwargs = kwargs.copy() request = copy_kwargs.pop("request", None) response = copy_kwargs.pop("response", None) - if request and request.headers.get("Cache-Control") == "no-store": + if request and request.headers.get("Cache-Control") in ("no-store", "no-cache"): return await func(*args, **kwargs) coder = coder or FastAPICache.get_coder() From e62f0117e044ec5749d8b82bf2aaf25d5f555452 Mon Sep 17 00:00:00 2001 From: Lucca Marques Date: Tue, 9 Aug 2022 13:45:53 -0300 Subject: [PATCH 2/3] feat: changelog changes --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 021e393..d765736 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.1 +### 0.1.10 + +- Add `Cache-Control:no-cache` support. + ### 0.1.9 - Replace `aioredis` with `redis-py`. From 7e64cd6490492ce3dd771be6271f3ac878d84935 Mon Sep 17 00:00:00 2001 From: vvanglro <947001731@qq.com> Date: Wed, 28 Sep 2022 17:37:05 +0800 Subject: [PATCH 3/3] feat: support cache jinja2 template response --- CHANGELOG.md | 4 ++++ examples/redis/index.html | 10 ++++++++++ examples/redis/main.py | 18 +++++++++++++++++- fastapi_cache/coder.py | 3 +++ pyproject.toml | 2 +- 5 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 examples/redis/index.html diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a15f9d..dd42c45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.2 +### 0.2.1 + +- Support cache jinja2 template response. + ### 0.2.0 - Make `request` and `response` optional. diff --git a/examples/redis/index.html b/examples/redis/index.html new file mode 100644 index 0000000..eee639c --- /dev/null +++ b/examples/redis/index.html @@ -0,0 +1,10 @@ + + + + + Title + + +

Cache HTML! {{ ret }}

+ + \ No newline at end of file diff --git a/examples/redis/main.py b/examples/redis/main.py index 9c51d15..6d88a71 100644 --- a/examples/redis/main.py +++ b/examples/redis/main.py @@ -7,13 +7,21 @@ from fastapi import FastAPI from redis.asyncio.connection import ConnectionPool from starlette.requests import Request from starlette.responses import Response - +from fastapi.responses import HTMLResponse +from fastapi.staticfiles import StaticFiles +from fastapi.templating import Jinja2Templates from fastapi_cache import FastAPICache from fastapi_cache.backends.redis import RedisBackend +from fastapi_cache.coder import PickleCoder from fastapi_cache.decorator import cache app = FastAPI() +app.mount( + path='/static', + app=StaticFiles(directory='./'), name='static', +) +templates = Jinja2Templates(directory='./') ret = 0 @@ -55,6 +63,14 @@ async def get_datetime(request: Request, response: Response): return pendulum.now() +@app.get('/html', response_class=HTMLResponse) +@cache(expire=60, namespace="html", coder=PickleCoder) +async def cache_html(request: Request): + return templates.TemplateResponse('index.html', { + 'request': request, "ret": await get_ret() + }) + + @app.on_event("startup") async def startup(): pool = ConnectionPool.from_url(url="redis://redis") diff --git a/fastapi_cache/coder.py b/fastapi_cache/coder.py index 0d45079..daeac83 100644 --- a/fastapi_cache/coder.py +++ b/fastapi_cache/coder.py @@ -6,6 +6,7 @@ from typing import Any import pendulum from fastapi.encoders import jsonable_encoder +from starlette.templating import _TemplateResponse as TemplateResponse CONVERTERS = { "date": lambda x: pendulum.parse(x, exact=True), @@ -60,6 +61,8 @@ class JsonCoder(Coder): class PickleCoder(Coder): @classmethod def encode(cls, value: Any): + if isinstance(value, TemplateResponse): + value = value.body return pickle.dumps(value) @classmethod diff --git a/pyproject.toml b/pyproject.toml index d345c73..dfe8dd2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "fastapi-cache2" -version = "0.2.0" +version = "0.2.1" description = "Cache for FastAPI" authors = ["long2ice "] license = "Apache-2.0"