43 lines
1.9 KiB
C#
43 lines
1.9 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Core.Blueprint.Redis.Configuration
|
|
{
|
|
/// <summary>
|
|
/// Provides extension methods for registering Redis-related services in the DI container.
|
|
/// </summary>
|
|
public static class RegisterBlueprint
|
|
{
|
|
/// <summary>
|
|
/// Adds Redis caching services to the service collection.
|
|
/// </summary>
|
|
/// <param name="services">The service collection to register the services into.</param>
|
|
/// <param name="configuration">The application configuration object.</param>
|
|
/// <returns>The updated service collection.</returns>
|
|
public static IServiceCollection AddRedis(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
// Retrieve the Redis connection string from the configuration.
|
|
// Get Redis configuration section
|
|
var redisConnectionString = configuration.GetSection("ConnectionStrings:Redis").Value;
|
|
if (string.IsNullOrEmpty(redisConnectionString))
|
|
{
|
|
throw new InvalidOperationException("Redis connection is not configured.");
|
|
}
|
|
|
|
// Register RedisCacheProvider
|
|
services.AddSingleton<IRedisCacheProvider>(provider =>
|
|
new RedisCacheProvider(redisConnectionString, provider.GetRequiredService<ILogger<RedisCacheProvider>>(), configuration));
|
|
|
|
// Get CacheSettings and register with the ICacheSettings interface
|
|
var cacheSettings = configuration.GetSection("CacheSettings").Get<CacheSettings>();
|
|
if (cacheSettings == null)
|
|
{
|
|
throw new InvalidOperationException("Redis CacheSettings section is not configured.");
|
|
}
|
|
services.AddSingleton<ICacheSettings>(cacheSettings);
|
|
|
|
return services;
|
|
}
|
|
}
|
|
} |