Asp.net 7 Output Caching - Cache by route, with other routes evicting the cache

Alex Winfield 1 Reputation point
2022-10-28T17:52:12.457+00:00

I've watched this video: https://learn.microsoft.com/en-us/events/build-2022/od114-output-caching-in-asp-net-core-7
...and I've looked through the middleware sample project: https://github.com/dotnet/aspnetcore/tree/main/src/Middleware/OutputCaching/samples/OutputCachingSample
...and I still have some questions I'm hoping to get some help with.

If I have an endpoint such as GET /widget/{id}, I don't need any special config options in order for each widget to be cached separately.
But what I don't see, is a way for a POST /widget/{id} to invalidate the cache for just the widget that's updated. I can use tags and invalidate all caches for all widgets, but is there a way to invalidate only the cached item that actually changed?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,400 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2022-10-28T22:07:31.587+00:00

    typically you don't cache posts. the post body is not part of routing, so it can not be used to effect the cache. caching is controlled by the url and headers.

    a more typical solution is the post/redirect/get. in your case you would tag by widget id, and the post would invalidate the widget tag and redirect to the get. this would also cause the subisquent gets to use get built from the new post data.