Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache_control_ttl is undocumented #198

Open
viktorfreiman opened this issue Jan 31, 2024 · 1 comment
Open

cache_control_ttl is undocumented #198

viktorfreiman opened this issue Jan 31, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@viktorfreiman
Copy link

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

@Krukov
Copy link
Owner

Krukov commented Feb 4, 2024

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 Krukov added the documentation Improvements or additions to documentation label Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants