Extend CacheResponse feature with a VaryByRoles option
A number of our services have responses that vary by the set of roles a user is associated with. It would be more memory efficient if we could cache by roles rather than by user for these services.

I’ve added support for VaryByRoles in this commit: https://github.com/ServiceStack/ServiceStack/commit/bbd3eda601b4a373d81bbed593bc2b743dbcf6f6
You need to be explicit in which roles the cache should be varied by, e.g:
[CacheResponse(Duration = 60, VaryByRoles = new[] { “RoleA”, “RoleB” })]
Where there will be separate caches created for Users in “RoleA” vs “RoleB” vs No Role/UnAuthenticated Users.
This is available from v4.0.55 that’s now on MyGet
-
Can't do that, the only API we can rely on is HasRole which is why we need a predetermined list of Roles that the response can be customised to. The forums includes an example of how you can customise the cache key used.
-
George Hemmings commented
Thanks for this, Demis.
In my particular case Roles are only known at runtime, as they can be defined in the app. I was thinking VaryByRoles = true would add all the users roles to the cache key. I get that the way it's been implemented is more flexible and fits in better with the auth attributes.
Could you possibly add a hook for adding additional key modifiers?