I'm posing a question to the community about which one is better, adding lots of keys into the Page.Cache all with the same CacheDependency set to the same file, OR would you think it's better to cache one dictionary or list structure with the single instance of the CacheDependency?
// set lots of keys with same cachedependency file...
Page.Cache.Add(”key1”, key1object, new CacheDependency(”theFile.txt”));
Page.Cache.Add(”key2”, key2object, new CacheDependency(”theFile.txt”));
Page.Cache.Add(”key3”, key3object, new CacheDependency(”theFile.txt”));
Page.Cache.Add(”key4”, key4object, new CacheDependency(”theFile.txt”));
or another way:
// set one single page.cache value with a single cachedependency...
IDictionary myDictionary = new Dictionary();
myDictionary.Add(”myDictionary.key1”, key1object);
myDictionary.Add(”myDictionary.key2”, key2object);
myDictionary.Add(”myDictionary.key3”, key3object);
myDictionary.Add(”myDictionary.key4”, key4object);
Page.Cache.Add(”MyDictionary”,myDictionary,new CacheDependency(”theFile.txt”));
Of course, getting a value from the IDictionary instance is a little more difficult:
IDictionary myDictionary = (IDictionary) Page.Cache[”MyDictionary”];
if(myDictionary==null) ReinitMyDictionary();
object someValue = myDictionary[”key1”];
versus just pulling directly from Page.Cache... (and remember that we still would have to cast to the specific object)
object someValue = (object) Page.Cache[”myDictionary.key1”];
if(someValue==null) { //either it wasn't found, or was removed... here's the big difference between the two implementations
ReinitMyDictionary();
It seems to be a little cleaner to put the IDictionary instance into Page.Cache, then you know for sure if you have to reload the collection, versus the direct Page.Cache method might be a little more efficient but is somewhat less precise as to what to do when you get a null... Obviously you could do an onRemovedCallback handler to ReinitMyDictionary() and somewhat guarantee for the second way that that key just plain wasn't in the list of keys loaded from, say theFile.txt...
The goal of the algorithm is simply to load theFile.txt's key/value pairs, and if the key isnt found, its just ignored.
I'd like your feedback to see what others would do for this... and what the performance implications are... (multiple casts, versus a single one, etc)