Disable database distributed cache in non-self-hosted environments (#6783)

* Disable database distributed cache in non-self-hosted environments

* Added distributed memory cache as a final fallback option
This commit is contained in:
Brant DeBow 2025-12-31 11:59:59 -05:00 committed by GitHub
parent 665be6bfb0
commit cc03842f5f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -661,8 +661,9 @@ public static class ServiceCollectionExtensions
}
/// <summary>
/// Adds an implementation of <see cref="IDistributedCache"/> to the service collection. Uses a memory
/// cache if self hosted or no Redis connection string is available in GlobalSettings.
/// Adds an implementation of <see cref="IDistributedCache"/> to the service collection. Uses Redis
/// if a connection string is available in GlobalSettings, a database-backed distributed cache if
/// self-hosted or a distributed memory cache as a final fallback.
/// </summary>
public static void AddDistributedCache(
this IServiceCollection services,
@ -677,19 +678,26 @@ public static class ServiceCollectionExtensions
}
else
{
var (databaseProvider, databaseConnectionString) = GetDatabaseProvider(globalSettings);
if (databaseProvider == SupportedDatabaseProviders.SqlServer)
if (globalSettings.SelfHosted)
{
services.AddDistributedSqlServerCache(o =>
var (databaseProvider, databaseConnectionString) = GetDatabaseProvider(globalSettings);
if (databaseProvider == SupportedDatabaseProviders.SqlServer)
{
o.ConnectionString = databaseConnectionString;
o.SchemaName = "dbo";
o.TableName = "Cache";
});
services.AddDistributedSqlServerCache(o =>
{
o.ConnectionString = databaseConnectionString;
o.SchemaName = "dbo";
o.TableName = "Cache";
});
}
else
{
services.AddSingleton<IDistributedCache, EntityFrameworkCache>();
}
}
else
{
services.AddSingleton<IDistributedCache, EntityFrameworkCache>();
services.AddDistributedMemoryCache();
}
}