Guest User

Untitled

a guest
Dec 11th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. public class MemoryCache : IObjectCache
  2. {
  3. public T Get<T>(CacheKey key, DateTime absoluteExpiration, Func<T> load)
  4. {
  5. var policy = new System.Runtime.Caching.CacheItemPolicy();
  6. policy.AbsoluteExpiration = absoluteExpiration;
  7.  
  8. return Get(key, policy, load);
  9. }
  10.  
  11. public T Get<T>(CacheKey key, TimeSpan slidingExpiration, Func<T> load)
  12. {
  13. var policy = new System.Runtime.Caching.CacheItemPolicy();
  14. policy.SlidingExpiration = slidingExpiration;
  15.  
  16. return Get(key, policy, load);
  17. }
  18.  
  19. private T Get<T>(CacheKey key, System.Runtime.Caching.CacheItemPolicy policy, Func<T> load)
  20. {
  21. var keyStr = key.ToString();
  22. var cache = System.Runtime.Caching.MemoryCache.Default;
  23. T result = (T)cache[keyStr];
  24.  
  25. if (result == null)
  26. {
  27. result = load();
  28. cache.Add(new System.Runtime.Caching.CacheItem(keyStr, result), policy);
  29. }
  30.  
  31. return result;
  32. }
  33. }
Add Comment
Please, Sign In to add comment