http-cache icon indicating copy to clipboard operation
http-cache copied to clipboard

Clear-Site-Data header

Open marcodpt opened this issue 2 years ago • 12 comments

I am wanting to use this library in a reverse proxy.

However, I would like GET requests to be cached without expiring until I receive a POST request for the same origin.

I can control the HTTP headers of each server without any problems.

However, to implement this logic, I needed Clear-Site-Data to be taken into account in the cache system.

Would you know if it is possible to do what I would like and if there is any chance Clear-Site-Data will be implemented?

marcodpt avatar Oct 22 '23 19:10 marcodpt

By default when a request is made to an endpoint that is not GET or HEAD method it should delete the cache record for that endpoint should it exist, so I think that part should be covered unless I'm missing something. If you wanted to ignore the caching rules you could set the mode to IgnoreRules, which would ignore all the staleness logic and leave you in control of expiry.

As for the header based logic, I think you may be able to use a feature that was just added in PR #53 by @veeshi to accomplish this once a new release has been made. I think you could construct your client with a cache_mode_fn in HttpCacheOptions that handles the checks you need and changes the mode based on certain conditions.

I have the new feature merged into develop and hoping to have some time soon to cut a new release.

06chaynes avatar Oct 23 '23 19:10 06chaynes

My problem is that the database has triggers and related data, for example when inserting an item into an order, say POST /api/items, the GET /api/orders cache needs to be cleared too.

So any POST request in localhost:8000 should clear all endpoints cache associated with this origin (but not at localhost:9000)

This is my use case in a reverse proxy

marcodpt avatar Oct 24 '23 01:10 marcodpt

Would it be possible for the logic that is making the request to interface with the cache manager directly? If so you could have it remove related records.

06chaynes avatar Oct 24 '23 13:10 06chaynes

I'll try, but anyway, thank you very much for your attention and your excellent work on this library.

marcodpt avatar Oct 24 '23 13:10 marcodpt

This caught me eye and it would be a useful feature to add support for a cache busting closure.

So the closure takes:

  • http::request::Parts
  • cache key closure, user provided one or the default if none provided.

It returns a list of keys for items to delete from the cache.

Users of the library can then either return, an empty list and no entries are deleted, or compute the default key or add additional keys and add matching logic based on the request.

veeshi avatar Oct 24 '23 21:10 veeshi

This caught me eye and it would be a useful feature to add support for a cache busting closure.

So the closure takes:

  • http::request::Parts

  • cache key closure, user provided one or the default if none provided.

It returns a list of keys for items to delete from the cache.

Users of the library can then either return, an empty list and no entries are deleted, or compute the default key or add additional keys and add matching logic based on the request.

I think I can see how this might work but not sure exactly where it would bolt in, I'm open to exploring it though.

06chaynes avatar Oct 25 '23 16:10 06chaynes

I'll try putting together a PR if I get time this weekend to see what it could look like.

veeshi avatar Oct 25 '23 17:10 veeshi

I've jotted up an initial idea, it generates the keys to bust in the cache first and removes them before continuing with the actual request. Not sure if that is the ideal way to do it.

veeshi avatar Oct 26 '23 22:10 veeshi

Just published new versions of the crates which should enable you to add the logic you desired, via new cache_bust field in the HttpCacheOptions struct.

06chaynes avatar Nov 01 '23 22:11 06chaynes

Thank you very much, I'll try to write the cache and give you feedback, it will take a few days, but once I've done it I'll get back to you.

marcodpt avatar Nov 02 '23 17:11 marcodpt

Sweet, looking forward to seeing how it turns out!

06chaynes avatar Nov 02 '23 18:11 06chaynes