2011-10-18 49 views

回答

2

不,但是由於輸出緩存可在ASP.NET 4.0中插入(使用提供者模型),因此您可以編寫自己的代碼。

要創建一個新的輸出緩存提供程序,您需要繼承System.Web.Caching.OutputCacheProvider,那麼需要引用System.WebSystem.Configuration

然後,它主要是覆蓋基本提供者的四個方法,即添加,獲取,刪除和設置的情況。由於您的網站可能會獲得不少點擊量,因此您一定要使用Singleton作爲DataCacheFactory,此代碼使用Jon Skeet's singleton pattern(假設我已正確理解)。

using System; 
using System.Web.Caching; 
using Microsoft.ApplicationServer.Caching; 

namespace AppFabricOutputCache 
{ 
public sealed class AppFabricOutputCacheProvider : OutputCacheProvider 
{ 
    private static readonly AppFabricOutputCacheProvider instance = new AppFabricOutputCacheProvider(); 
    private DataCacheFactory factory; 
    private DataCache cache; 

    static AppFabricOutputCacheProvider() 
    { } 

    private AppFabricOutputCacheProvider() 
    { 
     // Constructor - new up the factory and get a reference to the cache based 
     // on a setting in web.config 
     factory = new DataCacheFactory(); 
     cache = factory.GetCache(System.Web.Configuration.WebConfigurationManager.AppSettings["OutputCacheName"]); 
    } 

    public static AppFabricOutputCacheProvider Instance 
    { 
     get { return instance; } 
    } 

    public override object Add(string key, object entry, DateTime utcExpiry) 
    { 
     // Add an object into the cache. 
     // Slight disparity here in that we're passed an absolute expiry but AppFabric wants 
     // a TimeSpan. Subtract Now from the expiry we get passed to create the TimeSpan 
     cache.Add(key, entry, utcExpiry - DateTime.UtcNow); 
    } 

    public override object Get(string key) 
    { 
     return cache.Get(key); 
    } 

    public override void Remove(string key) 
    { 
     cache.Remove(key); 
    } 

    public override void Set(string key, object entry, DateTime utcExpiry) 
    { 
     // Set here means 'add it if it doesn't exist, update it if it does' 
     // We can do this by using the AppFabric Put method 
     cache.Put(key, entry, utcExpiry - DateTime.UtcNow); 
    } 
} 
} 

一旦你得到了這個寫的,你需要配置你的應用程序在你的web.config使用它:

<system.web> 
    <caching> 
     <outputCache defaultProvider="AppFabricOutputCache"> 
      <providers> 
       <add name="AppFabricOutputCache" type="AppFabricOutputCache, AppFabricOutputCacheProvider" /> 
      </providers> 
     </outputCache> 
    </caching> 
</system.web> 

MSDN: OutputCacheProvider
ScottGu's blog on creating OutputCacheProviders

相關問題