Skip to content

Instantly share code, notes, and snippets.

@ianfnelson
Created March 25, 2010 21:35
Show Gist options
  • Save ianfnelson/344161 to your computer and use it in GitHub Desktop.
Save ianfnelson/344161 to your computer and use it in GitHub Desktop.
Caching abstractions for DI and testing purposes
namespace IanFNelson.Utilities.Caching
{
using System;
using System.Web;
using System.Web.Caching;
public class AspNetCache : ICache
{
public object Get(string key)
{
return HttpRuntime.Cache.Get(key);
}
public void Insert(string key, object value)
{
if (value == null)
{
return;
}
HttpRuntime.Cache.Insert(key, value);
}
public void Insert(string key, object value, TimeSpan timeToLive, bool slidingExpiration)
{
if (value == null)
{
return;
}
if (slidingExpiration)
{
HttpRuntime.Cache.Insert(key, value, null, DateTime.Now.Add(timeToLive), Cache.NoSlidingExpiration);
}
else
{
HttpRuntime.Cache.Insert(key, value, null, Cache.NoAbsoluteExpiration, timeToLive);
}
}
public int Count
{
get { return HttpRuntime.Cache.Count; }
}
}
}
namespace IanFNelson.Utilities.Caching
{
using System;
public interface ICache
{
void Insert(string key, object value);
void Insert(string key, object value, TimeSpan timeToLive, bool slidingExpiration);
object Get(string key);
int Count { get; }
}
}
namespace IanFNelson.Utilities.Caching
{
using System;
public class NullCache : ICache
{
public static readonly NullCache Instance = new NullCache();
public void Insert(string key, object value)
{
}
public object Get(string key)
{
return null;
}
public void Insert(string key, object value, TimeSpan timeToLive, bool slidingExpiration)
{
}
public int Count
{
get { return 0; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment