2011-10-18 1 views

답변

2

아니요.하지만 출력 캐싱은 공급자 모델을 사용하는 ASP.NET 4.0에서 플러그 가능하므로 사용자가 직접 작성할 수 있습니다.

새 출력 캐시 공급자를 만들려면 System.Web.Caching.OutputCacheProvider에서 상속해야하며 System.WebSystem.Configuration을 참조해야합니다.

다음은 기본 공급자 인 Add, Get, Remove 및 Set에서 네 가지 메서드를 재정의하는 경우입니다.

귀하의 사이트가 아마 적게 히트를 얻으므로 DataCacheFactory에 Singleton을 사용하고 싶습니다.이 코드는 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