Cache.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Runtime.Caching;
  6. namespace LJProxy
  7. {
  8. public static class Cache
  9. {
  10. public static MemoryCache DefaultCache
  11. {
  12. get
  13. {
  14. return MemoryCache.Default;
  15. }
  16. }
  17. private static MemoryCache _fileCache = new MemoryCache("FileCache");
  18. public static MemoryCache FileCache
  19. {
  20. get
  21. {
  22. return _fileCache;
  23. }
  24. }
  25. public static CacheItemPolicy CreateCacheItemPolicy(TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null
  26. , List<string> dependencyKeys = null, CacheItemPriority? priority = null)
  27. {
  28. var cachePolicy = new CacheItemPolicy();
  29. if (slidingExpiration != null)
  30. cachePolicy.SlidingExpiration = slidingExpiration.Value;
  31. if (absoluteExpiration != null)
  32. cachePolicy.AbsoluteExpiration = absoluteExpiration.Value;
  33. if (dependencyKeys !=null && dependencyKeys.Count>0 )
  34. {
  35. cachePolicy.ChangeMonitors.Add(new HostFileChangeMonitor(dependencyKeys));
  36. }
  37. if (priority != null)
  38. cachePolicy.Priority = priority.Value;
  39. else
  40. cachePolicy.Priority = CacheItemPriority.Default;
  41. return cachePolicy;
  42. }
  43. }
  44. }