2022-08-01 00:04:10 +02:00
|
|
|
import time
|
2021-10-09 16:51:05 +08:00
|
|
|
|
2022-09-10 20:06:37 +08:00
|
|
|
import pendulum
|
2022-06-17 11:01:47 +08:00
|
|
|
import redis.asyncio as redis
|
2020-08-26 18:04:57 +08:00
|
|
|
import uvicorn
|
|
|
|
|
from fastapi import FastAPI
|
2023-01-07 13:55:41 +08:00
|
|
|
from starlette.responses import JSONResponse
|
2022-09-28 17:37:05 +08:00
|
|
|
from fastapi.responses import HTMLResponse
|
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
|
from fastapi.templating import Jinja2Templates
|
2022-11-07 16:39:17 +08:00
|
|
|
from redis.asyncio.connection import ConnectionPool
|
|
|
|
|
from starlette.requests import Request
|
|
|
|
|
from starlette.responses import Response
|
|
|
|
|
|
2020-08-26 18:04:57 +08:00
|
|
|
from fastapi_cache import FastAPICache
|
2021-07-26 16:33:22 +08:00
|
|
|
from fastapi_cache.backends.redis import RedisBackend
|
2022-09-28 17:37:05 +08:00
|
|
|
from fastapi_cache.coder import PickleCoder
|
2020-10-08 15:10:34 +08:00
|
|
|
from fastapi_cache.decorator import cache
|
2020-08-26 18:04:57 +08:00
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
2022-09-28 17:37:05 +08:00
|
|
|
app.mount(
|
2022-11-07 16:39:17 +08:00
|
|
|
path="/static",
|
|
|
|
|
app=StaticFiles(directory="./"),
|
|
|
|
|
name="static",
|
2022-09-28 17:37:05 +08:00
|
|
|
)
|
2022-11-07 16:39:17 +08:00
|
|
|
templates = Jinja2Templates(directory="./")
|
2020-10-08 15:10:34 +08:00
|
|
|
ret = 0
|
2020-08-26 18:04:57 +08:00
|
|
|
|
2020-10-08 15:10:34 +08:00
|
|
|
|
2020-11-03 18:08:06 +08:00
|
|
|
@cache(namespace="test", expire=1)
|
2020-10-08 15:10:34 +08:00
|
|
|
async def get_ret():
|
|
|
|
|
global ret
|
|
|
|
|
ret = ret + 1
|
|
|
|
|
return ret
|
2020-08-26 18:04:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/")
|
2022-09-10 20:06:37 +08:00
|
|
|
@cache(namespace="test", expire=10)
|
|
|
|
|
async def index():
|
2020-10-08 15:10:34 +08:00
|
|
|
return dict(ret=await get_ret())
|
2020-08-26 18:04:57 +08:00
|
|
|
|
|
|
|
|
|
2020-11-03 18:08:06 +08:00
|
|
|
@app.get("/clear")
|
|
|
|
|
async def clear():
|
|
|
|
|
return await FastAPICache.clear(namespace="test")
|
|
|
|
|
|
|
|
|
|
|
2021-10-09 16:51:05 +08:00
|
|
|
@app.get("/date")
|
2022-09-10 20:06:37 +08:00
|
|
|
@cache(namespace="test", expire=10)
|
2021-10-09 16:51:05 +08:00
|
|
|
async def get_data(request: Request, response: Response):
|
2022-09-10 20:06:37 +08:00
|
|
|
return pendulum.today()
|
2021-10-09 16:51:05 +08:00
|
|
|
|
|
|
|
|
|
2022-10-14 13:44:59 +02:00
|
|
|
# Note: This function MUST be sync to demonstrate fastapi-cache's correct handling,
|
|
|
|
|
# i.e. running cached sync functions in threadpool just like FastAPI itself!
|
2022-08-01 00:04:10 +02:00
|
|
|
@app.get("/blocking")
|
2022-09-10 20:06:37 +08:00
|
|
|
@cache(namespace="test", expire=10)
|
2022-10-14 13:44:59 +02:00
|
|
|
def blocking():
|
2022-10-14 21:59:57 +02:00
|
|
|
time.sleep(2)
|
|
|
|
|
return dict(ret=42)
|
2022-08-01 00:04:10 +02:00
|
|
|
|
|
|
|
|
|
2021-10-09 16:51:05 +08:00
|
|
|
@app.get("/datetime")
|
2022-09-10 20:06:37 +08:00
|
|
|
@cache(namespace="test", expire=2)
|
2021-10-09 16:51:05 +08:00
|
|
|
async def get_datetime(request: Request, response: Response):
|
2022-09-10 20:06:37 +08:00
|
|
|
print(request, response)
|
|
|
|
|
return pendulum.now()
|
2021-10-09 16:51:05 +08:00
|
|
|
|
|
|
|
|
|
2022-11-07 16:39:17 +08:00
|
|
|
@app.get("/html", response_class=HTMLResponse)
|
2022-09-28 17:37:05 +08:00
|
|
|
@cache(expire=60, namespace="html", coder=PickleCoder)
|
|
|
|
|
async def cache_html(request: Request):
|
2022-11-07 16:39:17 +08:00
|
|
|
return templates.TemplateResponse("index.html", {"request": request, "ret": await get_ret()})
|
2022-09-28 17:37:05 +08:00
|
|
|
|
|
|
|
|
|
2022-11-04 17:34:20 +08:00
|
|
|
@app.get("/cache_response_obj")
|
|
|
|
|
@cache(namespace="test", expire=5)
|
|
|
|
|
async def cache_response_obj():
|
|
|
|
|
return JSONResponse({"a": 1})
|
|
|
|
|
|
|
|
|
|
|
2020-08-26 18:04:57 +08:00
|
|
|
@app.on_event("startup")
|
|
|
|
|
async def startup():
|
2022-09-10 20:06:37 +08:00
|
|
|
pool = ConnectionPool.from_url(url="redis://redis")
|
2022-06-17 11:01:47 +08:00
|
|
|
r = redis.Redis(connection_pool=pool)
|
|
|
|
|
FastAPICache.init(RedisBackend(r), prefix="fastapi-cache")
|
2020-08-26 18:04:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
uvicorn.run("main:app", debug=True, reload=True)
|