Skip to content

Instantly share code, notes, and snippets.

@Tazer
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tazer/49dfca4216e0471fe62f to your computer and use it in GitHub Desktop.
Save Tazer/49dfca4216e0471fe62f to your computer and use it in GitHub Desktop.
Getting cached results if available else use the Func and invoke an database GET
public class CacheHelper : ICacheHelper
{
private readonly ICache _cache;
public CacheHelper(ICache cache)
{
this._cache = cache;
}
public IEnumerable<T> GetResult<T>(string cacheKey, Func<IEnumerable<T>> getDatabaseResultsFunc)
{
var cachedResult = _cache.Get<IList<T>>(cacheKey);
if (cachedResult != null) return cachedResult;
var result = getDatabaseResultsFunc();
_cache.Insert(cacheKey,result);
return result;
}
public T GetSingleResult<T>(string cacheKey, Func<T> getDatabaseResultsFunc)
{
var cachedResult = _cache.Get<T>(cacheKey);
if (cachedResult != null) return cachedResult;
var result = getDatabaseResultsFunc();
_cache.Insert(cacheKey, result);
return result;
}
}
// Getting cached results if available else use the Func and invoke an database GET
public IEnumerable<That> GetSomething(){
return _cacheHelper.GetResult("THAT_CACHE_KEY",() => {
var dbResult = gettingStuffFromDb();
return dbResult;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment