环境:net core 6.0


  1. builder.Services.AddMemoryCache();
  2. builder.Services.AddTransient<MemoryCacheService>();

Program.cs写入

  1. ICacheService cacheMemory;
  2. //控制器构造函数中得到服务
  3. public WorkFlowController(IServiceProvider service) {
  4. cacheMemory = service.GetService<MemoryCacheService>();
  5. }

控制器构造函数


其他用法,比如如何在业务层实例化缓存

  1. public static class ServiceLocator
  2. {
  3. /// <summary>
  4. /// 服务提供程序,用于直接获取已注入的类
  5. /// </summary>
  6. public static IServiceProvider ServiceProvider { get; set; }
  7. public static IApplicationBuilder ApplicationBuilder { get; set; }
  8. }
  1. var app = builder.Build();
  2. ServiceLocator.ApplicationBuilder = app;
  3. ServiceLocator.ServiceProvider = app.Services;
  1. //实例化对象
  2. var cache = ServiceLocator.ServiceProvider.GetService(typeof(MemoryCacheService)) as ICacheService;
  1. public interface ICacheService
  2. {
  3. /// <summary>
  4. /// 新增
  5. /// </summary>
  6. /// <param name="key"></param>
  7. /// <param name="value"></param>
  8. /// <param name="ExpirtionTime"></param>
  9. /// <returns></returns>
  10. bool Add(string key, object value, int ExpirtionTime = 20);
  11. /// <summary>
  12. /// 获取
  13. /// </summary>
  14. /// <param name="key"></param>
  15. /// <returns></returns>
  16. object GetValue(string key);
  17. /// <summary>
  18. /// 验证缓存项是否存在
  19. /// </summary>
  20. /// <param name="key">缓存Key</param>
  21. /// <returns></returns>
  22. bool Exists(string key);
  23. /// <summary>
  24. /// 移除
  25. /// </summary>
  26. /// <param name="key"></param>
  27. /// <returns></returns>
  28. bool Remove(string key);
  29. }
  1. public class MemoryCacheService : ICacheService
  2. {
  3. protected IMemoryCache _cache;
  4. public MemoryCacheService(IMemoryCache cache)
  5. {
  6. _cache = cache;
  7. }
  8. public bool Add(string key, object value, int ExpirtionTime = 20)
  9. {
  10. if (!string.IsNullOrEmpty(key))
  11. {
  12. MemoryCacheEntryOptions cacheEntityOps = new MemoryCacheEntryOptions()
  13. {
  14. //滑动过期时间 20秒没有访问则清除
  15. SlidingExpiration = TimeSpan.FromSeconds(ExpirtionTime),
  16. //设置份数
  17. Size = 1,
  18. //优先级
  19. Priority = CacheItemPriority.Low,
  20. };
  21. //过期回掉
  22. cacheEntityOps.RegisterPostEvictionCallback((keyInfo, valueInfo, reason, state) =>
  23. {
  24. Console.WriteLine($"回调函数输出【键:{keyInfo},值:{valueInfo},被清除的原因:{reason}】");
  25. });
  26. _cache.Set(key, value, cacheEntityOps);
  27. }
  28. return true;
  29. }
  30. public bool Remove(string key)
  31. {
  32. if (string.IsNullOrEmpty(key))
  33. {
  34. return false;
  35. }
  36. if (Exists(key))
  37. {
  38. _cache.Remove(key);
  39. return true;
  40. }
  41. return false;
  42. }
  43. public object GetValue(string key)
  44. {
  45. if (string.IsNullOrEmpty(key))
  46. {
  47. return null;
  48. }
  49. if (Exists(key))
  50. {
  51. return _cache.Get(key);
  52. }
  53. return null;
  54. }
  55. public bool Exists(string key)
  56. {
  57. if (string.IsNullOrEmpty(key))
  58. {
  59. return false;
  60. }
  61. object cache;
  62. return _cache.TryGetValue(key, out cache);
  63. }
  64. }

说明:如果使用的自带的IMemoryCache,只需要builder.Services.AddMemoryCache();即可