Fix Redis cache provider
This commit is contained in:
parent
73b909f780
commit
e3cdf1fb32
@ -1,6 +1,7 @@
|
|||||||
using Azure.Identity;
|
using Azure.Identity;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using StackExchange.Redis;
|
using StackExchange.Redis;
|
||||||
|
using System;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace Core.Blueprint.Redis
|
namespace Core.Blueprint.Redis
|
||||||
@ -29,20 +30,32 @@ namespace Core.Blueprint.Redis
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 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.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="connectionString">The Redis connection string.</param>
|
/// <param name="connectionString">The Redis connection string.</param>
|
||||||
/// <returns>An <see cref="IDatabase"/> instance representing the Redis cache database.</returns>
|
/// <returns>An <see cref="IDatabase"/> instance representing the Redis cache database.</returns>
|
||||||
/// <exception cref="Exception">Thrown when the connection to Redis fails.</exce
|
/// <exception cref="Exception">Thrown when the connection to Redis fails.</exception>
|
||||||
async Task<IDatabase> InitializeRedisAsync(string connectionString)
|
async Task<IDatabase> InitializeRedisAsync(string connectionString)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var configurationOptions = await ConfigurationOptions.Parse($"{connectionString}")
|
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty;
|
||||||
|
ConnectionMultiplexer connectionMultiplexer;
|
||||||
|
|
||||||
|
if (environment.Equals("Local", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(connectionString);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var configurationOptions = await ConfigurationOptions.Parse(connectionString)
|
||||||
.ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential());
|
.ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential());
|
||||||
|
|
||||||
configurationOptions.AbortOnConnectFail = false;
|
configurationOptions.AbortOnConnectFail = false;
|
||||||
var connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions);
|
|
||||||
|
connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions);
|
||||||
|
}
|
||||||
|
|
||||||
_logger.LogInformation("Successfully connected to Redis.");
|
_logger.LogInformation("Successfully connected to Redis.");
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user