Deactivate redis using a flag
This commit is contained in:
@@ -27,7 +27,7 @@ namespace Core.Blueprint.Redis.Configuration
|
|||||||
|
|
||||||
// Register RedisCacheProvider
|
// Register RedisCacheProvider
|
||||||
services.AddSingleton<IRedisCacheProvider>(provider =>
|
services.AddSingleton<IRedisCacheProvider>(provider =>
|
||||||
new RedisCacheProvider(redisConnectionString, provider.GetRequiredService<ILogger<RedisCacheProvider>>()));
|
new RedisCacheProvider(redisConnectionString, provider.GetRequiredService<ILogger<RedisCacheProvider>>(), configuration));
|
||||||
|
|
||||||
// Get CacheSettings and register with the ICacheSettings interface
|
// Get CacheSettings and register with the ICacheSettings interface
|
||||||
var cacheSettings = configuration.GetSection("CacheSettings").Get<CacheSettings>();
|
var cacheSettings = configuration.GetSection("CacheSettings").Get<CacheSettings>();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
using Azure.Identity;
|
using Azure.Identity;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
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
|
||||||
@@ -13,6 +13,7 @@ namespace Core.Blueprint.Redis
|
|||||||
{
|
{
|
||||||
private IDatabase _cacheDatabase = null!;
|
private IDatabase _cacheDatabase = null!;
|
||||||
private readonly ILogger<RedisCacheProvider> _logger;
|
private readonly ILogger<RedisCacheProvider> _logger;
|
||||||
|
private readonly bool _useRedis;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="RedisCacheProvider"/> class.
|
/// Initializes a new instance of the <see cref="RedisCacheProvider"/> class.
|
||||||
@@ -20,12 +21,13 @@ namespace Core.Blueprint.Redis
|
|||||||
/// <param name="connectionString">The Redis connection string.</param>
|
/// <param name="connectionString">The Redis connection string.</param>
|
||||||
/// <param name="logger">The logger instance for logging operations.</param>
|
/// <param name="logger">The logger instance for logging operations.</param>
|
||||||
/// <exception cref="ArgumentNullException">Thrown when connection string is null or empty.</exception>
|
/// <exception cref="ArgumentNullException">Thrown when connection string is null or empty.</exception>
|
||||||
public RedisCacheProvider(string connectionString, ILogger<RedisCacheProvider> logger)
|
public RedisCacheProvider(string connectionString, ILogger<RedisCacheProvider> logger, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(connectionString))
|
if (string.IsNullOrWhiteSpace(connectionString))
|
||||||
throw new ArgumentNullException(nameof(connectionString), "Redis connection string cannot be null or empty.");
|
throw new ArgumentNullException(nameof(connectionString), "Redis connection string cannot be null or empty.");
|
||||||
|
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_useRedis = configuration.GetValue<bool>("UseRedisCache", false);
|
||||||
_cacheDatabase = InitializeRedisAsync(connectionString).GetAwaiter().GetResult();
|
_cacheDatabase = InitializeRedisAsync(connectionString).GetAwaiter().GetResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,9 +38,11 @@ namespace Core.Blueprint.Redis
|
|||||||
/// <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.</exception>
|
/// <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
|
||||||
|
{
|
||||||
|
if (_useRedis)
|
||||||
{
|
{
|
||||||
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty;
|
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty;
|
||||||
ConnectionMultiplexer connectionMultiplexer;
|
ConnectionMultiplexer connectionMultiplexer;
|
||||||
@@ -61,6 +65,9 @@ namespace Core.Blueprint.Redis
|
|||||||
|
|
||||||
return connectionMultiplexer.GetDatabase();
|
return connectionMultiplexer.GetDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error establishing Redis connection.");
|
_logger.LogError(ex, "Error establishing Redis connection.");
|
||||||
@@ -77,8 +84,11 @@ namespace Core.Blueprint.Redis
|
|||||||
public async ValueTask<TEntity> GetAsync<TEntity>(string key)
|
public async ValueTask<TEntity> GetAsync<TEntity>(string key)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
if (_useRedis is not false)
|
||||||
{
|
{
|
||||||
var value = await _cacheDatabase.StringGetAsync(key);
|
var value = await _cacheDatabase.StringGetAsync(key);
|
||||||
|
|
||||||
if (value.IsNullOrEmpty)
|
if (value.IsNullOrEmpty)
|
||||||
{
|
{
|
||||||
_logger.LogInformation($"Cache miss for key: {key}");
|
_logger.LogInformation($"Cache miss for key: {key}");
|
||||||
@@ -88,6 +98,9 @@ namespace Core.Blueprint.Redis
|
|||||||
_logger.LogInformation($"Cache hit for key: {key}");
|
_logger.LogInformation($"Cache hit for key: {key}");
|
||||||
return JsonSerializer.Deserialize<TEntity>(value);
|
return JsonSerializer.Deserialize<TEntity>(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, $"Error getting cache item with key {key}");
|
_logger.LogError(ex, $"Error getting cache item with key {key}");
|
||||||
@@ -104,11 +117,14 @@ namespace Core.Blueprint.Redis
|
|||||||
public async ValueTask SetAsync<TEntity>(string key, TEntity value, TimeSpan? expiry = null)
|
public async ValueTask SetAsync<TEntity>(string key, TEntity value, TimeSpan? expiry = null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
if (_useRedis is not false)
|
||||||
{
|
{
|
||||||
var json = JsonSerializer.Serialize(value);
|
var json = JsonSerializer.Serialize(value);
|
||||||
await _cacheDatabase.StringSetAsync(key, json, expiry);
|
await _cacheDatabase.StringSetAsync(key, json, expiry);
|
||||||
_logger.LogInformation($"Cache item set with key: {key}");
|
_logger.LogInformation($"Cache item set with key: {key}");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, $"Error setting cache item with key {key}");
|
_logger.LogError(ex, $"Error setting cache item with key {key}");
|
||||||
@@ -123,10 +139,13 @@ namespace Core.Blueprint.Redis
|
|||||||
public async ValueTask RemoveAsync(string key)
|
public async ValueTask RemoveAsync(string key)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
if (_useRedis is not false)
|
||||||
{
|
{
|
||||||
await _cacheDatabase.KeyDeleteAsync(key);
|
await _cacheDatabase.KeyDeleteAsync(key);
|
||||||
_logger.LogInformation($"Cache item removed with key: {key}");
|
_logger.LogInformation($"Cache item removed with key: {key}");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, $"Error removing cache item with key {key}");
|
_logger.LogError(ex, $"Error removing cache item with key {key}");
|
||||||
@@ -142,10 +161,14 @@ namespace Core.Blueprint.Redis
|
|||||||
public async ValueTask<bool> ExistsAsync(string key)
|
public async ValueTask<bool> ExistsAsync(string key)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
if (_useRedis is not false)
|
||||||
{
|
{
|
||||||
var exists = await _cacheDatabase.KeyExistsAsync(key);
|
var exists = await _cacheDatabase.KeyExistsAsync(key);
|
||||||
_logger.LogInformation($"Cache item exists check for key: {key} - {exists}");
|
_logger.LogInformation($"Cache item exists check for key: {key} - {exists}");
|
||||||
return exists;
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -162,6 +185,8 @@ namespace Core.Blueprint.Redis
|
|||||||
public async ValueTask RefreshAsync(string key, TimeSpan? expiry = null)
|
public async ValueTask RefreshAsync(string key, TimeSpan? expiry = null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
if (_useRedis is not false)
|
||||||
{
|
{
|
||||||
var value = await _cacheDatabase.StringGetAsync(key);
|
var value = await _cacheDatabase.StringGetAsync(key);
|
||||||
if (!value.IsNullOrEmpty)
|
if (!value.IsNullOrEmpty)
|
||||||
@@ -174,6 +199,7 @@ namespace Core.Blueprint.Redis
|
|||||||
_logger.LogWarning($"Cache item with key: {key} does not exist, cannot refresh");
|
_logger.LogWarning($"Cache item with key: {key} does not exist, cannot refresh");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, $"Error refreshing cache item with key {key}");
|
_logger.LogError(ex, $"Error refreshing cache item with key {key}");
|
||||||
|
|||||||
Reference in New Issue
Block a user