Skip to content

Commit 3c45880

Browse files
authored
Merge pull request #57 from cnblogs/improve-socket
Improve connecting socket
2 parents c56e9d2 + 849602c commit 3c45880

14 files changed

+633
-247
lines changed

Enyim.Caching/Configuration/ConfigurationHelper.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static void CheckForInterface(Type type, Type interfaceType)
7373
// throw new System.Configuration.ConfigurationErrorsException("The type " + type.AssemblyQualifiedName + " must implement " + interfaceType.AssemblyQualifiedName);
7474
}
7575

76-
public static EndPoint ResolveToEndPoint(string value)
76+
public static DnsEndPoint ResolveToEndPoint(string value)
7777
{
7878
if (String.IsNullOrEmpty(value))
7979
throw new ArgumentNullException("value");
@@ -86,25 +86,8 @@ public static EndPoint ResolveToEndPoint(string value)
8686
if (!Int32.TryParse(parts[1], out port))
8787
throw new ArgumentException("Cannot parse port: " + parts[1], "value");
8888

89-
return ResolveToEndPoint(parts[0], port);
90-
}
91-
92-
public static EndPoint ResolveToEndPoint(string host, int port)
93-
{
94-
if (String.IsNullOrEmpty(host))
95-
throw new ArgumentNullException("host");
96-
97-
IPAddress address;
98-
// parse as an IP address
99-
if (!IPAddress.TryParse(host, out address))
100-
{
101-
var addresses = Dns.GetHostAddresses(host);
102-
address = addresses.FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
103-
if (address == null)
104-
throw new ArgumentException(String.Format("Could not resolve host '{0}'.", host));
105-
}
106-
return new IPEndPoint(address, port);
107-
}
89+
return new DnsEndPoint(parts[0], port);
90+
}
10891
}
10992
}
11093

Enyim.Caching/Configuration/IMemcachedClientConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface IMemcachedClientConfiguration
1313
/// <summary>
1414
/// Gets a list of <see cref="T:IPEndPoint"/> each representing a Memcached server in the pool.
1515
/// </summary>
16-
IList<EndPoint> Servers { get; }
16+
IList<DnsEndPoint> Servers { get; }
1717

1818
/// <summary>
1919
/// Gets the configuration of the socket pool.

Enyim.Caching/Configuration/MemcachedClientConfiguration.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,22 @@ public MemcachedClientConfiguration(
4141
var options = optionsAccessor.Value;
4242
if ((options == null || options.Servers.Count == 0) && configuration != null)
4343
{
44-
configuration.GetSection("enyimMemcached").Bind(options);
45-
}
46-
47-
Servers = new List<EndPoint>();
48-
foreach (var server in options.Servers)
49-
{
50-
IPAddress address;
51-
if (IPAddress.TryParse(server.Address, out address))
44+
var section = configuration.GetSection("enyimMemcached");
45+
if (section.Exists())
5246
{
53-
Servers.Add(new IPEndPoint(address, server.Port));
47+
section.Bind(options);
5448
}
5549
else
5650
{
57-
Servers.Add(new DnsEndPoint(server.Address, server.Port));
58-
}
51+
_logger.LogWarning($"No enyimMemcached setting in appsetting.json. Use default configuration");
52+
options.AddDefaultServer();
53+
}
54+
}
55+
56+
Servers = new List<DnsEndPoint>();
57+
foreach (var server in options.Servers)
58+
{
59+
Servers.Add(new DnsEndPoint(server.Address, server.Port));
5960
}
6061

6162
SocketPool = new SocketPoolConfiguration();
@@ -187,13 +188,13 @@ public void AddServer(string address)
187188
/// <param name="port">The port number of the memcached instance.</param>
188189
public void AddServer(string host, int port)
189190
{
190-
this.Servers.Add(ConfigurationHelper.ResolveToEndPoint(host, port));
191+
this.Servers.Add(new DnsEndPoint(host, port));
191192
}
192193

193194
/// <summary>
194195
/// Gets a list of <see cref="T:IPEndPoint"/> each representing a Memcached server in the pool.
195196
/// </summary>
196-
public IList<EndPoint> Servers { get; private set; }
197+
public IList<DnsEndPoint> Servers { get; private set; }
197198

198199
/// <summary>
199200
/// Gets the configuration of the socket pool.
@@ -250,7 +251,7 @@ public ITranscoder Transcoder
250251

251252
#region [ interface ]
252253

253-
IList<System.Net.EndPoint> IMemcachedClientConfiguration.Servers
254+
IList<System.Net.DnsEndPoint> IMemcachedClientConfiguration.Servers
254255
{
255256
get { return this.Servers; }
256257
}

Enyim.Caching/Configuration/MemcachedClientOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public void AddServer(string address, int port)
3030
Servers.Add(new Server { Address = address, Port = port });
3131
}
3232

33+
public void AddDefaultServer() => AddServer("memcached", 11211);
34+
3335
public void AddPlainTextAuthenticator(string zone, string userName, string password)
3436
{
3537
Authentication = new Authentication

Enyim.Caching/Enyim.Caching.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>EnyimMemcachedCore is a Memcached client library for .NET Core. Usage: Add services.AddEnyimMemcached(...) and app.UseEnyimMemcached() in Startup. Add IMemcachedClient into constructor.</Description>
5-
<VersionPrefix>2.1.6</VersionPrefix>
5+
<VersionPrefix>2.1.7</VersionPrefix>
66
<Authors>cnblogs.com</Authors>
77
<TargetFramework>netstandard2.0</TargetFramework>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

Enyim.Caching/EnyimMemcachedServiceCollectionExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public static IServiceCollection AddEnyimMemcached(this IServiceCollection servi
4949
throw new ArgumentNullException(nameof(configurationSection));
5050
}
5151

52+
if(!configurationSection.Exists())
53+
{
54+
throw new ArgumentNullException($"{configurationSection.Key} in appsettings.json");
55+
}
56+
5257
return AddEnyimMemcachedInternal(services, s => s.Configure<MemcachedClientOptions>(configurationSection));
5358
}
5459

@@ -64,7 +69,13 @@ public static IServiceCollection AddEnyimMemcached(this IServiceCollection servi
6469
throw new ArgumentNullException(nameof(configuration));
6570
}
6671

67-
return AddEnyimMemcachedInternal(services, s => s.Configure<MemcachedClientOptions>(configuration.GetSection(sectionKey)));
72+
var section = configuration.GetSection(sectionKey);
73+
if (!section.Exists())
74+
{
75+
throw new ArgumentNullException($"{sectionKey} in appsettings.json");
76+
}
77+
78+
return AddEnyimMemcachedInternal(services, s => s.Configure<MemcachedClientOptions>(section));
6879
}
6980

7081
private static IServiceCollection AddEnyimMemcachedInternal(IServiceCollection services, Action<IServiceCollection> configure)
@@ -81,6 +92,6 @@ private static IServiceCollection AddEnyimMemcachedInternal(IServiceCollection s
8192
services.AddSingleton<IDistributedCache>(factory => factory.GetService<MemcachedClient>());
8293

8394
return services;
84-
}
95+
}
8596
}
8697
}

0 commit comments

Comments
 (0)