mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-24 20:47:54 +00:00
- Fix redis cache.
- Encode key builder.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import aioredis
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
from starlette.requests import Request
|
||||
@@ -5,6 +6,7 @@ from starlette.responses import Response
|
||||
|
||||
from fastapi_cache import FastAPICache
|
||||
from fastapi_cache.backends.inmemory import InMemoryBackend
|
||||
from fastapi_cache.backends.redis import RedisBackend
|
||||
from fastapi_cache.decorator import cache
|
||||
|
||||
app = FastAPI()
|
||||
@@ -20,7 +22,7 @@ async def get_ret():
|
||||
|
||||
|
||||
@app.get("/")
|
||||
@cache(namespace="test", expire=2)
|
||||
@cache(namespace="test", expire=20)
|
||||
async def index(request: Request, response: Response):
|
||||
return dict(ret=await get_ret())
|
||||
|
||||
@@ -32,7 +34,8 @@ async def clear():
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache")
|
||||
redis = aioredis.from_url(url="redis://localhost")
|
||||
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user