Reputation: 8763
So I just inherited an ASP.NET MVC app...
The app caches user permissions in the application cache, which is fine, except for the fact that it caches all of the permissions under a single key. It's basically one big 50MB dictionary object with a bunch of nested dictionaries within it.
On each request the 50MB object is accessed like so:
public bool HasAccess(string user, string permission)
{
var cache = (GodCache)MemoryCache.Default["GodCache"]); //I am huge
return cache[user][permission];
}
What's the best case that can be made for why this is could be a bad idea?
Upvotes: 1
Views: 1257
Reputation: 6693
In general, I think it is better to break it up into smaller objects than have 1 huge object in the cache. Under memory pressure, the cache items will be evicted. If you have one large object graph as a single item in the cache, then that whole object is going to be evicted and you have to re-construct the whole object graph again which will take a lot of time and resources.
Instead, if the cache is broken down into smaller key-value pairs, then only some of the items are going to get evicted and presumably it will take much less time and resources to re-construct that single item.
Also, if the cache is going to become an out-of-process one (like redis), there is the added overhead of seralization/deserialization of the object graph and under heavy/concurrent load it could really consume a lot of CPU cycles for a huge object.
Upvotes: 2