mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-24 20:47:54 +00:00
Merge branch 'main' into feat/type-hints-covering
# Conflicts: # fastapi_cache/coder.py # fastapi_cache/decorator.py
This commit is contained in:
@@ -2,6 +2,10 @@
|
||||
|
||||
## 0.2
|
||||
|
||||
### 0.2.1
|
||||
|
||||
- Support cache jinja2 template response.
|
||||
|
||||
### 0.2.0
|
||||
|
||||
- Make `request` and `response` optional.
|
||||
@@ -9,6 +13,10 @@
|
||||
|
||||
## 0.1
|
||||
|
||||
### 0.1.10
|
||||
|
||||
- Add `Cache-Control:no-cache` support.
|
||||
|
||||
### 0.1.9
|
||||
|
||||
- Replace `aioredis` with `redis-py`.
|
||||
|
||||
10
examples/redis/index.html
Normal file
10
examples/redis/index.html
Normal file
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Cache HTML! {{ ret }} </h1>
|
||||
</body>
|
||||
</html>
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -57,6 +65,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")
|
||||
|
||||
@@ -6,6 +6,7 @@ from typing import Any, Dict, Union
|
||||
|
||||
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) -> str:
|
||||
if isinstance(value, TemplateResponse):
|
||||
value = value.body
|
||||
return str(pickle.dumps(value))
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -97,7 +97,7 @@ def cache(
|
||||
request: Optional[Request] = copy_kwargs.pop("request", None)
|
||||
response: Optional[Response] = copy_kwargs.pop("response", None)
|
||||
if (
|
||||
request and request.headers.get("Cache-Control") == "no-store"
|
||||
request and request.headers.get("Cache-Control") in ("no-store", "no-cache")
|
||||
) or not FastAPICache.get_enable():
|
||||
return await ensure_async_func(*args, **kwargs)
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "fastapi-cache2"
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
description = "Cache for FastAPI"
|
||||
authors = ["long2ice <long2ice@gmail.com>"]
|
||||
license = "Apache-2.0"
|
||||
|
||||
Reference in New Issue
Block a user