CacheHelper.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Microsoft.Extensions.Caching.Memory;
  2. using System;
  3. namespace ZR.Common
  4. {
  5. public class CacheHelper
  6. {
  7. public static MemoryCache Cache { get; set; }
  8. static CacheHelper()
  9. {
  10. Cache = new MemoryCache(new MemoryCacheOptions
  11. {
  12. //SizeLimit = 1024
  13. });
  14. }
  15. /// <summary>
  16. /// 获取缓存
  17. /// </summary>
  18. /// <typeparam name="T"></typeparam>
  19. /// <param name="key"></param>
  20. /// <returns></returns>
  21. public static T GetCache<T>(string key) where T : class
  22. {
  23. if (key == null)
  24. throw new ArgumentNullException(nameof(key));
  25. //return Cache.Get(key) as T; //或者
  26. return Cache.Get<T>(key);
  27. }
  28. /// <summary>
  29. /// 获取缓存
  30. /// </summary>
  31. /// <param name="CacheKey"></param>
  32. /// <returns></returns>
  33. public static object GetCache(string CacheKey)
  34. {
  35. return Cache.Get<object>(CacheKey);
  36. }
  37. public static object Get(string CacheKey)
  38. {
  39. return Cache.Get(CacheKey);
  40. }
  41. /// <summary>
  42. /// 设置缓存,永久缓存
  43. /// </summary>
  44. /// <param name="CacheKey">key</param>
  45. /// <param name="objObject">值</param>
  46. public static object SetCache(string CacheKey, object objObject)
  47. {
  48. return Cache.Set(CacheKey, objObject);
  49. }
  50. /// <summary>
  51. /// 设置缓存
  52. /// </summary>
  53. /// <param name="CacheKey">key</param>
  54. /// <param name="objObject">值</param>
  55. /// <param name="Timeout">过期时间(分钟)</param>
  56. public static object SetCache(string CacheKey, object objObject, int Timeout)
  57. {
  58. return Cache.Set(CacheKey, objObject, DateTime.Now.AddMinutes(Timeout));
  59. }
  60. /// <summary>
  61. /// 设置缓存(秒)
  62. /// </summary>
  63. /// <param name="CacheKey">key</param>
  64. /// <param name="objObject">值</param>
  65. /// <param name="Timeout">过期时间(秒)</param>
  66. public static void SetCaches(string CacheKey, object objObject, int Timeout)
  67. {
  68. Cache.Set(CacheKey, objObject, DateTime.Now.AddSeconds(Timeout));
  69. }
  70. /// <summary>
  71. /// 设置缓存
  72. /// </summary>
  73. /// <param name="CacheKey">key</param>
  74. /// <param name="objObject">值</param>
  75. /// <param name="absoluteExpiration">过期时间</param>
  76. /// <param name="slidingExpiration">过期时间间隔</param>
  77. public static object SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
  78. {
  79. return Cache.Set(CacheKey, objObject, absoluteExpiration);
  80. }
  81. /// <summary>
  82. /// 设定绝对的过期时间
  83. /// </summary>
  84. /// <param name="CacheKey"></param>
  85. /// <param name="objObject"></param>
  86. /// <param name="Seconds">超过多少秒后过期</param>
  87. public static void SetCacheDateTime(string CacheKey, object objObject, long Seconds)
  88. {
  89. Cache.Set(CacheKey, objObject, DateTime.Now.AddSeconds(Seconds));
  90. }
  91. /// <summary>
  92. /// 删除缓存
  93. /// </summary>
  94. /// <param name="key">key</param>
  95. public static void Remove(string key)
  96. {
  97. Cache.Remove(key);
  98. }
  99. }
  100. }