Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
tiakun opened this issue Nov 24, 2024 · 2 comments
Open
1 task done
Labels
area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlesware feature-output-caching Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.

Comments

@tiakun
Copy link

tiakun commented Nov 24, 2024

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.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlesware label Nov 24, 2024
@captainsafia
Copy link
Member

@tiakun Thanks for filling this issue!

It seems like you'd like to propose some new API to solve the problem that you're running into here.

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.

Can you share an example of the code you wish you could write with the missing APIs to solve this problem?

@captainsafia captainsafia added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Nov 25, 2024
@tiakun
Copy link
Author

tiakun commented Nov 26, 2024

@tiakun Thanks for filling this issue!

It seems like you'd like to propose some new API to solve the problem that you're running into here.

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.

Can you share an example of the code you wish you could write with the missing APIs to solve this problem?

My pleasure. Here is an example of IOutputCachePolicy I would like to write

class MyOutputCachePolicy : IOutputCachePolicy 
{
     public async ValueTask ServeFromCacheAsync(OutputCacheContext context, CancellationToken cancellation)
     {
         var cachedEtag = context.GetCachedResponseEtag();
         // IsFresh here is my own method to check if the cache is fresh or not
         if (!await IsFreshAsync(context, cachedEtag)) 
         {
              context.MarkCachedResponseInvalid();
         }
     }
     ...    
}

From the example, GetCachedResponseEtag returns cached etag and MarkCachedResponseInvalid flag the cached response to be stale, effectively prevent the middleware to return the cache and allow the middleware to evict the cache.

I'm not sure if I should have used an API proposal template instead. I could do it if it is more appropriate.

@dotnet-policy-service dotnet-policy-service bot added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlesware feature-output-caching Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.
Projects
None yet
Development

No branches or pull requests

3 participants