ObjectCaching.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Long.Core.Extensions;
  2. using System.Runtime.Caching;
  3. namespace Long.Utils.Cache
  4. {
  5. /// <summary>
  6. /// 缓存类
  7. /// </summary>
  8. public class ObjectCaching
  9. {
  10. /// <summary>
  11. /// 添加缓存
  12. /// </summary>
  13. /// <param name="key">键</param>
  14. /// <param name="value">值</param>
  15. /// <param name="timeout">过期时间(秒)</param>
  16. public static void Add(string key, object value, long timeout = 60)
  17. {
  18. Add(key, value, timeout, null);
  19. }
  20. /// <summary>
  21. /// 添加缓存
  22. /// </summary>
  23. /// <param name="key">键</param>
  24. /// <param name="value">值</param>
  25. /// <param name="timeout">过期时间(秒)</param>
  26. /// <param name="regionName">区域名称</param>
  27. /// <exception cref="ApplicationException"></exception>
  28. public static void Add(string key, object value, long timeout, string regionName)
  29. {
  30. ObjectCache cache = MemoryCache.Default;
  31. key.AssertNullOrWhiteSpace("缓存键名不能未空。");
  32. if (timeout <= 0)
  33. {
  34. throw new ApplicationException("参数 timeout 的值不能小于1。");
  35. }
  36. var policy = new CacheItemPolicy()
  37. {
  38. SlidingExpiration = new TimeSpan(timeout * TimeSpan.TicksPerSecond),
  39. };
  40. cache.Add(key, value, policy, regionName);
  41. }
  42. /// <summary>
  43. /// 判断缓存内容是否存在
  44. /// </summary>
  45. /// <param name="key">键</param>
  46. /// <returns></returns>
  47. public static bool Contains(string key)
  48. {
  49. return Contains(key, null);
  50. }
  51. /// <summary>
  52. /// 判断缓存内容是否存在
  53. /// </summary>
  54. /// <param name="key">键</param>
  55. /// <param name="regionName">区域名</param>
  56. /// <returns></returns>
  57. public static bool Contains(string key, string regionName)
  58. {
  59. ObjectCache cache = MemoryCache.Default;
  60. key.AssertNullOrWhiteSpace("缓存键名不能未空。");
  61. return cache.Contains(key, regionName);
  62. }
  63. /// <summary>
  64. /// 获取缓存内容
  65. /// </summary>
  66. /// <typeparam name="T">泛型对象</typeparam>
  67. /// <param name="key">键</param>
  68. /// <returns></returns>
  69. public static T Get<T>(string key)
  70. {
  71. ObjectCache cache = MemoryCache.Default;
  72. key.AssertNullOrWhiteSpace("缓存键名不能未空。");
  73. return (T)cache.Get(key, null);
  74. }
  75. /// <summary>
  76. /// 获取缓存内容
  77. /// </summary>
  78. /// <param name="key">键</param>
  79. /// <param name="regionName">区域名</param>
  80. /// <returns></returns>
  81. public static T Get<T>(string key, string regionName)
  82. {
  83. ObjectCache cache = MemoryCache.Default;
  84. key.AssertNullOrWhiteSpace("缓存键名不能未空。");
  85. return (T)cache.Get(key, regionName);
  86. }
  87. /// <summary>
  88. /// 删除缓存
  89. /// </summary>
  90. /// <param name="key"></param>
  91. public static void Remove(string key)
  92. {
  93. Remove(key, null);
  94. }
  95. /// <summary>
  96. /// 删除缓存
  97. /// </summary>
  98. /// <param name="key">键</param>
  99. /// <param name="regionName">区域名</param>
  100. public static void Remove(string key, string regionName)
  101. {
  102. ObjectCache cache = MemoryCache.Default;
  103. key.AssertNullOrWhiteSpace("缓存键名不能未空。");
  104. cache.Remove(key, regionName);
  105. }
  106. }
  107. }