From e3cdf1fb327038f08eda5cd72aca6fa348deb3fc Mon Sep 17 00:00:00 2001 From: Sergio Matias Date: Sun, 22 Jun 2025 19:12:58 -0600 Subject: [PATCH] Fix Redis cache provider --- Core.Blueprint.Redis/RedisCacheProvider.cs | 25 ++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Core.Blueprint.Redis/RedisCacheProvider.cs b/Core.Blueprint.Redis/RedisCacheProvider.cs index 525e310..47a275d 100644 --- a/Core.Blueprint.Redis/RedisCacheProvider.cs +++ b/Core.Blueprint.Redis/RedisCacheProvider.cs @@ -1,6 +1,7 @@ using Azure.Identity; using Microsoft.Extensions.Logging; using StackExchange.Redis; +using System; using System.Text.Json; namespace Core.Blueprint.Redis @@ -29,20 +30,32 @@ namespace Core.Blueprint.Redis } /// - /// Initializes and establishes a connection to Redis using the provided connection string. + /// Initializes and establishes a connection to Redis based on the environment. + /// Uses a local connection in development, and Azure with token credentials in other environments. /// /// The Redis connection string. /// An instance representing the Redis cache database. - /// Thrown when the connection to Redis fails.Thrown when the connection to Redis fails. async Task InitializeRedisAsync(string connectionString) { try { - var configurationOptions = await ConfigurationOptions.Parse($"{connectionString}") - .ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential()); + var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty; + ConnectionMultiplexer connectionMultiplexer; - configurationOptions.AbortOnConnectFail = false; - var connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions); + if (environment.Equals("Local", StringComparison.OrdinalIgnoreCase)) + { + connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(connectionString); + } + else + { + var configurationOptions = await ConfigurationOptions.Parse(connectionString) + .ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential()); + + configurationOptions.AbortOnConnectFail = false; + + connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions); + } _logger.LogInformation("Successfully connected to Redis.");