Skip to content

Instantly share code, notes, and snippets.

@kevinblake
Created February 1, 2014 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kevinblake/8755257 to your computer and use it in GitHub Desktop.
Save kevinblake/8755257 to your computer and use it in GitHub Desktop.
SimpleCache
internal interface ICacheService
{
T Get<T>(string cacheId, Func<T> getItemCallback) where T : class;
T Get<T>(string cacheId, CacheDependency cacheDependency, DateTime absoluteExpiration, TimeSpan slidingExpiration,
Func<T> getItemCallback) where T : class;
}
public class InMemoryCache : ICacheService
{
public T Get<T>(string cacheId, Func<T> getItemCallback) where T : class
{
return Get(cacheId, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromHours(1), getItemCallback);
}
public T Get<T>(string cacheId, CacheDependency cacheDependency, DateTime absoluteExpiration,
TimeSpan slidingExpiration, Func<T> getItemCallback) where T : class
{
var item = HttpRuntime.Cache.Get(cacheId) as T;
if (item == null)
{
item = getItemCallback();
HttpContext.Current.Cache.Insert(cacheId, item, cacheDependency, absoluteExpiration, slidingExpiration);
}
return item;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment