Skip to content

Output-caching should support custom cache invalidation through IOutputCachePolicy #59127

Open
@tiakun

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.

  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 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.

Metadata

Assignees

No one assigned

    Labels

    Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middleswarefeature-output-caching

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions