diff --git a/src/SharedWeb/Utilities/ServiceCollectionExtensions.cs b/src/SharedWeb/Utilities/ServiceCollectionExtensions.cs index 91047d98bc..8f5dfdf3f4 100644 --- a/src/SharedWeb/Utilities/ServiceCollectionExtensions.cs +++ b/src/SharedWeb/Utilities/ServiceCollectionExtensions.cs @@ -661,8 +661,9 @@ public static class ServiceCollectionExtensions } /// - /// Adds an implementation of to the service collection. Uses a memory - /// cache if self hosted or no Redis connection string is available in GlobalSettings. + /// Adds an implementation of 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. /// 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(); + } } else { - services.AddSingleton(); + services.AddDistributedMemoryCache(); } }