2023-05-09 15:30:46 +01:00
|
|
|
from typing import Dict, Optional
|
|
|
|
|
|
2022-09-10 20:06:37 +08:00
|
|
|
import pendulum
|
2020-08-26 18:04:57 +08:00
|
|
|
import uvicorn
|
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
from starlette.requests import Request
|
2023-01-11 21:20:41 +08:00
|
|
|
from starlette.responses import JSONResponse, Response
|
2020-08-26 18:04:57 +08:00
|
|
|
|
|
|
|
|
from fastapi_cache import FastAPICache
|
2022-02-24 10:07:33 -06:00
|
|
|
from fastapi_cache.backends.inmemory import InMemoryBackend
|
2020-10-08 15:10:34 +08:00
|
|
|
from fastapi_cache.decorator import cache
|
2023-05-08 16:42:21 +01:00
|
|
|
from pydantic import BaseModel
|
2020-08-26 18:04:57 +08:00
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
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)
|
2022-10-14 21:59:33 +02:00
|
|
|
async def get_date():
|
2022-09-10 20:06:37 +08:00
|
|
|
return pendulum.today()
|
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
|
|
|
return {"now": pendulum.now()}
|
2021-10-09 16:51:05 +08:00
|
|
|
|
2023-02-15 10:45:19 +08:00
|
|
|
|
2023-02-15 10:35:41 +09:00
|
|
|
@cache(namespace="test")
|
|
|
|
|
async def func_kwargs(*unused_args, **kwargs):
|
|
|
|
|
return kwargs
|
|
|
|
|
|
2023-02-15 10:45:19 +08:00
|
|
|
|
2023-02-15 10:35:41 +09:00
|
|
|
@app.get("/kwargs")
|
|
|
|
|
async def get_kwargs(name: str):
|
|
|
|
|
return await func_kwargs(name, name=name)
|
2022-11-04 17:34:20 +08:00
|
|
|
|
2023-02-15 10:45:19 +08:00
|
|
|
|
2022-10-14 21:59:33 +02:00
|
|
|
@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
|
|
|
|
|
|
2021-10-09 16:51: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})
|
|
|
|
|
|
|
|
|
|
|
2023-04-27 18:14:59 +01:00
|
|
|
class SomeClass:
|
|
|
|
|
def __init__(self, value):
|
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
|
|
async def handler_method(self):
|
|
|
|
|
return self.value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# register an instance method as a handler
|
|
|
|
|
instance = SomeClass(17)
|
|
|
|
|
app.get("/method")(cache(namespace="test")(instance.handler_method))
|
|
|
|
|
|
|
|
|
|
|
2023-05-08 16:42:21 +01:00
|
|
|
# cache a Pydantic model instance; the return type annotation is required in this case
|
|
|
|
|
class Item(BaseModel):
|
|
|
|
|
name: str
|
2023-05-09 15:30:46 +01:00
|
|
|
description: Optional[str] = None
|
2023-05-08 16:42:21 +01:00
|
|
|
price: float
|
2023-05-09 15:30:46 +01:00
|
|
|
tax: Optional[float] = None
|
2023-05-08 16:42:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/pydantic_instance")
|
|
|
|
|
@cache(namespace="test", expire=5)
|
|
|
|
|
async def pydantic_instance() -> Item:
|
|
|
|
|
return Item(name="Something", description="An instance of a Pydantic model", price=10.5)
|
|
|
|
|
|
|
|
|
|
|
2023-04-28 18:43:40 +01:00
|
|
|
put_ret = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.put("/uncached_put")
|
|
|
|
|
@cache(namespace="test", expire=5)
|
|
|
|
|
async def uncached_put():
|
|
|
|
|
global put_ret
|
|
|
|
|
put_ret = put_ret + 1
|
|
|
|
|
return {"value": put_ret}
|
|
|
|
|
|
|
|
|
|
|
2023-04-28 16:10:11 +01:00
|
|
|
@app.get("/namespaced_injection")
|
|
|
|
|
@cache(namespace="test", expire=5, injected_dependency_namespace="monty_python")
|
|
|
|
|
def namespaced_injection(
|
|
|
|
|
__fastapi_cache_request: int = 42, __fastapi_cache_response: int = 17
|
2023-05-09 15:30:46 +01:00
|
|
|
) -> Dict[str, int]:
|
2023-04-28 16:10:11 +01:00
|
|
|
return {
|
|
|
|
|
"__fastapi_cache_request": __fastapi_cache_request,
|
|
|
|
|
"__fastapi_cache_response": __fastapi_cache_response,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-08-26 18:04:57 +08:00
|
|
|
@app.on_event("startup")
|
|
|
|
|
async def startup():
|
2022-02-24 10:07:33 -06:00
|
|
|
FastAPICache.init(InMemoryBackend())
|
2020-08-26 18:04:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-05-09 17:07:04 +01:00
|
|
|
uvicorn.run("main:app", reload=True)
|