Cache Abstraction
I'm in the middle of a project and I wrote my own Cache logic that melds nicely to my project's needs. But how do I test it? I can't mock the System.Web.Caching.Cache class. So I looked in the Abstractions that ship with ASP.NET MVC. What do you know, they're not there!
So I created my own base class and wrapper. Here's the code. Enjoy.
using System;
using System.Collections;
using System.Web.Caching;
namespace WebCommon.Abstractions
{
public abstract class CacheBase : IEnumerable
{
public abstract int Count { get; }
public abstract long EffectivePercentagePhysicalMemoryLimit { get; }
public abstract long EffectivePrivateBytesLimit { get; }
public abstract object this[string key] { get; set; }
public abstract object Add(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);
public abstract object Get(string key);
public abstract IDictionaryEnumerator GetEnumerator();
public abstract void Insert(string key, object value);
public abstract void Insert(string key, object value, CacheDependency dependencies);
public abstract void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration);
public abstract void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);
public abstract object Remove(string key);
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}
using System;
using System.Collections;
using System.Web.Caching;
namespace WebCommon.Abstractions
{
public class CacheWrapper : CacheBase
{
private Cache _cache;
public CacheWrapper(Cache cache)
{
_cache = cache;
}
public override int Count
{
get { return _cache.Count; }
}
public override long EffectivePercentagePhysicalMemoryLimit
{
get { return _cache.EffectivePercentagePhysicalMemoryLimit; }
}
public override long EffectivePrivateBytesLimit
{
get { return _cache.EffectivePrivateBytesLimit; }
}
public override object this[string key]
{
get
{
return _cache[key];
}
set
{
_cache[key] = value;
}
}
public override object Add(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
{
return _cache.Add(key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);
}
public override object Get(string key)
{
return _cache.Get(key);
}
public override IDictionaryEnumerator GetEnumerator()
{
return _cache.GetEnumerator();
}
public override void Insert(string key, object value)
{
_cache.Insert(key, value);
}
public override void Insert(string key, object value, CacheDependency dependencies)
{
_cache.Insert(key, value, dependencies);
}
public override void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
_cache.Insert(key, value, dependencies, absoluteExpiration, slidingExpiration);
}
public override void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
{
_cache.Insert(key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);
}
public override object Remove(string key)
{
return _cache.Remove(key);
}
}
}