【问题标题】:Windows Server Appfabric provider for asp.net output caching用于 asp.net 输出缓存的 Windows Server Appfabric 提供程序
【发布时间】:2011-10-18 01:55:10
【问题描述】:

是否存在用于 Windows Server AppFabric 1.0 的 asp.net 输出缓存提供程序?

【问题讨论】:

    标签: asp.net appfabric


    【解决方案1】:

    没有。但是因为输出缓存在 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

    【讨论】:

      猜你喜欢
      • 2017-09-28
      • 2011-12-24
      • 2019-08-03
      • 1970-01-01
      • 2012-04-06
      • 1970-01-01
      • 2013-01-04
      • 2012-12-08
      • 1970-01-01
      相关资源
      最近更新 更多