Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. public static class MemoryCacheExtensions
  2. {
  3. public static T AddOrGetExistingLazy<T>(this MemoryCache cache, string key, Func<T> valueFactory, CacheItemPolicy cacheItemPolicy)
  4. {
  5. var newValue = new Lazy<T>(valueFactory);
  6. var value = (Lazy<T>)cache.AddOrGetExisting(key, newValue, cacheItemPolicy);
  7. return (value ?? newValue).Value; // Lazy<T> handles the locking itself
  8. }
  9.  
  10. public static void SetLazy<T>(this MemoryCache cache, string key, T value, CacheItemPolicy cacheItemPolicy)
  11. {
  12. var cacheValue = new Lazy<T>(() => value);
  13. cache.Set(key, cacheValue, cacheItemPolicy);
  14. }
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement