コード例 #1
0
        public static IServiceCollection AddAppCaching(this IServiceCollection services, IWebHostEnvironment environment, IConfiguration configuration)
        {
            if (environment.IsDevelopment())
            {
                services.AddDistributedMemoryCache();
            }
            else
            {
                var distributedCacheSection = new DistributedCacheSection();
                configuration.Bind(DistributedCacheSection.SectionName, distributedCacheSection);

                services.AddStackExchangeRedisCache(options =>
                {
                    options.Configuration = distributedCacheSection.Configuration;
                    options.InstanceName  = distributedCacheSection.InstanceName;
                });

                services.AddSingleton <IConnectionMultiplexer>(provider => ConnectionMultiplexer.Connect($"{distributedCacheSection.Configuration},channelPrefix={distributedCacheSection.InstanceName}"));
                services.AddSingleton <IDistributedLockFactory>(provider => RedLockFactory.Create(new List <RedLockMultiplexer>()
                {
                    new RedLockMultiplexer(provider.GetService <IConnectionMultiplexer>())
                }));
            }

            return(services);
        }
コード例 #2
0
        public static IServiceCollection AddAppSignalR(this IServiceCollection services, IWebHostEnvironment environment, IConfiguration configuration)
        {
            var signalRBuilder = services.AddSignalR()
                                 .AddMessagePackProtocol();

            if (!environment.IsDevelopment())
            {
                var distributedCacheSection = new DistributedCacheSection();
                configuration.Bind(DistributedCacheSection.SectionName, distributedCacheSection);

                signalRBuilder.AddStackExchangeRedis(distributedCacheSection.Configuration, options =>
                {
                    options.Configuration.ChannelPrefix = distributedCacheSection.InstanceName;
                });
            }

            return(services);
        }