title: asp.net static caching in hashtable
body: <p>I was previously using the application object to cache data that never changes. I am rewriting the project and have found out the aplication object is a no-no and is just there for legacy support from clasic ASP.</p>
  
  <p>I know I can also use the cache - but I don't want to as I use that for data that needs to be invalidated.</p>
  
  <p>I am therefore looking at static variables for static data (makes sense).</p>
  
  <p>My question is, rather than specifying static variables in each class, I was thinking of using a hashtable to store all the data and wrapping it into its own class - like a factory.</p>
  
  <p>Something like this:</p>
  
  <pre><code>''' &lt;summary&gt;
  ''' Methods and properties related to the access and management of the local static memory cache.
  ''' Fastest type of cache but not available across applications, web farm or web garden environments.
  ''' Use this cache when data is static or can be stale across application instances.
  ''' &lt;/summary&gt;
  ''' &lt;remarks&gt;&lt;/remarks&gt;
  Public Class LocalStaticCache
  
      'Internal data holder:
      Private Shared _objCache As Hashtable = Hashtable.Synchronized(New Hashtable)
  
      Private Sub New()
      End Sub
  
      ''' &lt;summary&gt;
      ''' Gets or sets an object in cache. Returns Nothing if the object does not exist.
      ''' &lt;/summary&gt;
      ''' &lt;param name="key"&gt;The name of the object.&lt;/param&gt;
      ''' &lt;returns&gt;&lt;/returns&gt;
      ''' &lt;remarks&gt;&lt;/remarks&gt;
      Public Shared Property Item(key As String) As Object
          Get
              If String.IsNullOrEmpty(key) Then Return Nothing
              Return _objCache(key)
          End Get
          Private Set(value As Object)
              _objCache(key) = value
          End Set
      End Property
  
      ''' &lt;summary&gt;
      ''' Insert an object into the cache.
      ''' &lt;/summary&gt;
      ''' &lt;param name="key"&gt;The unique object key.&lt;/param&gt;
      ''' &lt;param name="value"&gt;The object to store in the cache.&lt;/param&gt;
      ''' &lt;remarks&gt;&lt;/remarks&gt;
      Public Shared Sub Insert(key As String,
                               value As Object)
          If Not String.IsNullOrWhiteSpace(key) Then
  
              If _objCache.ContainsKey(key) Then
                  'If the key already exists in the Cache it will overwrite only if the objects differ:
                  Interlocked.CompareExchange(Item(key), value, value)
                  Return
              End If
  
              'store the item to the cache:
              Item(key) = value
          End If
      End Sub
  
      ''' &lt;summary&gt;
      ''' Remove an object from the cache.
      ''' &lt;/summary&gt;
      ''' &lt;param name="key"&gt;The key of the object to remove.&lt;/param&gt;
      ''' &lt;remarks&gt;&lt;/remarks&gt;
      Public Shared Sub Remove(key As String)
          If _objCache.ContainsKey(key) Then
              _objCache.Remove(key)
          End If
      End Sub
  
  End Class
  </code></pre>
  
  <p>Do you think storing all static data into a hashtable is this a good idea for performance, 
  or would it be better for each class to have its own static data holders inside their class?</p>
  
  <p><strong>Is this thread safe</strong>? Note: I am implementing Hashtable.Synchronized and Interlocked.CompareExchange to prevent race conditions - <strong>but what about locking and contention</strong>?</p>
  
  <p>Note, the data is never changed once it has been set the first time round (the items in the hashtable never need updating).</p>
  
  <p>I have datasets and large chunks of record sets as memory streams to store.</p>
  
  <p>Any thoughts or pointers?</p>
  
  <p>thanks.</p>