cashews
cashews copied to clipboard
cache_control_ttl is undocumented
I have found that cache_control_ttl has no documentation and it's unclear on how and when to use it.
Hello,
It is a pretty new feature - a part of contrib part for fastapi ( obvious )
cache_control_ttl
works only with CacheRequestControlMiddleware
setup. When request comes with Cache-Control
header the value of max-age
stored in context variable and can be used as ttl for cache by cache_control_ttl
.
app = FastAPI()
app.add_middleware(CacheRequestControlMiddleware)
cache.setup(os.environ.get("CACHE_URI", "redis://"))
@app.get("/")
@cache(ttl=cache_control_ttl(default="1m"), key="simple:{user_agent:hash}")
async def simple(user_agent: str = Header("No")):
...
# HTTP GET /
# Cache-Control: private, max-age=300
# Cashews will cache response for 5 min (300 sec)