Krukov / cashews

Cache with async power
MIT License
371 stars 22 forks source link

cache_control_ttl is undocumented #198

Open viktorfreiman opened 5 months ago

viktorfreiman commented 5 months ago

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

Krukov commented 5 months ago

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)