Created
February 28, 2013 21:48
-
-
Save forcewake/5060409 to your computer and use it in GitHub Desktop.
Usage: cacheProvider.Get("cache id", (delegate method if cache is empty));
Cache provider will check if there's anything by the name of "cache id" in the cache, and if there's not, it will call a delegate method to fetch data and store it in cache. Example: var products=cacheService.Get("catalog.products", ()=>productRepository.GetAll())
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
public class InMemoryCache: ICacheService | |
{ | |
public T Get<T>(string cacheID, Func<T> getItemCallback) where T : class | |
{ | |
T item = HttpRuntime.Cache.Get(cacheID) as T; | |
if (item == null) | |
{ | |
item = getItemCallback(); | |
HttpContext.Current.Cache.Insert(cacheID,item); | |
} | |
return item; | |
} | |
} | |
interface ICacheService | |
{ | |
T Get<T>(string cacheID, Func<T> getItemCallback) where T : class; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment