fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🚀 [Feature]: Cache-Control based on query parameters/Force cache removing

Open bangbaew opened this issue 1 year ago • 4 comments

Feature Description

The Cache middleware documentation says that This middleware will cache the Body, Content-Type and StatusCode using the c.Path() as unique identifier., however, can I have the option to control the cache using c.Path() and c.Queries() as unique identifier? because when I try to query on the same path with different query parameters, it sends the same result every time I make a request, I want a customization to lower the caching level from c.Path() to c.Path() and c.Queries() together.

And it would be great if it has the ability to force removing the cache based on conditions, such as successful data mutation on POST/PUT/DELETE, the next GET request will return the new result immediately without having to add Cache-Control: no-cache header.

Additional Context (optional)

If I query with GET /transactions?page=1 and GET /transactions?page=2, they both will send the same cached result because the path is /transactions.

And I have to add Cache-Control: no-cache header in the GET after I update data, otherwise it will return the old result.

Code Snippet (optional)

app.Use(cache.New())

Checklist:

  • [X] I agree to follow Fiber's Code of Conduct.
  • [X] I have checked for existing issues that describe my suggestion prior to opening this one.
  • [X] I understand that improperly formatted feature requests may be closed without explanation.

bangbaew avatar Jul 03 '23 12:07 bangbaew

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

welcome[bot] avatar Jul 03 '23 12:07 welcome[bot]

https://docs.gofiber.io/api/middleware/cache#config image

you are able to define the key under which the cache will be used

i.e. you can also append the query parameters

ReneWerner87 avatar Jul 03 '23 12:07 ReneWerner87

https://docs.gofiber.io/api/middleware/cache#config image

you are able to define the key under which the cache will be used

i.e. you can also append the query parameters

Thanks for replying fast, I've changed from c.Path() to c.OriginalURL() and it works perfectly! my next question, is there a way I can remove the cache key from the server? I wanna remove a key after successful data update, so the next GET request will return the new result instead of the old one without having to make a request with Cache-Control: no-cache header from the client.

bangbaew avatar Jul 03 '23 13:07 bangbaew

unfortunately not

that would be worth a feature

create the possibility to invalidate the content of the cache for the middleware


you won't be able to remove your additional header though, because otherwise the browser won't get the new content

here we are talking about two different caching operations one is the browser cache at the client, which has to be invalidated again for this specific url and the other is the content which is in the cache storage, which should speed up the golang operations in the handler.

ReneWerner87 avatar Jul 03 '23 13:07 ReneWerner87