diff --git a/examples/in_memory/__init__.py b/examples/in_memory/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/examples/in_memory/main.py b/examples/in_memory/main.py new file mode 100644 index 0000000..78dd965 --- /dev/null +++ b/examples/in_memory/main.py @@ -0,0 +1,53 @@ +from datetime import date, datetime + +import uvicorn +from fastapi import FastAPI +from starlette.requests import Request +from starlette.responses import 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=20) +async def index(request: Request, response: Response): + 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=20) +async def get_data(request: Request, response: Response): + return date.today() + + +@app.get("/datetime") +@cache(namespace="test", expire=20) +async def get_datetime(request: Request, response: Response): + return datetime.now() + + +@app.on_event("startup") +async def startup(): + FastAPICache.init(InMemoryBackend()) + + +if __name__ == "__main__": + uvicorn.run("main:app", debug=True, reload=True) diff --git a/examples/redis/__init__.py b/examples/redis/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/examples/main.py b/examples/redis/main.py similarity index 100% rename from examples/main.py rename to examples/redis/main.py