Search code examples
asp.netappfabric

Windows Server Appfabric provider for asp.net output caching


Does asp.net output cache provider exist for Windows Server AppFabric 1.0?


Solution

  • No. But because output caching is pluggable in ASP.NET 4.0 (using the provider model), you can write your own.

    To create a new output cache provider, you'll need to inherit from System.Web.Caching.OutputCacheProvider, you'll need to reference System.Web and System.Configuration.

    Then it's largely a case of overriding the four methods from the base provider, Add, Get, Remove and Set.

    As your site is probably going to getting quite a few hits, you'll definitely want to use a Singleton for the DataCacheFactory, this code uses Jon Skeet's singleton pattern (assuming I've understood it correctly).

    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);
        }
    }
    }
    

    Once you've got this written, you need to configure your application to use it in your 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