mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-24 20:47:54 +00:00
first commit
This commit is contained in:
0
examples/__init__.py
Normal file
0
examples/__init__.py
Normal file
32
examples/main.py
Normal file
32
examples/main.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import aioredis
|
||||
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.redis import RedisBackend
|
||||
from fastapi_cache.decorator import cache, cache_response
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@cache()
|
||||
async def get_cache():
|
||||
return 1
|
||||
|
||||
|
||||
@app.get("/")
|
||||
@cache_response(expire=60)
|
||||
async def index(request: Request, response: Response):
|
||||
return dict(hello="world")
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
redis = await aioredis.create_redis_pool("redis://localhost", encoding="utf8")
|
||||
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run("main:app", debug=True, reload=True)
|
||||
Reference in New Issue
Block a user