Created
February 1, 2014 17:06
-
-
Save kevinblake/8755257 to your computer and use it in GitHub Desktop.
SimpleCache
This file contains hidden or 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
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; | |
} |
This file contains hidden or 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
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