Exploring the new output caching middleware #222
Replies: 12 comments
-
If you want a reason to use the vary by value caching option, then I can think that extracting a claim from the httpcontext would suit this use-case. You can then vary by user, if you have logged-on users, or perhaps other data that can be extracted from the httpcontext such as request or session data. whether these are "good" reasons depends on your needs. It doesn;t have as fine-grained control over evicting cache as it does from adding it, If you cache by an ID parameter, you cannot evict only that ID's cache, its an all-or-nothing based on large tag names. |
Beta Was this translation helpful? Give feedback.
-
Well done, but as most people still use Controllers it would be good to have more infos regarding that. For example how to use |
Beta Was this translation helpful? Give feedback.
-
@AndiRudi thanks. [OutputCache(Duration = 10_000, VaryByQueryKeys = new [] { "id" })] Sadly, as far as I know, this doesn't support tags. |
Beta Was this translation helpful? Give feedback.
-
Hi Tim, |
Beta Was this translation helpful? Give feedback.
-
If I am not mistaken, Output Caching does not work with an authorization header. When I started trying it out it took me quite a while to figure this out... |
Beta Was this translation helpful? Give feedback.
-
@dimigithub Good question, how does the implementation look like? For comparison, I copy-pasted the default policy and removed the header check and it seems to be working. I added the policy to the base policies with: builder.Services.AddOutputCache(options =>
{
options.AddBasePolicy(new MyPolicy());
}); Now, for every endpoint that doesn't use Routes that use app.MapGet("/cache-test", () => ...).CacheOutput(); For me, this makes sense because you don't want to cache all endpoints. |
Beta Was this translation helpful? Give feedback.
-
Hi Tim,
And I misunderstood the principle: I thought that routes with the cachedouput attribute were cached. It is indeed true that routes without the CacheOutput attribuut, are cached. |
Beta Was this translation helpful? Give feedback.
-
Yea, that's possible @dimigithub |
Beta Was this translation helpful? Give feedback.
-
Fantastic article thanks! I didn't have any success with adding the base policy, I still couldn't cache authorized requests. For me what worked was using excludeDefaultPolicy: true like this
combined with
Where AllowAuthorizationCachePolicy was just the default policy with the authorization checks removed. |
Beta Was this translation helpful? Give feedback.
-
Here's another way you could use tags with controller attributes: Given two endpoints:
and
You can do this...
|
Beta Was this translation helpful? Give feedback.
-
Cool, thanks @AdamDiament ! |
Beta Was this translation helpful? Give feedback.
-
@gbjbaanb you can actually achieve fine grained control over evicting when using varybyvalue, for example: In the setup:
Then in the custom AuthorizedCacheByByClaimPolicy CacheRequestAsync method itself you can set tags per execution. So you can have a method which takes the httpcontext and returns a tag - you could interrogate the claims, and return a tag based on the same claim type, then evict only that, for example:
|
Beta Was this translation helpful? Give feedback.
-
Exploring the new output caching middleware - Tim Deschryver
Taking a closer look at the capability of the new output caching middleware in .NET 7
https://timdeschryver.dev/blog/exploring-the-new-output-caching-middleware
Beta Was this translation helpful? Give feedback.
All reactions