mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
Merge pull request #55 from RyanTruran/documentation
added InMemory example
This commit is contained in:
0
examples/in_memory/__init__.py
Normal file
0
examples/in_memory/__init__.py
Normal file
53
examples/in_memory/main.py
Normal file
53
examples/in_memory/main.py
Normal file
@@ -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)
|
||||||
0
examples/redis/__init__.py
Normal file
0
examples/redis/__init__.py
Normal file
Reference in New Issue
Block a user