mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
Merge pull request #88 from vvanglro/feat/cache_html
feat: support cache jinja2 template response
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user