Overview
FastAPI-Cache 0.2.0, released on January 11, 2023, adds Redis and Memcached backends for caching FastAPI responses.
Main Features
Redis and Memcached
The @cache() decorator caches responses with a configurable TTL via Redis or Memcached.
python
from fastapi import FastAPI
from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
from fastapi_cache.decorator import cache
from redis import asyncio as aioredis
app = FastAPI()
@app.on_event('startup')
async def startup():
redis = aioredis.from_url('redis://localhost')
FastAPICache.init(RedisBackend(redis), prefix='cache')
@app.get('/data')
@cache(expire=60)
async def get_data():
return {'result': 'cached for 60s'}
