cashews icon indicating copy to clipboard operation
cashews copied to clipboard

cache_control_ttl is undocumented

Open viktorfreiman opened this issue 1 year ago • 1 comments

I have found that cache_control_ttl has no documentation and it's unclear on how and when to use it.

viktorfreiman avatar Jan 31 '24 15:01 viktorfreiman

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) 

    

Krukov avatar Feb 04 '24 19:02 Krukov