mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 13:07:53 +00:00
67 lines
1.4 KiB
Python
67 lines
1.4 KiB
Python
import pendulum
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
from starlette.requests import Request
|
|
from starlette.responses import JSONResponse, Response
|
|
|
|
from fastapi_cache import FastAPICache
|
|
from fastapi_cache.backends.inmemory import InMemoryBackend
|
|
from fastapi_cache.decorator import cache
|
|
|
|
app = FastAPI()
|
|
|
|
ret = 0
|
|
|
|
|
|
@cache(namespace="test", expire=1)
|
|
async def get_ret():
|
|
global ret
|
|
ret = ret + 1
|
|
return ret
|
|
|
|
|
|
@app.get("/")
|
|
@cache(namespace="test", expire=10)
|
|
async def index():
|
|
return dict(ret=await get_ret())
|
|
|
|
|
|
@app.get("/clear")
|
|
async def clear():
|
|
return await FastAPICache.clear(namespace="test")
|
|
|
|
|
|
@app.get("/date")
|
|
@cache(namespace="test", expire=10)
|
|
async def get_date():
|
|
return pendulum.today()
|
|
|
|
|
|
@app.get("/datetime")
|
|
@cache(namespace="test", expire=2)
|
|
async def get_datetime(request: Request, response: Response):
|
|
return {"now": pendulum.now()}
|
|
|
|
|
|
@app.get("/sync-me")
|
|
@cache(namespace="test")
|
|
def sync_me():
|
|
# as per the fastapi docs, this sync function is wrapped in a thread,
|
|
# thereby converted to async. fastapi-cache does the same.
|
|
return 42
|
|
|
|
|
|
@app.get("/cache_response_obj")
|
|
@cache(namespace="test", expire=5)
|
|
async def cache_response_obj():
|
|
return JSONResponse({"a": 1})
|
|
|
|
|
|
@app.on_event("startup")
|
|
async def startup():
|
|
FastAPICache.init(InMemoryBackend())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run("main:app", debug=True, reload=True)
|