2011-10-18 45 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

相关问题