CacheResponse Filter - support for VaryByHeader?

I cannot tell whether the [CacheResponse] filter handles for Headers or not.
In some endpoints that I would like to add caching, I check for the existence of a custom header that I need to use to vary the cache output, not just querystrings.

Is this possible with [CacheResponse] or do I need to create my own CacheKey (including my headers) to do this effectively?


It only supports VaryByUser or VaryByRoles which will create unique caches per user or per specified role. If that’s not suitable you’ll need to construct your own Cache Key. Can you explain your exact use-case a bit better so we can tell if it’s something that’s common enough scenario to support?

Our APIs are behind a proxy (Azure API Management) that handles authentication / user management so there are no Roles or Users used in the API currently.

We have a few business rules where some of the APIs return a subset of data based on purchased access levels.
We have configured API Management to pass the Access Level in the header (CustomerType - Full / Limited / etc) so our API knows what level of data to return. Perhaps this is bad design, and we should have different Endpoints specified for CustomerTypes but we wanted to keep the number of endpoints down and the logic consolidated as these CustomerTypes could grow.

Anyway, we can modify API Management to pass the Access Level in the querystring instead if there is no easy way to differentiate cached responses based on certain Headers in the request.

On another note, how are the Content-Type headers being handled? IE: XML versus JSON so that a client gets the correct response format from cache.

BTW - We really love SS. It has changed the way we think about APIs forever.


Ok no worries, I’ve added support for VaryByHeaders in this commit where you should now be able to create different caches based on Custom Headers with:

[CacheResponse(VaryByHeaders = new [] { "X-Custom" })]

This change is available from v4.5.13 that’s not available on MyGet.

We create different caches for different response formats by default.

Awesome, that’s the goal :smile: glad you’re finding it useful!