mythz very quickly answered my original question on SO (link below in case others are interested), but I had a follow up question that’s more specific.
I am trying access the cacheInfo for a request in a GlobalRequestFilter, but it is always null. In the code below, ci is always null. The same code works as a response filter, but does not solve my original problem (presumably because the incorrect cache key has already been looked up in the cache).
this.GlobalRequestFilters.Add((req, res, responseDto) =>
{
var ci = (CacheInfo)req.GetItem(Keywords.CacheInfo);
});
Original question: regarding how to change the cache key of the url-based CacheControl attribute- :
My global request filter is executing, and my modified cache key is getting written into the cache (I’m removing the timestamp from the url), but the Factory method is still being called for the service every time (the cache isn’t being used).
What factory method? the GlobalRequestFilters? yes they’re executed for every Service Request so CacheInfo is only going to be populated for requests using a Request Filter Attribute which populates them like [CacheResponse].
Incidentally, I am using this same code to dynamically change my cache expiry time, something I had thought I had lost when I switched from .ToOptimizedResult() to the more declarative CacheResponse. I have a special cache duration that’s something like “if it’s before noon, cache 10 minutes, and if it’s after noon, cache the rest of the day”.
To enable this, I use a special duration of 59 seconds in my cacheresponse (behind a constant).