using Core.Blueprint.Caching.Adapters;
using Core.Blueprint.Caching.Contracts;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Core.Blueprint.Caching.Configuration
{
///
/// Provides extension methods for registering Redis-related services in the DI container.
///
public static class RegisterBlueprint
{
///
/// Adds Redis caching services to the service collection.
///
/// The service collection to register the services into.
/// The application configuration object.
/// The updated service collection.
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("UseRedisCache");
if (useRedis)
{
var redisConnectionString = configuration.GetSection("ConnectionStrings:Redis").Value;
if (string.IsNullOrEmpty(redisConnectionString))
{
throw new InvalidOperationException("Redis connection is not configured.");
}
services.AddSingleton(provider =>
new RedisCacheProvider(redisConnectionString, provider.GetRequiredService>()));
}
else
{
services.AddMemoryCache();
services.AddSingleton();
}
var cacheSettings = configuration.GetSection("CacheSettings").Get();
if (cacheSettings == null)
{
throw new InvalidOperationException("CacheSettings section is not configured.");
}
services.AddSingleton(cacheSettings);
return services;
}
}
}