fastapi-cache
fastapi-cache copied to clipboard
Help: Quickstart example not working
Versions:
fastapi-cache2==0.2.2
fastapi-cache2[redis]==0.2.2
.venv ❯ python --version
Python 3.12.4
Redis:
docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:7.4.0-v0
Code:
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
import time
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
from redis import asyncio as aioredis
import uvicorn
@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
redis = aioredis.from_url("redis://0.0.0.0")
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
yield
app = FastAPI(lifespan=lifespan)
@cache()
async def get_cache():
return 1
@app.get("/")
@cache(expire=10)
async def index(request: Request):
return time.time()
if __name__ == "__main__":
uvicorn.run(app, port=8000)
https://github.com/user-attachments/assets/2954d26a-b64a-40c5-ad8e-75c70f307138
Issue:: Every time I hit http://0.0.0.0:8000/ I get a new value of time, even within 10 seconds