views:

480

answers:

1

I want to use AppFabric (Velocity) as the Disk Caching Provider to work with ASP.NET 4.0's extensible output caching feature. But when I installed the AppFabric, I found it is incredibly hard to configure and I have no idea how can I make my ASP.NET app to work with it. So I was wondering is there a easy to understand tutorial for configuring both?

Or, is there any easier way other than AppFarbric to implement disk caching with ASP.NET?

A: 

I wrote some VB code for an AppFabricOutputCacheProvider in January - it's on my blog here. A C# (4.0) version would be:

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

namespace AppFabricOutputCache
{
    public class CacheProvider: System.Web.Caching.OutputCacheProvider, IDisposable
    {
        DataCache mCache;

        const String OutputCacheName = "OutputCache";

        public void New()
        {
            DataCacheFactory factory;

            factory = new DataCacheFactory();

            mCache = factory.GetCache(OutputCacheName);
        }

        public override Object Add(String key, Object entry, DateTime utcExpiry)
        {
            mCache.Add(key, entry, utcExpiry - DateTime.UtcNow);

            return entry;
        }

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

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

        public override void Set(string key, object entry, DateTime utcExpiry)
        {
            mCache.Put(key, entry, utcExpiry - DateTime.UtcNow);
        }

        public void IDisposable.Dispose()
        {
            mCache = null;
        }

    }
}

To use this in your application, you need this in your web.config.

<caching>
  <outputCache>
    <providers>
      <add name="AppFabricOutputCacheProvider" type="AppFabricOutputCache.CacheProvider"/>
    </providers>
  </outputCache>
</caching>

Gunnar Peipman has a disk-based output cache provider on his blog here.

PhilPursglove
Hi Phil, thanks for your answer! I just saw another solution: http://bit.ly/22wknTDo you know which solution is faster? (I only store cache on my local server, no distributed store needs)
silent
Hard to say without testing it but I would *expect* that AppFabric would have the edge as it is all done in memory, a disk-based cache is likely to have some latency from accessing the disk.
PhilPursglove