Created
March 25, 2010 21:35
-
-
Save ianfnelson/344161 to your computer and use it in GitHub Desktop.
Caching abstractions for DI and testing purposes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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