-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cache.Configuration.cs
262 lines (191 loc) · 8.68 KB
/
Cache.Configuration.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#region Related components
using System;
using System.Xml;
using System.Net;
using System.Collections.Generic;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using Microsoft.Extensions.Options;
#endregion
namespace net.vieapps.Components.Caching
{
/// <summary>
/// Caching configuration
/// </summary>
public interface ICacheConfiguration
{
string Provider { get; }
string RegionName { get; }
int ExpirationTime { get; }
IList<CacheServer> Servers { get; }
string Options { get; }
MemcachedProtocol Protocol { get; }
ISocketPoolConfiguration SocketPool { get; }
IAuthenticationConfiguration Authentication { get; }
string KeyTransformer { get; }
string Transcoder { get; }
string NodeLocator { get; }
}
/// <summary>
/// Caching configuration
/// </summary>
[Serializable]
public class CacheConfiguration : ICacheConfiguration
{
public CacheConfiguration() { }
public string Provider { get; set; } = "Redis";
public string RegionName { get; set; } = "VIEApps-NGX-Cache";
public int ExpirationTime { get; set; } = 30;
public IList<CacheServer> Servers { get; set; } = new List<CacheServer>();
public string Options { get; set; } = "";
public MemcachedProtocol Protocol { get; set; } = MemcachedProtocol.Binary;
public ISocketPoolConfiguration SocketPool { get; set; } = new SocketPoolConfiguration();
public IAuthenticationConfiguration Authentication { get; set; } = new AuthenticationConfiguration();
public string KeyTransformer { get; set; } = "";
public string Transcoder { get; set; } = "";
public string NodeLocator { get; set; } = "";
public CacheConfiguration(IOptions<CacheOptions> options)
{
if (options == null)
throw new ArgumentNullException(nameof(options));
var configuration = options.Value;
this.Provider = configuration.Provider;
this.RegionName = configuration.RegionName;
this.ExpirationTime = configuration.ExpirationTime;
this.Servers = configuration.Servers;
this.Options = configuration.Options;
this.Protocol = configuration.Protocol;
this.SocketPool = configuration.SocketPool;
this.Authentication = configuration.Authentication;
this.KeyTransformer = configuration.KeyTransformer;
this.Transcoder = configuration.Transcoder;
this.NodeLocator = configuration.NodeLocator;
}
public CacheConfiguration(CacheConfigurationSectionHandler configuration)
{
if (configuration == null)
throw new ArgumentNullException(nameof(configuration), "No configuration is found");
this.Provider = configuration.Section.Attributes["provider"]?.Value ?? "Redis";
this.RegionName = configuration.Section.Attributes["region"]?.Value ?? "VIEApps-NGX-Cache";
if (Int32.TryParse(configuration.Section.Attributes["expirationTime"]?.Value ?? "30", out var intValue))
this.ExpirationTime = intValue;
if (configuration.Section.SelectNodes("servers/add") is XmlNodeList servers)
foreach (XmlNode server in servers)
{
var type = server.Attributes["type"]?.Value ?? "Redis";
this.Servers.Add(new CacheServer(server.Attributes["address"]?.Value ?? "localhost", Int32.TryParse(server.Attributes["port"]?.Value ?? (type.ToLower().Equals("redis") ? "6379" : "11211"), out var port) ? port : type.ToLower().Equals("redis") ? 6379 : 11211, type));
}
if (configuration.Section.SelectSingleNode("options") is XmlNode options)
foreach (XmlAttribute option in options.Attributes)
if (!string.IsNullOrWhiteSpace(option.Value))
this.Options += (this.Options != "" ? "," : "") + option.Name + "=" + option.Value;
if (Enum.TryParse(configuration.Section.Attributes["protocol"]?.Value ?? "Binary", out MemcachedProtocol protocol))
this.Protocol = protocol;
if (configuration.Section.SelectSingleNode("socketPool") is XmlNode socketpool)
{
if (Int32.TryParse(socketpool.Attributes["maxPoolSize"]?.Value, out intValue))
this.SocketPool.MaxPoolSize = intValue;
if (Int32.TryParse(socketpool.Attributes["minPoolSize"]?.Value, out intValue))
this.SocketPool.MinPoolSize = intValue;
if (TimeSpan.TryParse(socketpool.Attributes["connectionTimeout"]?.Value, out var timespanValue))
this.SocketPool.ConnectionTimeout = timespanValue;
if (TimeSpan.TryParse(socketpool.Attributes["deadTimeout"]?.Value, out timespanValue))
this.SocketPool.DeadTimeout = timespanValue;
if (TimeSpan.TryParse(socketpool.Attributes["queueTimeout"]?.Value, out timespanValue))
this.SocketPool.QueueTimeout = timespanValue;
if (TimeSpan.TryParse(socketpool.Attributes["receiveTimeout"]?.Value, out timespanValue))
this.SocketPool.ReceiveTimeout = timespanValue;
if (Boolean.TryParse(socketpool.Attributes["noDelay"]?.Value, out var boolValue))
this.SocketPool.NoDelay = boolValue;
if ("throttling" == socketpool.Attributes["failurePolicy"]?.Value)
this.SocketPool.FailurePolicyFactory = new ThrottlingFailurePolicyFactory(Int32.TryParse(socketpool.Attributes["failureThreshold"]?.Value, out intValue) ? intValue : 4, TimeSpan.TryParse(socketpool.Attributes["resetAfter"]?.Value, out timespanValue) ? timespanValue : TimeSpan.FromSeconds(5));
}
if (configuration.Section.SelectSingleNode("authentication") is XmlNode authentication)
if (authentication.Attributes["type"]?.Value != null)
try
{
this.Authentication.Type = authentication.Attributes["type"].Value;
if (authentication.Attributes["zone"]?.Value != null)
this.Authentication.Parameters.Add("zone", authentication.Attributes["zone"].Value);
if (authentication.Attributes["userName"]?.Value != null)
this.Authentication.Parameters.Add("userName", authentication.Attributes["userName"].Value);
if (authentication.Attributes["password"]?.Value != null)
this.Authentication.Parameters.Add("password", authentication.Attributes["password"].Value);
}
catch { }
if (configuration.Section.SelectSingleNode("keyTransformer") is XmlNode keyTransformer)
this.KeyTransformer = keyTransformer.Attributes["type"]?.Value;
if (configuration.Section.SelectSingleNode("transcoder") is XmlNode transcoder)
this.Transcoder = transcoder.Attributes["type"]?.Value;
if (configuration.Section.SelectSingleNode("nodeLocator") is XmlNode nodeLocator)
this.NodeLocator = nodeLocator.Attributes["type"]?.Value;
}
}
// -----------------------------------------------------------
/// <summary>
/// Caching options
/// </summary>
[Serializable]
public class CacheOptions : IOptions<CacheOptions>
{
public CacheOptions() { }
public string Provider { get; set; } = "Redis";
public string RegionName { get; set; } = "VIEApps-NGX-Cache";
public int ExpirationTime { get; set; } = 30;
public List<CacheServer> Servers { get; set; } = new List<CacheServer>();
public string Options { get; set; } = "";
public MemcachedProtocol Protocol { get; set; } = MemcachedProtocol.Binary;
public SocketPoolConfiguration SocketPool { get; set; } = new SocketPoolConfiguration();
public AuthenticationConfiguration Authentication { get; set; } = new AuthenticationConfiguration();
public string KeyTransformer { get; set; } = "";
public string Transcoder { get; set; } = "";
public string NodeLocator { get; set; } = "";
public CacheOptions Value => this;
}
// -----------------------------------------------------------
/// <summary>
/// Information of a distributed cache server
/// </summary>
[Serializable]
public class CacheServer
{
public string Address { get; set; }
public int Port { get; set; }
public string Type { get; set; } = "Redis";
public CacheServer() { }
public CacheServer(string address, int port, string type = "Redis")
{
this.Address = address;
this.Port = port;
this.Type = type;
}
}
// -----------------------------------------------------------
/// <summary>
/// Redis cache configuration
/// </summary>
[Serializable]
public class RedisClientConfiguration
{
public RedisClientConfiguration() { }
public List<IPEndPoint> Servers { get; internal set; } = new List<IPEndPoint>();
public string Options { get; internal set; } = "";
}
// -----------------------------------------------------------
/// <summary>
/// Redis cache options
/// </summary>
[Serializable]
public class RedisClientOptions : IOptions<RedisClientOptions>
{
public RedisClientOptions() { }
public List<IPEndPoint> Servers { get; set; } = new List<IPEndPoint>();
public string Options { get; set; } = "";
public RedisClientOptions Value => this;
}
// -----------------------------------------------------------
/// <summary>
/// Configuration section handler of the caching component
/// </summary>
public class CacheConfigurationSectionHandler : MemcachedClientConfigurationSectionHandler { }
}