使用Enterprise Lib的緩存

摘要:使用Enterprise Lib的緩存

  1.  維護加入三個EntLib的四個類別庫(Microsoft.Practices.EnterpriseLibrary.Caching,Microsoft.Practices.EnterpriseLibrary.Caching.Cryptography,Microsoft.Practices.EnterpriseLibrary.Caching.Database,Microsoft.Practices.EnterpriseLibrary.Common)。
  2. 設定Config,可以用其工具維護。
    
    <configSections>
      <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
      </configSections>
    <cachingConfiguration defaultCacheManager="Cache Manager">
        <cacheManagers>
          <add name="Cache Manager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            expirationPollFrequencyInSeconds="3600" maximumElementsInCacheBeforeScavenging="1000"
            numberToRemoveWhenScavenging="10" backingStoreName="NullBackingStore" />
        </cacheManagers>
        <backingStores>
          <add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            name="NullBackingStore" />
        </backingStores>
      </cachingConfiguration>
  3. 編碼
    
     Dim strCacheKey = "DataKey"
                Dim objDefaultCache = EnterpriseLibraryContainer.Current.GetInstance(Of ICacheManager)()
    
                If objDefaultCache.Contains(strCacheKey) Then
                    Dim objCache = objDefaultCache.GetData(strCacheKey)
    
                    If Not objCache Is Nothing Then
                        objReturn = CType(objCache, List(Of Info))
                    Else
                  
                        objReturn = GetData()
                        objDefaultCache.Remove(strCacheKey)
                        objDefaultCache.Add(strCacheKey, objReturn, CacheItemPriority.High, Nothing, New SlidingTime(New TimeSpan(0, 2, 0)))
                    End If
                Else
                  
                    objReturn = GetData()
                    objDefaultCache.Add(strCacheKey, objReturn, CacheItemPriority.High, Nothing, New SlidingTime(New TimeSpan(0, 2, 0)))
    
                End If
  4. Okey.

 


人生到處知何似
應似飛鴻踏雪泥