Skip to content

Commit e216227

Browse files
authored
Merge pull request #243 from cnblogs/fix-nodelocator-setting
fix: update NodeLocator when adding server
2 parents 46c593d + 9727bda commit e216227

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

src/Enyim.Caching/Configuration/MemcachedClientConfiguration.cs

+35-14
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ namespace Enyim.Caching.Configuration
1717
/// </summary>
1818
public class MemcachedClientConfiguration : IMemcachedClientConfiguration
1919
{
20-
// these are lazy initialized in the getters
20+
private readonly bool _useLegacyNodeLocator;
21+
private readonly ILogger<MemcachedClientConfiguration> _logger;
22+
2123
private Type _nodeLocator;
2224
private ITranscoder _transcoder;
2325
private IMemcachedKeyTransformer _keyTransformer;
24-
private ILogger<MemcachedClientConfiguration> _logger;
26+
2527

2628
/// <summary>
2729
/// Initializes a new instance of the <see cref="T:MemcachedClientConfiguration"/> class.
@@ -153,18 +155,10 @@ public MemcachedClientConfiguration(
153155
_logger.LogDebug($"Use KeyTransformer Type : '{keyTransformer}'");
154156
}
155157

158+
_useLegacyNodeLocator = options.UseLegacyNodeLocator;
156159
if (NodeLocator == null)
157160
{
158-
if (options.Servers.Count > 1)
159-
{
160-
NodeLocator = options.UseLegacyNodeLocator ? typeof(LegacyNodeLocator) : typeof(DefaultNodeLocator);
161-
}
162-
else
163-
{
164-
NodeLocator = typeof(SingleNodeLocator);
165-
}
166-
167-
_logger.LogDebug($"Use NodeLocator: {NodeLocator}");
161+
SetNodeLocator();
168162
}
169163

170164
if (!string.IsNullOrEmpty(options.Transcoder))
@@ -217,7 +211,7 @@ private void ConfigureServers(MemcachedClientOptions options)
217211
{
218212
if (IPAddress.TryParse(server.Address, out var address))
219213
{
220-
Servers.Add(new IPEndPoint(address, server.Port));
214+
AddServer(address, server.Port);
221215
}
222216
else
223217
{
@@ -229,23 +223,50 @@ private void ConfigureServers(MemcachedClientOptions options)
229223
}
230224
}
231225

226+
private void SetNodeLocator()
227+
{
228+
if (Servers.Count > 1)
229+
{
230+
NodeLocator = _useLegacyNodeLocator ? typeof(LegacyNodeLocator) : typeof(DefaultNodeLocator);
231+
}
232+
else
233+
{
234+
NodeLocator = typeof(SingleNodeLocator);
235+
}
236+
237+
_logger.LogDebug("Use NodeLocator: {NodeLocator}", NodeLocator);
238+
}
239+
232240
/// <summary>
233241
/// Adds a new server to the pool.
234242
/// </summary>
235243
/// <param name="address">The address and the port of the server in the format 'host:port'.</param>
236244
public void AddServer(string address)
237245
{
238246
Servers.Add(ConfigurationHelper.ResolveToEndPoint(address));
247+
SetNodeLocator();
239248
}
240249

241250
/// <summary>
242251
/// Adds a new server to the pool.
243252
/// </summary>
244-
/// <param name="address">The host name or IP address of the server.</param>
253+
/// <param name="address">The host name.</param>
245254
/// <param name="port">The port number of the memcached instance.</param>
246255
public void AddServer(string host, int port)
247256
{
248257
Servers.Add(new DnsEndPoint(host, port));
258+
SetNodeLocator();
259+
}
260+
261+
/// <summary>
262+
/// Adds a new server to the pool.
263+
/// </summary>
264+
/// <param name="ip">The IP address of the server.</param>
265+
/// <param name="port">The port number of the memcached instance.</param>
266+
public void AddServer(IPAddress ip, int port)
267+
{
268+
Servers.Add(new IPEndPoint(ip, port));
269+
SetNodeLocator();
249270
}
250271

251272
/// <summary>

0 commit comments

Comments
 (0)