From 9ecc4794fe3f32aae410fc61fc5fb272b5913323 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 8 Feb 2024 14:34:53 -0500 Subject: [PATCH] [PM-6177] Persistent distributed cache using Cosmos (#3772) * cosmos distributed cache * rename container to default --- .../Utilities/ServiceCollectionExtensions.cs | 4 +-- src/Core/Core.csproj | 1 + src/Core/Settings/GlobalSettings.cs | 8 ++++- .../Utilities/ServiceCollectionExtensions.cs | 29 +++++++++++++++---- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/Api/Utilities/ServiceCollectionExtensions.cs b/src/Api/Utilities/ServiceCollectionExtensions.cs index a98d6722d..4c6664bf7 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 642900e26..aa6f5778b 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 efa44c440..0739a4c81 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 2f26c4939..cfa1226e6 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)