Код: Выделить всё
async def init_cache():
try:
redis = aioredis.from_url( # type: ignore
appConfig.REDIS_ENDPOINT,
encoding='utf-8',
decode_responses=False,
)
FastAPICache.init(RedisBackend(redis), prefix='igw-cache')
logging.info('Cache initialized successfully.')
except Exception as e:
logging.error(f'Error initializing cache: {e}')
@asynccontextmanager
async def lifespan(_app: FastAPI) -> AsyncIterator[None]:
await init_cache()
yield
Код: Выделить всё
app = FastAPI(lifespan=lifespan, docs_url=docs_url)
@router.get("/test-cache")
@cache(expire=60)
async def index(request: Request, response: Response):
logging.info('The endpoint executed...')
return dict(hello="world")
Подробнее здесь: https://stackoverflow.com/questions/790 ... pplication
Мобильная версия