Revert memory cache

This commit is contained in:
2025-06-21 22:05:27 -06:00
parent 31b26399a9
commit ff24c06934
7 changed files with 27 additions and 135 deletions

View File

@@ -1,10 +1,8 @@
using Core.Blueprint.Caching.Adapters;
using Core.Blueprint.Caching.Contracts;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Core.Blueprint.Caching.Configuration
namespace Core.Blueprint.Redis.Configuration
{
/// <summary>
/// Provides extension methods for registering Redis-related services in the DI container.
@@ -19,30 +17,23 @@ namespace Core.Blueprint.Caching.Configuration
/// <returns>The updated service collection.</returns>
public static IServiceCollection AddRedis(this IServiceCollection services, IConfiguration configuration)
{
// TODO for the following variable we'll need to add in the appsettings.json the following config: "UseRedisCache": true,
bool useRedis = configuration.GetValue<bool>("UseRedisCache");
if (useRedis)
// Retrieve the Redis connection string from the configuration.
// Get Redis configuration section
var redisConnectionString = configuration.GetSection("ConnectionStrings:Redis").Value;
if (string.IsNullOrEmpty(redisConnectionString))
{
var redisConnectionString = configuration.GetSection("ConnectionStrings:Redis").Value;
if (string.IsNullOrEmpty(redisConnectionString))
{
throw new InvalidOperationException("Redis connection is not configured.");
}
services.AddSingleton<ICacheProvider>(provider =>
new RedisCacheProvider(redisConnectionString, provider.GetRequiredService<ILogger<RedisCacheProvider>>()));
}
else
{
services.AddMemoryCache();
services.AddSingleton<ICacheProvider, MemoryCacheProvider>();
throw new InvalidOperationException("Redis connection is not configured.");
}
// Register RedisCacheProvider
services.AddSingleton<IRedisCacheProvider>(provider =>
new RedisCacheProvider(redisConnectionString, provider.GetRequiredService<ILogger<RedisCacheProvider>>()));
// Get CacheSettings and register with the ICacheSettings interface
var cacheSettings = configuration.GetSection("CacheSettings").Get<CacheSettings>();
if (cacheSettings == null)
{
throw new InvalidOperationException("CacheSettings section is not configured.");
throw new InvalidOperationException("Redis CacheSettings section is not configured.");
}
services.AddSingleton<ICacheSettings>(cacheSettings);