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

[Feature Request]: hono/cache cacheName should also take function #2650

Closed
MathurAditya724 opened this issue May 9, 2024 · 3 comments · Fixed by #2652
Closed

[Feature Request]: hono/cache cacheName should also take function #2650

MathurAditya724 opened this issue May 9, 2024 · 3 comments · Fixed by #2652
Labels
enhancement New feature or request.

Comments

@MathurAditya724
Copy link
Contributor

What is the feature you are proposing?

In the hono/cache middleware, how can we add query params or other variables from the context? Like caching the data based on different limit parameters or user profiles/roles.

I checked the code and it's a simple implementation, I can create a PR for this 😀

@MathurAditya724 MathurAditya724 added the enhancement New feature or request. label May 9, 2024
@ColeTownsend
Copy link

Echoing this! It seems like using the cache in conjunction with Cloudflare gets around this, but I imagine it would be helpful for other deployment locations where that isn't the case. It would also be more explicit as to what's happening.

@yusukebe
Copy link
Member

@MathurAditya724

Nice! Like it.

I checked the code and it's a simple implementation, I can create a PR for this 😀

Please!

@MathurAditya724
Copy link
Contributor Author

Echoing this! It seems like using the cache in conjunction with Cloudflare gets around this, but I imagine it would be helpful for other deployment locations where that isn't the case. It would also be more explicit as to what's happening.

While going through the code in more detail, I figured out why that was happening. Basically cacheName is a unique name for the collection of key-value stores and the URL (c.req.url) is used as the key to store those values. So as long as the values you want to use for the cache keys are in the URL, It is straightforward.

But in scenarios where you want to cache data conditionally like when certain parameters are present in the URL or the header, you will have to wrap a custom middleware around the cache.

@yusukebe I would suggest we have a keyGenerator function, which has a default value of (c) => c.req.url and if the returned key is undefined or null we will not cache it. Through this others can define their custom generator to tackle these issues in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants