diff --git a/src/Api/Utilities/ServiceCollectionExtensions.cs b/src/Api/Utilities/ServiceCollectionExtensions.cs index a98d6722df..4c6664bf7d 100644 --- a/src/Api/Utilities/ServiceCollectionExtensions.cs +++ b/src/Api/Utilities/ServiceCollectionExtensions.cs @@ -90,9 +90,9 @@ public static class ServiceCollectionExtensions builder.AddSqlServer(globalSettings.SqlServer.ConnectionString); } - if (CoreHelpers.SettingHasValue(globalSettings.Redis.ConnectionString)) + if (CoreHelpers.SettingHasValue(globalSettings.DistributedCache?.Redis?.ConnectionString)) { - builder.AddRedis(globalSettings.Redis.ConnectionString); + builder.AddRedis(globalSettings.DistributedCache.Redis.ConnectionString); } if (CoreHelpers.SettingHasValue(globalSettings.Storage.ConnectionString)) diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 642900e26c..aa6f5778b7 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -38,6 +38,7 @@ + diff --git a/src/Core/Settings/GlobalSettings.cs b/src/Core/Settings/GlobalSettings.cs index efa44c440b..0739a4c81a 100644 --- a/src/Core/Settings/GlobalSettings.cs +++ b/src/Core/Settings/GlobalSettings.cs @@ -55,7 +55,7 @@ public class GlobalSettings : IGlobalSettings public virtual MailSettings Mail { get; set; } = new MailSettings(); public virtual IConnectionStringSettings Storage { get; set; } = new ConnectionStringSettings(); public virtual ConnectionStringSettings Events { get; set; } = new ConnectionStringSettings(); - public virtual IConnectionStringSettings Redis { get; set; } = new ConnectionStringSettings(); + public virtual DistributedCacheSettings DistributedCache { get; set; } = new DistributedCacheSettings(); public virtual NotificationsSettings Notifications { get; set; } = new NotificationsSettings(); public virtual IFileStorageSettings Attachment { get; set; } public virtual FileStorageSettings Send { get; set; } @@ -550,4 +550,10 @@ public class GlobalSettings : IGlobalSettings public string FlagDataFilePath { get; set; } = "flags.json"; public Dictionary FlagValues { get; set; } = new Dictionary(); } + + public class DistributedCacheSettings + { + public virtual IConnectionStringSettings Redis { get; set; } = new ConnectionStringSettings(); + public virtual IConnectionStringSettings Cosmos { get; set; } = new ConnectionStringSettings(); + } } diff --git a/src/SharedWeb/Utilities/ServiceCollectionExtensions.cs b/src/SharedWeb/Utilities/ServiceCollectionExtensions.cs index 2f26c49393..cfa1226e6d 100644 --- a/src/SharedWeb/Utilities/ServiceCollectionExtensions.cs +++ b/src/SharedWeb/Utilities/ServiceCollectionExtensions.cs @@ -48,6 +48,8 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc.Localization; +using Microsoft.Azure.Cosmos.Fluent; +using Microsoft.Extensions.Caching.Cosmos; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -693,16 +695,33 @@ public static class ServiceCollectionExtensions this IServiceCollection services, GlobalSettings globalSettings) { - if (globalSettings.SelfHosted || string.IsNullOrEmpty(globalSettings.Redis.ConnectionString)) + if (!string.IsNullOrEmpty(globalSettings.DistributedCache?.Redis?.ConnectionString)) + { + services.AddStackExchangeRedisCache(options => + { + options.Configuration = globalSettings.DistributedCache.Redis.ConnectionString; + }); + } + else { services.AddDistributedMemoryCache(); - return; } - services.AddStackExchangeRedisCache(options => + if (!string.IsNullOrEmpty(globalSettings.DistributedCache?.Cosmos?.ConnectionString)) { - options.Configuration = globalSettings.Redis.ConnectionString; - }); + services.AddKeyedSingleton("persistent", (s, _) => + new CosmosCache(new CosmosCacheOptions + { + DatabaseName = "cache", + ContainerName = "default", + CreateIfNotExists = false, + ClientBuilder = new CosmosClientBuilder(globalSettings.DistributedCache?.Cosmos?.ConnectionString) + })); + } + else + { + services.AddKeyedSingleton("persistent"); + } } public static IServiceCollection AddOptionality(this IServiceCollection services)