Last active
August 29, 2015 14:04
-
-
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
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 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; | |
} | |
} |
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
// 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