Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class MemoryCacheExtensions
- {
- public static T AddOrGetExistingLazy<T>(this MemoryCache cache, string key, Func<T> valueFactory, CacheItemPolicy cacheItemPolicy)
- {
- var newValue = new Lazy<T>(valueFactory);
- var value = (Lazy<T>)cache.AddOrGetExisting(key, newValue, cacheItemPolicy);
- return (value ?? newValue).Value; // Lazy<T> handles the locking itself
- }
- public static void SetLazy<T>(this MemoryCache cache, string key, T value, CacheItemPolicy cacheItemPolicy)
- {
- var cacheValue = new Lazy<T>(() => value);
- cache.Set(key, cacheValue, cacheItemPolicy);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement