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

Need a way to invalidate a key/reset cache when updates are made to the cached resources.

Open kumarom opened this issue 1 year ago • 4 comments

Need a way to invalidate a key/reset cache when updates are made to the cached resources.

For eg: if the resource is cached, and one of the resource properties is updated, we need to update the resource in the cache. Otherwise, the changes do not get reflected.

I am unsure if this is already possible, but if it is, an example of how to do this would greatly help.

kumarom avatar Nov 10 '23 06:11 kumarom

Maybe you can use the CacheStore.Delete or CacheStore.Set directly? https://github.com/chenyahui/gin-cache/blob/main/persist/cache.go#L20

chenyahui avatar Nov 12 '23 08:11 chenyahui

Maybe you can use the CacheStore.Delete or CacheStore.Set directly? https://github.com/chenyahui/gin-cache/blob/main/persist/cache.go#L20

Yes, For now, I have done like that.

It would be better to expose the default CacheStrategy so that it can be used for this purpose. Another option is to implement a custom CacheStrategy. But it is not that simple. This means one has to implement/duplicate CacheKey generator function as well.

It would be nice to expose these functions so that consumer code can use them to generate the CacheKeys and invoke CacheStore.Delete or CacheStore.Set as you suggested. This avoids duplication of default behavior.

kumarom avatar Nov 13 '23 04:11 kumarom

That is indeed a good suggestion, and I will consider implementing it as soon as possible. If you have the time, feel free to submit a pull request as well.

chenyahui avatar Nov 16 '23 15:11 chenyahui

@chenyahui Thank you. Looking forward to the implementation.

kumarom avatar Nov 20 '23 07:11 kumarom