Reputation: 1611
I am implementing caching using MemoryCache (.net 4.0) for caching global data which will be used by all users of my website.
My initial approach: Store a KeyedCollection which would hold a collection of the Customer objects with a key to get a single object. This collection could have up to 250 such objects. Whenever the cache expires, I would rebuild the KeyedCollection and add it to the cache.
New approach Now, I am thinking why not store each Customer object directly to the cache with the customerid as the look-up key. Therefore, MemoryCache.Default would have upto 250 such Customer objects versus a single KeyedCollection. Benefits:
Any thoughts on using one versus the other in terms of performance and other factors?
Upvotes: 3
Views: 3176
Reputation: 4059
The solution will depend on how often you need to work on the objects as a collection.
Reasons for storing as a collection:
CacheItemPolicy
. This case is probably unlikely,
however. KeyValuePair<string,
object>
).Reasons for storing individually:
So, compare your likely usage scenario, and choose accordingly. Chances are, unless you are writing lots of .Where
, .Select
, etc, calls or have reason to pass around the whole collection, then storing individually is going to be the better choice.
Upvotes: 3