Thread: Caching
View Single Post
  #3 (permalink)  
Old May 29th, 2010, 03:04 AM
chroniclemaster1 chroniclemaster1 is offline
Friend of Wrox
Points: 1,749, Level: 16
Points: 1,749, Level: 16 Points: 1,749, Level: 16 Points: 1,749, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2007
Location: San Diego, CA, USA.
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

Cool. So as long as the application isn't recycled, you can fetch "whatever" from the Cache object days later?

One other related question. I found one of the code samples I was remembering. Does it become part of the application state as well, and can it be fetched days later (again provided the application isn't recycled in between)?

If so, I'm wondering if the magic happens because the Dictionary is a static field?

Code:
class MyCache<K, V> {
   private static Dictionary<K, V> _objectCache;

   public MyCache() {
      MyCache<K, V>._objectCache = new Dictionary<K, V>();
   }

   private V findValueInDB(K key) {
      // code to retrieve value from external resource
   }

   public V lookupValue(K key) {
      V retVal;
      if (_objectCache.ContainsKey(key) == true) {
         __objectCache.TryGetValue(key, out retVal);
      } else {
         retVal = findValueInDB(key);
      }
      return retVal;
   }
}
__________________
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.