mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
33 lines
764 B
Python
33 lines
764 B
Python
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)
|