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

Advanced options for cache key (headers, custom properties, ...) #3

Open
muety opened this issue Aug 3, 2022 · 0 comments · May be fixed by #6
Open

Advanced options for cache key (headers, custom properties, ...) #3

muety opened this issue Aug 3, 2022 · 0 comments · May be fixed by #6

Comments

@muety
Copy link

muety commented Aug 3, 2022

First of all, thanks a lot for this project, it's basically exactly what I was looking for! 🙌

However, I encountered some limitations quite soon. I'd like to have more options for configuring the keys by which objects are cached.

My use case at hand is to cache user-specific responses, so that for authenticated requests to the same route, different content is returned for different users. I see two possible solutions to this:

  1. Consider request headers in cache key. Some (-> make this configurable) headers could be included to the key when writing to or reading from the cache, e.g. the Authorization header's contents.
  2. Add user-defined key elements. A more generic solution is to allow the developer to specify custom values to consider as part of the cache key. Something like this:
@app.get("/products", response_model=List[Product])
async def list_products(
        rcache: ResponseCache = cache_manager.from_request(),
        current_user: models.User = Depends(deps.principal),
):
    if rcache.exists(props={'user': current_user.id}):
        return rcache.data

    products = await db.fetch_products()

    await rcache.set(products, tag="all-products", props={'user': current_user.id})
    return products
@muety muety linked a pull request Aug 8, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant