Output-caching should support custom cache invalidation through IOutputCachePolicy
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
### Is there an existing issue for this?
- [x] 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.
1. 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](https://github.com/dotnet/aspnetcore/blob/0a5f4deafc371a78e89eea7bfaa615404c52cd6a/src/Middleware/OutputCaching/src/OutputCacheMiddleware.cs#L270) is just determining the age of the cache.
2. 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.
1. 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()`.
2. 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.
Contributor guide
Assessment
This issue has not been assessed yet.