Project is MVC WebAPI based.
We are passing permission context of a client to our API servers as a serialized JSON object in the claims headers of the request. This is not a huge object: 6 properties and one collection of enum-based key-value pairs (up to 6 items here)
Vast majority of the requests to API occur every minute (some more frequently) from the same set of clients. Probably 700-900 clients (and growing), each one sending the same claims over and over, every minute.
For every request, various components of the code deserialize this object probably 5-6 times. This deserialization causes significant CPU drain on the servers.
What would be the best way to cache these deserializations in memory? Would a static Dictionary object with keys being serialized JSON strings, work well or would searching thru it be too slow, as these strings would be decently large in size?
EDIT: Every Action of every controller gets filtered thru this attribute to ensure that calls have proper permissions
public class AccountResolveAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext context)
{
var controller = (ControllerBase) context.ControllerContext.Controller;
var identity = (ClaimsIdentity) controller.User.Identity;
var users = identity.Claims
.Where(c => c.Type == ClaimTypes.UserData.ToString())
.Select(c => JsonConvert.DeserializeObject<UserInformation>(c.Value))
.ToList();
var accountId = controller.ReadAccountIdFromHeader();
if (users.All(u => u.AccountId != accountId))
{
throw new ApplicationException(string.Format("You have no rights for viewing of information on an account Id={0}", accountId));
}
}
}
There are also calls in the base controller that interrogate the claims, but AccountResolve could probably cache the result of the first deserialization into the controller so that those calls do not try to deserialize again. However, the claims are the same over and over again, and I'm just trying to find a way to optimize to not deserialize again and again the same string. I've tried caching the serialization string as a key and result object into memory in a global static ConcurrentDictionary, but it doesn't appear to have helped