-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRedisServiceProvider.cs
38 lines (33 loc) · 1.46 KB
/
RedisServiceProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Snd.Sdk.Storage.Base;
using SnD.Sdk.Storage.Cache;
using Snd.Sdk.Storage.Providers.Configurations;
using StackExchange.Redis;
namespace Snd.Sdk.Storage.Providers;
/// <summary>
/// Provider for Redis service.
/// </summary>
public static class RedisServiceProvider
{
/// <summary>
/// Adds Redis connection to the DI container.
/// </summary>
/// <param name="services">Service collection (DI container).</param>
/// <param name="appConfiguration">Application configuration with "RedisServiceProvider" section configured according to <see cref="RedisServiceProvider"/>.</param>
/// <returns></returns>
public static IServiceCollection AddRedisCache(this IServiceCollection services, IConfiguration appConfiguration)
{
var redisConfiguration = new RedisConfiguration();
appConfiguration.GetSection(nameof(RedisServiceProvider)).Bind(redisConfiguration);
var options = new ConfigurationOptions()
{
EndPoints = { { redisConfiguration.Host, redisConfiguration.Port } },
Password = redisConfiguration.Password,
Ssl = redisConfiguration.UseSsl,
DefaultDatabase = redisConfiguration.DatabaseNumber
};
services.AddSingleton<IDatabase>(sp => ConnectionMultiplexer.Connect(options).GetDatabase());
return services.AddSingleton<IRedisService, RedisService>();
}
}