MemoryCacheSetup.cs 937 B

12345678910111213141516171819202122232425262728293031
  1. using Microsoft.Extensions.Caching.Memory;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.Options;
  4. using Ropin.Core.Common;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace Ropin.Core.Extensions
  11. {
  12. /// <summary>
  13. /// Memory缓存 启动服务
  14. /// </summary>
  15. public static class MemoryCacheSetup
  16. {
  17. public static void AddMemoryCacheSetup(this IServiceCollection services)
  18. {
  19. if (services == null) throw new ArgumentNullException(nameof(services));
  20. services.AddScoped<ICaching, MemoryCaching>();
  21. services.AddSingleton<IMemoryCache>(factory =>
  22. {
  23. var value = factory.GetRequiredService<IOptions<MemoryCacheOptions>>();
  24. var cache = new MemoryCache(value);
  25. return cache;
  26. });
  27. }
  28. }
  29. }