Output-caching should support custom cache invalidation through IOutputCachePolicy #59127
Open
Description
Is there an existing issue for this?
- I have searched the existing issues
Is your feature request related to a problem? Please describe the problem.
I was working to migrate from my old caching mechanism to Output Cache, but I was struggled on an issue. I was trying to revalidate the cache in IOutputCachePolicy.ServeFromCacheAsync because the document said that at that point the freshness of the cached response can be updated. What I found out is.
- There is no way to tell the framework from within IOutputCachePolicy.ServeFromCacheAsync that the cache is not fresh anymore. From my investigation, the only logic implemented in the middleware is just determining the age of the cache.
- Even if the earlier were possible, the cached response is not publicly accessible through OutputCacheContext. This makes it impossible to inspect from outside if the cached response is still fresh or not. For example, we at least need to inspect the cached response's Etag to determine the freshness.
Describe the solution you'd like
I would like to suggest the following solution.
- Allow IOutputCachePolicy to tell the framework that the cache is already invalid inside ServeFromCacheAsync. This could be a simple method added to OutputContext, e.g.
MarkCacheInvalid()
. - Allow IOutputCachePolicy to inspect cached response inside IOutputCachePolicy.ServeFromCacheAsync through OutputCacheContext
Additional context
Now it is possible by hacking through internal members by reflection, but I believe it should be implemented because right now there is not much we can do related to the cache in IOutputCachePolicy.ServeFromCacheAsync.