diff --git a/common/ASC.Api.Core/Core/BaseStartup.cs b/common/ASC.Api.Core/Core/BaseStartup.cs index 18e1e21c44..056bd55146 100644 --- a/common/ASC.Api.Core/Core/BaseStartup.cs +++ b/common/ASC.Api.Core/Core/BaseStartup.cs @@ -23,6 +23,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Authorization; @@ -58,7 +59,7 @@ namespace ASC.Api.Core if (bool.TryParse(Configuration["core:products"], out var loadProducts)) { LoadProducts = loadProducts; - } + } } public virtual void ConfigureServices(IServiceCollection services) @@ -84,8 +85,8 @@ namespace ASC.Api.Core { options.JsonSerializerOptions.Converters.Add(c); } - } - }; + } + }; services.AddControllers() .AddXmlSerializerFormatters() @@ -103,7 +104,18 @@ namespace ASC.Api.Core DIHelper.TryAdd(); DIHelper.TryAdd(); - DIHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + var redisConfiguration = Configuration.GetSection("Redis").Get(); + + if (redisConfiguration != null) + { + DIHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + } + else + { + DIHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>)); + } + + DIHelper.TryAdd(typeof(IWebhookPublisher), typeof(WebhookPublisher)); if (LoadProducts) diff --git a/common/ASC.Common/Caching/KafkaCache.cs b/common/ASC.Common/Caching/KafkaCache.cs index c753c67e64..a8bcbbc67b 100644 --- a/common/ASC.Common/Caching/KafkaCache.cs +++ b/common/ASC.Common/Caching/KafkaCache.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Concurrent; -using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -24,8 +23,6 @@ namespace ASC.Common.Caching private ILog Log { get; set; } private ConcurrentDictionary Cts { get; set; } private ConcurrentDictionary> Actions { get; set; } - private MemoryCacheNotify MemoryCacheNotify { get; set; } - private string ChannelName { get; } = $"ascchannel{typeof(T).Name}"; private ProtobufSerializer ValueSerializer { get; } = new ProtobufSerializer(); private ProtobufDeserializer ValueDeserializer { get; } = new ProtobufDeserializer(); private ProtobufSerializer KeySerializer { get; } = new ProtobufSerializer(); @@ -41,26 +38,13 @@ namespace ASC.Common.Caching Key = Guid.NewGuid(); var settings = configuration.GetSetting("kafka"); - if (settings != null && !string.IsNullOrEmpty(settings.BootstrapServers)) - { - ClientConfig = new ClientConfig { BootstrapServers = settings.BootstrapServers }; - AdminClientConfig = new AdminClientConfig { BootstrapServers = settings.BootstrapServers }; - } - else - { - MemoryCacheNotify = new MemoryCacheNotify(); - } + ClientConfig = new ClientConfig { BootstrapServers = settings.BootstrapServers }; + AdminClientConfig = new AdminClientConfig { BootstrapServers = settings.BootstrapServers }; } public void Publish(T obj, CacheNotifyAction cacheNotifyAction) { - if (ClientConfig == null) - { - MemoryCacheNotify.Publish(obj, cacheNotifyAction); - return; - } - try { if (Producer == null) @@ -102,12 +86,8 @@ namespace ASC.Common.Caching public void Subscribe(Action onchange, CacheNotifyAction cacheNotifyAction) { - if (ClientConfig == null) - { - MemoryCacheNotify.Subscribe(onchange, cacheNotifyAction); - return; - } var channelName = GetChannelName(cacheNotifyAction); + Cts[channelName] = new CancellationTokenSource(); Actions[channelName] = onchange; @@ -137,7 +117,7 @@ namespace ASC.Common.Caching } }).Wait(); } - catch(AggregateException) + catch (AggregateException) { } @@ -189,7 +169,7 @@ namespace ASC.Common.Caching private string GetChannelName(CacheNotifyAction cacheNotifyAction) { - return $"{ChannelName}{cacheNotifyAction}"; + return $"asc:channel:{cacheNotifyAction}:{typeof(T).FullName}".ToLower(); } public void Unsubscribe(CacheNotifyAction action) @@ -231,40 +211,4 @@ namespace ASC.Common.Caching { public string BootstrapServers { get; set; } } - - public class MemoryCacheNotify : ICacheNotify where T : IMessage, new() - { - private readonly Dictionary>> actions = new Dictionary>>(); - - public void Publish(T obj, CacheNotifyAction action) - { - if (actions.TryGetValue(GetKey(action), out var onchange) && onchange != null) - { - foreach (var a in onchange) - { - a(obj); - } - } - } - - public void Subscribe(Action onchange, CacheNotifyAction notifyAction) - { - if (onchange != null) - { - var key = GetKey(notifyAction); - actions.TryAdd(key, new List>()); - actions[key].Add(onchange); - } - } - - public void Unsubscribe(CacheNotifyAction action) - { - actions.Remove(GetKey(action)); - } - - private string GetKey(CacheNotifyAction cacheNotifyAction) - { - return $"{typeof(T).Name}{cacheNotifyAction}"; - } - } } \ No newline at end of file diff --git a/common/ASC.Common/Caching/MemoryCacheNotify.cs b/common/ASC.Common/Caching/MemoryCacheNotify.cs index 7b86415add..fa9e1eeb7e 100644 --- a/common/ASC.Common/Caching/MemoryCacheNotify.cs +++ b/common/ASC.Common/Caching/MemoryCacheNotify.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Threading.Tasks; using Google.Protobuf; @@ -20,10 +21,7 @@ namespace ASC.Common.Caching { if (_actions.TryGetValue(GetKey(action), out var onchange) && onchange != null) { - foreach (var a in onchange) - { - a(obj); - } + Parallel.ForEach(onchange, a => a(obj)); } } @@ -44,7 +42,7 @@ namespace ASC.Common.Caching private string GetKey(CacheNotifyAction cacheNotifyAction) { - return $"{typeof(T).Name}{cacheNotifyAction}"; + return $"asc:channel:{cacheNotifyAction}:{typeof(T).FullName}".ToLower(); } } } \ No newline at end of file diff --git a/common/services/ASC.ApiSystem/Startup.cs b/common/services/ASC.ApiSystem/Startup.cs index f303f4dddb..a52b62d689 100644 --- a/common/services/ASC.ApiSystem/Startup.cs +++ b/common/services/ASC.ApiSystem/Startup.cs @@ -75,7 +75,17 @@ namespace ASC.ApiSystem services.AddMemoryCache(); - diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + var redisConfiguration = Configuration.GetSection("Redis").Get(); + + if (redisConfiguration != null) + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + } + else + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>)); + } + diHelper.TryAdd(); diHelper.TryAdd(); diHelper.TryAdd(); diff --git a/common/services/ASC.ClearEvents/Program.cs b/common/services/ASC.ClearEvents/Program.cs index ebfc508e07..5ab33a691e 100644 --- a/common/services/ASC.ClearEvents/Program.cs +++ b/common/services/ASC.ClearEvents/Program.cs @@ -39,12 +39,13 @@ using Autofac.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - - +using Microsoft.Extensions.Hosting; + +using StackExchange.Redis.Extensions.Core.Configuration; + namespace ASC.Thumbnails.Svc -{ - public class Program +{ +public class Program { public async static Task Main(string[] args) { @@ -81,9 +82,19 @@ namespace ASC.Thumbnails.Svc .ConfigureServices((hostContext, services) => { services.AddMemoryCache(); - var diHelper = new DIHelper(services); + var diHelper = new DIHelper(services); + + var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get(); + + if (redisConfiguration != null) + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + } + else + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>)); + } - diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); services.AddHostedService(); diHelper.TryAdd(); }) diff --git a/common/services/ASC.Data.Storage.Migration/Program.cs b/common/services/ASC.Data.Storage.Migration/Program.cs index f130a609d8..b47137e537 100644 --- a/common/services/ASC.Data.Storage.Migration/Program.cs +++ b/common/services/ASC.Data.Storage.Migration/Program.cs @@ -69,7 +69,17 @@ namespace ASC.Data.Storage.Migration { services.AddMemoryCache(); var diHelper = new DIHelper(services); - diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + + var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get(); + + if (redisConfiguration != null) + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + } + else + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>)); + } diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath); diff --git a/common/services/ASC.Notify/Program.cs b/common/services/ASC.Notify/Program.cs index 68c94ad7bc..b3b283d949 100644 --- a/common/services/ASC.Notify/Program.cs +++ b/common/services/ASC.Notify/Program.cs @@ -72,7 +72,17 @@ namespace ASC.Notify services.AddMemoryCache(); var diHelper = new DIHelper(services); - diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get(); + + if (redisConfiguration != null) + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + } + else + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>)); + } + diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath); services.Configure(hostContext.Configuration.GetSection("notify")); diff --git a/common/services/ASC.Radicale/Program.cs b/common/services/ASC.Radicale/Program.cs index 736f0fd53e..ae016dbbb5 100644 --- a/common/services/ASC.Radicale/Program.cs +++ b/common/services/ASC.Radicale/Program.cs @@ -93,8 +93,19 @@ public class Program .ConfigureServices((hostContext, services) => { services.AddMemoryCache(); - var diHelper = new DIHelper(services); - diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + var diHelper = new DIHelper(services); + + var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get(); + + if (redisConfiguration != null) + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + } + else + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>)); + } + diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath); services.AddHostedService(); diff --git a/common/services/ASC.Socket.IO.Svc/Program.cs b/common/services/ASC.Socket.IO.Svc/Program.cs index 04d6d7ded6..9b7c596d2b 100644 --- a/common/services/ASC.Socket.IO.Svc/Program.cs +++ b/common/services/ASC.Socket.IO.Svc/Program.cs @@ -94,7 +94,18 @@ public class Program { services.AddMemoryCache(); var diHelper = new DIHelper(services); - diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + + var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get(); + + if (redisConfiguration != null) + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + } + else + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>)); + } + diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath); services.AddHostedService(); diff --git a/common/services/ASC.SsoAuth.Svc/Program.cs b/common/services/ASC.SsoAuth.Svc/Program.cs index 64e264bcb6..4aa6cb6024 100644 --- a/common/services/ASC.SsoAuth.Svc/Program.cs +++ b/common/services/ASC.SsoAuth.Svc/Program.cs @@ -95,7 +95,18 @@ public class Program { services.AddMemoryCache(); var diHelper = new DIHelper(services); - diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + + var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get(); + + if (redisConfiguration != null) + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + } + else + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>)); + } + diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath); services.AddHostedService(); diff --git a/common/services/ASC.Studio.Notify/Program.cs b/common/services/ASC.Studio.Notify/Program.cs index c054cd4f71..72be26bb97 100644 --- a/common/services/ASC.Studio.Notify/Program.cs +++ b/common/services/ASC.Studio.Notify/Program.cs @@ -72,7 +72,18 @@ namespace ASC.Studio.Notify { services.AddMemoryCache(); var diHelper = new DIHelper(services); - diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + + var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get(); + + if (redisConfiguration != null) + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + } + else + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>)); + } + diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath); services.AddHostedService(); diHelper.TryAdd(); diff --git a/common/services/ASC.Thumbnails.Svc/Program.cs b/common/services/ASC.Thumbnails.Svc/Program.cs index c99269d343..3866376384 100644 --- a/common/services/ASC.Thumbnails.Svc/Program.cs +++ b/common/services/ASC.Thumbnails.Svc/Program.cs @@ -95,7 +95,17 @@ public class Program services.AddMemoryCache(); var diHelper = new DIHelper(services); - diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get(); + + if (redisConfiguration != null) + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + } + else + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>)); + } + services.AddHostedService(); diHelper.TryAdd(); diff --git a/common/services/ASC.Webhooks.Service/Program.cs b/common/services/ASC.Webhooks.Service/Program.cs index 080039a05d..a286c88554 100644 --- a/common/services/ASC.Webhooks.Service/Program.cs +++ b/common/services/ASC.Webhooks.Service/Program.cs @@ -70,7 +70,16 @@ namespace ASC.Webhooks.Service var diHelper = new DIHelper(services); - diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get(); + + if (redisConfiguration != null) + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + } + else + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>)); + } diHelper.TryAdd(); diff --git a/config/redis.json b/config/redis.json index fb4f128368..40af8fe188 100644 --- a/config/redis.json +++ b/config/redis.json @@ -1,14 +1,5 @@ { "Redis": { - "Ssl": false, - "ConnectTimeout": 5000, - "SyncTimeout": 60000, - "ConnectRetry": 2, - "Database": 0, - "Hosts": [ - { - "Host": "127.0.0.1", - "Port": "6379" - }] + } -} \ No newline at end of file +} diff --git a/products/ASC.CRM/BackgroundTasks/Program.cs b/products/ASC.CRM/BackgroundTasks/Program.cs index 8dce9babea..fc407d36d7 100644 --- a/products/ASC.CRM/BackgroundTasks/Program.cs +++ b/products/ASC.CRM/BackgroundTasks/Program.cs @@ -71,7 +71,16 @@ namespace ASC.CRM.BackgroundTasks var diHelper = new DIHelper(services); - diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get(); + + if (redisConfiguration != null) + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + } + else + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>)); + } diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath); diff --git a/products/ASC.Files/Service/Program.cs b/products/ASC.Files/Service/Program.cs index d22e60940d..b60257bf33 100644 --- a/products/ASC.Files/Service/Program.cs +++ b/products/ASC.Files/Service/Program.cs @@ -76,7 +76,16 @@ namespace ASC.Files.Service var diHelper = new DIHelper(services); - diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get(); + + if (redisConfiguration != null) + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>)); + } + else + { + diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>)); + } diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath);