zhaolei
2020-11-20 921de2254ff5712a44ed8575ee8efe34252f6603
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#if !NET45
using Prow.Cache;
using Microsoft.Extensions.Options;
 
namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// 配置服务注入方法扩展类
    /// </summary>
    public static class CacheManagerServiceCollectionExtensions
    {
        /// <summary>
        /// 增加缓存管理到服务容器中 <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection AddCacheManager(this IServiceCollection services)
        {
            services.AddHttpContextAccessor();
            services.AddSingleton<IConfigureOptions<CacheOptions>, CacheConfigureOptions<CacheOptions>>();
            services.AddSingleton<IOptionsChangeTokenSource<CacheOptions>, ConfigurationChangeTokenSource<CacheOptions>>();
            services.AddSingleton(CacheManager.CreateCache);
            services.AddHttpClient(DefaultCache.HttpClientName, client => client.DefaultRequestHeaders.Connection.Add("keep-alive"));
            services.RegisterCacheManager();
            return services;
        }
    }
}
#endif