Last active
May 8, 2019 16:51
-
-
Save jdalley/0cc8caed02845ad5a6006e4db1267720 to your computer and use it in GitHub Desktop.
MemoryCache Helper
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Runtime.Caching; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace TestProj.Helpers | |
{ | |
/// <summary> | |
/// Helpers on top of System.Runtime.Caching.MemoryCache.Default. | |
/// </summary> | |
public static class CacheHelper | |
{ | |
/// <summary> | |
/// Get or Add an object of a given type to MemoryCache.Default. Uses AbsoluteExpiration as CacheItemPolicy. | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="cacheItemName"></param> | |
/// <param name="cacheTimeInMinutes"></param> | |
/// <param name="objectSettingFunction"></param> | |
/// <returns></returns> | |
public static T GetOrAddObject<T>(string cacheItemName, int cacheTimeInMinutes, Func<T> objectSettingFunction) | |
{ | |
var cache = MemoryCache.Default; | |
var cachedObject = (T)cache[cacheItemName]; | |
if (cachedObject == null) | |
{ | |
var policy = | |
new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(cacheTimeInMinutes) }; | |
cachedObject = objectSettingFunction(); | |
cache.Set(cacheItemName, cachedObject, policy); | |
} | |
return cachedObject; | |
} | |
/// <summary> | |
/// Check if a given item exists in MemoryCache.Default; Remove it. | |
/// </summary> | |
/// <param name="cachedItemName"></param> | |
public static void RemoveCachedItem(string cachedItemName) | |
{ | |
var cache = MemoryCache.Default; | |
var cachedObject = cache[cachedItemName]; | |
if (cachedObject != null) | |
{ | |
cache.Remove(cachedItemName); | |
} | |
} | |
/// <summary> | |
/// Grab all keys in MemoryCache.Default, and remove them one by one. | |
/// </summary> | |
public static void ClearAllCachedItems() | |
{ | |
var cachedKeys = MemoryCache.Default.Select(c => c.Key).ToList(); | |
foreach (var cacheKey in cachedKeys) | |
{ | |
MemoryCache.Default.Remove(cacheKey); | |
} | |
} | |
} | |
} |
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
using TestProj.Helpers | |
namespace TestProj | |
{ | |
public class CustomStuff() | |
{ | |
public SomeCustomObject GetCustomObjectFromCache() | |
{ | |
var custObj = CacheHelper.GetOrAddObject("someUniqueItemIdentifier", cacheTimeInMinutes: 1440, () => | |
{ | |
// Some expensive call to get SomeCustomObject | |
var obj = ExpensiveCall(); | |
return obj; | |
}); | |
return custObj. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment