1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-25 12:45:18 +01:00

[PM-6177] Persistent distributed cache using Cosmos (#3772)

* cosmos distributed cache

* rename container to default
This commit is contained in:
Kyle Spearrin 2024-02-08 14:34:53 -05:00 committed by GitHub
parent 3e73f1cb4a
commit 9ecc4794fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 8 deletions

View File

@ -90,9 +90,9 @@ public static class ServiceCollectionExtensions
builder.AddSqlServer(globalSettings.SqlServer.ConnectionString); 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)) if (CoreHelpers.SettingHasValue(globalSettings.Storage.ConnectionString))

View File

@ -38,6 +38,7 @@
<PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="1.0.8" /> <PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="1.0.8" />
<PackageReference Include="Microsoft.Azure.NotificationHubs" Version="4.1.0" /> <PackageReference Include="Microsoft.Azure.NotificationHubs" Version="4.1.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.5" /> <PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.5" />
<PackageReference Include="Microsoft.Extensions.Caching.Cosmos" Version="1.6.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="6.0.25" /> <PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="6.0.25" />

View File

@ -55,7 +55,7 @@ public class GlobalSettings : IGlobalSettings
public virtual MailSettings Mail { get; set; } = new MailSettings(); public virtual MailSettings Mail { get; set; } = new MailSettings();
public virtual IConnectionStringSettings Storage { get; set; } = new ConnectionStringSettings(); public virtual IConnectionStringSettings Storage { get; set; } = new ConnectionStringSettings();
public virtual ConnectionStringSettings Events { 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 NotificationsSettings Notifications { get; set; } = new NotificationsSettings();
public virtual IFileStorageSettings Attachment { get; set; } public virtual IFileStorageSettings Attachment { get; set; }
public virtual FileStorageSettings Send { get; set; } public virtual FileStorageSettings Send { get; set; }
@ -550,4 +550,10 @@ public class GlobalSettings : IGlobalSettings
public string FlagDataFilePath { get; set; } = "flags.json"; public string FlagDataFilePath { get; set; } = "flags.json";
public Dictionary<string, string> FlagValues { get; set; } = new Dictionary<string, string>(); public Dictionary<string, string> FlagValues { get; set; } = new Dictionary<string, string>();
} }
public class DistributedCacheSettings
{
public virtual IConnectionStringSettings Redis { get; set; } = new ConnectionStringSettings();
public virtual IConnectionStringSettings Cosmos { get; set; } = new ConnectionStringSettings();
}
} }

View File

@ -48,6 +48,8 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.Localization; using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.Azure.Cosmos.Fluent;
using Microsoft.Extensions.Caching.Cosmos;
using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -693,16 +695,33 @@ public static class ServiceCollectionExtensions
this IServiceCollection services, this IServiceCollection services,
GlobalSettings globalSettings) 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(); services.AddDistributedMemoryCache();
return;
} }
services.AddStackExchangeRedisCache(options => if (!string.IsNullOrEmpty(globalSettings.DistributedCache?.Cosmos?.ConnectionString))
{ {
options.Configuration = globalSettings.Redis.ConnectionString; services.AddKeyedSingleton<IDistributedCache>("persistent", (s, _) =>
}); new CosmosCache(new CosmosCacheOptions
{
DatabaseName = "cache",
ContainerName = "default",
CreateIfNotExists = false,
ClientBuilder = new CosmosClientBuilder(globalSettings.DistributedCache?.Cosmos?.ConnectionString)
}));
}
else
{
services.AddKeyedSingleton<IDistributedCache, MemoryDistributedCache>("persistent");
}
} }
public static IServiceCollection AddOptionality(this IServiceCollection services) public static IServiceCollection AddOptionality(this IServiceCollection services)