title: Azure Appfabric Caching + WCF Webservices
body: <p>I have a series of WCF Web services hosted in Windows Azure and I am trying to implement appfabric caching.</p>
  
  <p>I am struggling with stateless nature of web services and the need to avoid the expensive initialisation of datacachefactory and datacache objects.</p>
  
  <p>I have wrapped my datacachefactory in a singleton as this seemed to be a good place to start.....</p>
  
  <pre><code>Imports Microsoft.ApplicationServer.Caching
  Public Class Cache
  
      Private Shared _DataCacheFactory As DataCacheFactory
      Private Shared _DataCache As Microsoft.ApplicationServer.Caching.DataCache
  
      Private Sub New()
  
      End Sub
  
  
      Shared ReadOnly Property DataCacheFactory As DataCacheFactory
          Get
              If IsNothing(_DataCacheFactory) Then
  
                  Dim localTimeout As New TimeSpan(0, 10, 0)
                  Dim localCacheConfig As New DataCacheLocalCacheProperties(10000, localTimeout, DataCacheLocalCacheInvalidationPolicy.TimeoutBased)
                  Dim factoryConfig As New DataCacheFactoryConfiguration()
  
  
  
                  factoryConfig.LocalCacheProperties = localCacheConfig
  
                  _DataCacheFactory = New DataCacheFactory(factoryConfig)
  
              End If
  
              Return _DataCacheFactory
  
          End Get
      End Property
  
      Shared ReadOnly Property DataCache As Microsoft.ApplicationServer.Caching.DataCache
          Get
              If IsNothing(_DataCache) Then
                  _DataCache = DataCacheFactory.GetDefaultCache
              End If
  
              Return _DataCache
  
          End Get
      End Property
  
  End Class
  </code></pre>
  
  <p>But when I try to use it, it seems to be going out of scope and is being recreated repeatedly instead of just once per azure instance.  If I am understanding things correctly then this basically comes down to.....Where can I store a global variable in a wcf web service so that it doesn't go out of scope.</p>