Compare commits
2 Commits
351cc28181
...
fbfa21f89a
| Author | SHA1 | Date | |
|---|---|---|---|
| fbfa21f89a | |||
| e3cdf1fb32 |
@@ -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
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// <param name="connectionString">The Redis connection string.</param>
|
||||
/// <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)
|
||||
{
|
||||
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());
|
||||
|
||||
configurationOptions.AbortOnConnectFail = false;
|
||||
var connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions);
|
||||
|
||||
connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Successfully connected to Redis.");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user