-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathRedisDatabaseProvider.cs
143 lines (125 loc) · 4.61 KB
/
RedisDatabaseProvider.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using Elastic.Apm.StackExchange.Redis;
namespace EasyCaching.Redis
{
using StackExchange.Redis;
using StackExchange.Redis.KeyspaceIsolation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
/// <summary>
/// Redis database provider.
/// </summary>
public class RedisDatabaseProvider : IRedisDatabaseProvider
{
/// <summary>
/// The options.
/// </summary>
private readonly RedisDBOptions _options;
private readonly bool _useApm;
/// <summary>
/// The connection multiplexer.
/// </summary>
private Lazy<ConnectionMultiplexer> _connectionMultiplexer;
public RedisDatabaseProvider(string name, RedisOptions options)
{
_options = options.DBConfig;
_useApm = options.UseApm;
_connectionMultiplexer = new Lazy<ConnectionMultiplexer>(CreateConnectionMultiplexer);
_name = name;
}
private readonly string _name;
public string DBProviderName => this._name;
/// <summary>
/// Gets the database connection.
/// </summary>
public IDatabase GetDatabase()
{
try
{
var database = _connectionMultiplexer.Value.GetDatabase();
if (!string.IsNullOrEmpty(_options.KeyPrefix))
database = database.WithKeyPrefix(_options.KeyPrefix);
if (_useApm)
_connectionMultiplexer.Value.UseElasticApm();
return database;
}
catch (Exception)
{
_connectionMultiplexer = new Lazy<ConnectionMultiplexer>(CreateConnectionMultiplexer);
throw;
}
}
/// <summary>
/// Gets the server list.
/// </summary>
/// <returns>The server list.</returns>
public IEnumerable<IServer> GetServerList()
{
var endpoints = GetMastersServersEndpoints();
foreach (var endpoint in endpoints)
{
yield return _connectionMultiplexer.Value.GetServer(endpoint);
}
}
/// <summary>
/// Creates the connection multiplexer.
/// </summary>
/// <returns>The connection multiplexer.</returns>
private ConnectionMultiplexer CreateConnectionMultiplexer()
{
if (_options.ConfigurationOptions != null)
return ConnectionMultiplexer.Connect(_options.ConfigurationOptions);
if (string.IsNullOrWhiteSpace(_options.Configuration))
{
var configurationOptions = new ConfigurationOptions
{
ConnectTimeout = _options.ConnectionTimeout,
User = _options.Username,
Password = _options.Password,
Ssl = _options.IsSsl,
SslHost = _options.SslHost,
AllowAdmin = _options.AllowAdmin,
DefaultDatabase = _options.Database,
AbortOnConnectFail = _options.AbortOnConnectFail,
};
foreach (var endpoint in _options.Endpoints)
{
configurationOptions.EndPoints.Add(endpoint.Host, endpoint.Port);
}
return ConnectionMultiplexer.Connect(configurationOptions);
}
else
{
return ConnectionMultiplexer.Connect(_options.Configuration);
}
}
/// <summary>
/// Gets the masters servers endpoints.
/// </summary>
private List<EndPoint> GetMastersServersEndpoints()
{
var masters = new List<EndPoint>();
foreach (var ep in _connectionMultiplexer.Value.GetEndPoints())
{
var server = _connectionMultiplexer.Value.GetServer(ep);
if (server.IsConnected)
{
//Cluster
if (server.ServerType == ServerType.Cluster)
{
masters.AddRange(server.ClusterConfiguration.Nodes.Where(n => !n.IsReplica).Select(n => n.EndPoint));
break;
}
// Single , Master-Slave
if (server.ServerType == ServerType.Standalone && !server.IsReplica)
{
masters.Add(ep);
break;
}
}
}
return masters;
}
}
}