Skip to content

Instantly share code, notes, and snippets.

@forcewake
Created February 28, 2013 21:48
Show Gist options
  • Save forcewake/5060409 to your computer and use it in GitHub Desktop.
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())
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