I am caching using MemoryCache (.net 4.0) to cache global data that will be used by all users of my website.
My initial approach:
Save the KeyedCollection, which will store a collection of Customer objects with a key to get one object. This collection can contain up to 250 of such objects. Whenever the cache expires, I rebuild the KeyedCollection and add it to the cache.
New approach
Now I think, why not store each Customer object directly in the cache using customerid as a keyword. Therefore, MemoryCache.Default will have up to 250 such Customer objects compared to a single KeyedCollection. Benefits:
- More efficiently, as I get the Customer object directly from the cache without having to do another search in the Keyed Collection.
- I would add a new Customer object to the cache only when it is requested for the first time. A sort of lazy addition as opposed to pre-creating the entire cache.
Any thoughts on using one and the other in terms of performance and other factors?
source
share