Skip to content

Commit 39665ae

Browse files
authored
Merge pull request #47 from catcherwong/dotnetcore
Simplify the usages of Transcoder and KeyTransformer.
2 parents f6faa40 + 928a4ff commit 39665ae

File tree

3 files changed

+162
-145
lines changed

3 files changed

+162
-145
lines changed

Enyim.Caching/Configuration/MemcachedClientConfiguration.cs

Lines changed: 146 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ public class MemcachedClientConfiguration : IMemcachedClientConfiguration
1717
// these are lazy initialized in the getters
1818
private Type nodeLocator;
1919
private ITranscoder _transcoder;
20-
private IMemcachedKeyTransformer keyTransformer;
20+
private IMemcachedKeyTransformer _keyTransformer;
2121
private ILogger<MemcachedClientConfiguration> _logger;
2222

2323
/// <summary>
2424
/// Initializes a new instance of the <see cref="T:MemcachedClientConfiguration"/> class.
2525
/// </summary>
2626
public MemcachedClientConfiguration(
2727
ILoggerFactory loggerFactory,
28-
IOptions<MemcachedClientOptions> optionsAccessor)
28+
IOptions<MemcachedClientOptions> optionsAccessor,
29+
ITranscoder transcoder = null,
30+
IMemcachedKeyTransformer keyTransformer = null)
2931
{
3032
if (optionsAccessor == null)
3133
{
@@ -46,7 +48,7 @@ public MemcachedClientConfiguration(
4648
else
4749
{
4850
Servers.Add(new DnsEndPoint(server.Address, server.Port));
49-
}
51+
}
5052
}
5153

5254
SocketPool = new SocketPoolConfiguration();
@@ -61,8 +63,8 @@ public MemcachedClientConfiguration(
6163
SocketPool.MaxPoolSize = options.SocketPool.MaxPoolSize;
6264
_logger.LogInformation($"{nameof(SocketPool.MaxPoolSize)}: {SocketPool.MaxPoolSize}");
6365

64-
SocketPool.ConnectionTimeout = options.SocketPool.ConnectionTimeout;
65-
_logger.LogInformation($"{nameof(SocketPool.ConnectionTimeout)}: {SocketPool.ConnectionTimeout}");
66+
SocketPool.ConnectionTimeout = options.SocketPool.ConnectionTimeout;
67+
_logger.LogInformation($"{nameof(SocketPool.ConnectionTimeout)}: {SocketPool.ConnectionTimeout}");
6668

6769
SocketPool.ReceiveTimeout = options.SocketPool.ReceiveTimeout;
6870
_logger.LogInformation($"{nameof(SocketPool.ReceiveTimeout)}: {SocketPool.ReceiveTimeout}");
@@ -104,7 +106,7 @@ public MemcachedClientConfiguration(
104106
}
105107
}
106108

107-
if(!string.IsNullOrEmpty(options.KeyTransformer))
109+
if (!string.IsNullOrEmpty(options.KeyTransformer))
108110
{
109111
try
110112
{
@@ -115,18 +117,23 @@ public MemcachedClientConfiguration(
115117
_logger.LogDebug($"Use '{options.KeyTransformer}' KeyTransformer");
116118
}
117119
}
118-
catch(Exception ex)
120+
catch (Exception ex)
119121
{
120122
_logger.LogError(new EventId(), ex, $"Unable to load '{options.KeyTransformer}' KeyTransformer");
121-
}
123+
}
124+
}
125+
else if (keyTransformer != null)
126+
{
127+
this._keyTransformer = keyTransformer;
128+
_logger.LogDebug($"Use KeyTransformer Type : '{keyTransformer.ToString()}'");
122129
}
123130

124-
if(NodeLocator == null)
131+
if (NodeLocator == null)
125132
{
126133
NodeLocator = options.Servers.Count > 1 ? typeof(DefaultNodeLocator) : typeof(SingleNodeLocator);
127134
}
128135

129-
if(!string.IsNullOrEmpty(options.Transcoder))
136+
if (!string.IsNullOrEmpty(options.Transcoder))
130137
{
131138
try
132139
{
@@ -145,140 +152,145 @@ public MemcachedClientConfiguration(
145152
_logger.LogError(new EventId(), ex, $"Unable to load '{options.Transcoder}'");
146153
}
147154
}
155+
else if (transcoder != null)
156+
{
157+
this._transcoder = transcoder;
158+
_logger.LogDebug($"Use Transcoder Type : '{transcoder.ToString()}'");
159+
}
148160

149161
if (options.NodeLocatorFactory != null)
150162
{
151163
NodeLocatorFactory = options.NodeLocatorFactory;
152164
}
153-
}
154-
155-
/// <summary>
156-
/// Adds a new server to the pool.
157-
/// </summary>
158-
/// <param name="address">The address and the port of the server in the format 'host:port'.</param>
159-
public void AddServer(string address)
160-
{
161-
this.Servers.Add(ConfigurationHelper.ResolveToEndPoint(address));
162-
}
163-
164-
/// <summary>
165-
/// Adds a new server to the pool.
166-
/// </summary>
167-
/// <param name="address">The host name or IP address of the server.</param>
168-
/// <param name="port">The port number of the memcached instance.</param>
169-
public void AddServer(string host, int port)
170-
{
171-
this.Servers.Add(ConfigurationHelper.ResolveToEndPoint(host, port));
172-
}
173-
174-
/// <summary>
175-
/// Gets a list of <see cref="T:IPEndPoint"/> each representing a Memcached server in the pool.
176-
/// </summary>
177-
public IList<EndPoint> Servers { get; private set; }
178-
179-
/// <summary>
180-
/// Gets the configuration of the socket pool.
181-
/// </summary>
182-
public ISocketPoolConfiguration SocketPool { get; private set; }
183-
184-
/// <summary>
185-
/// Gets the authentication settings.
186-
/// </summary>
187-
public IAuthenticationConfiguration Authentication { get; private set; }
188-
189-
/// <summary>
190-
/// Gets or sets the <see cref="T:Enyim.Caching.Memcached.IMemcachedKeyTransformer"/> which will be used to convert item keys for Memcached.
191-
/// </summary>
192-
public IMemcachedKeyTransformer KeyTransformer
193-
{
194-
get { return this.keyTransformer ?? (this.keyTransformer = new DefaultKeyTransformer()); }
195-
set { this.keyTransformer = value; }
196-
}
197-
198-
/// <summary>
199-
/// Gets or sets the Type of the <see cref="T:Enyim.Caching.Memcached.IMemcachedNodeLocator"/> which will be used to assign items to Memcached nodes.
200-
/// </summary>
201-
/// <remarks>If both <see cref="M:NodeLocator"/> and <see cref="M:NodeLocatorFactory"/> are assigned then the latter takes precedence.</remarks>
202-
public Type NodeLocator
203-
{
204-
get { return this.nodeLocator; }
205-
set
206-
{
207-
ConfigurationHelper.CheckForInterface(value, typeof(IMemcachedNodeLocator));
208-
this.nodeLocator = value;
209-
}
210-
}
211-
212-
/// <summary>
213-
/// Gets or sets the NodeLocatorFactory instance which will be used to create a new IMemcachedNodeLocator instances.
214-
/// </summary>
215-
/// <remarks>If both <see cref="M:NodeLocator"/> and <see cref="M:NodeLocatorFactory"/> are assigned then the latter takes precedence.</remarks>
216-
public IProviderFactory<IMemcachedNodeLocator> NodeLocatorFactory { get; set; }
217-
218-
/// <summary>
219-
/// Gets or sets the <see cref="T:Enyim.Caching.Memcached.ITranscoder"/> which will be used serialize or deserialize items.
220-
/// </summary>
221-
public ITranscoder Transcoder
222-
{
223-
get { return _transcoder ?? (_transcoder = new DefaultTranscoder()); }
224-
set { _transcoder = value; }
225-
}
226-
227-
/// <summary>
228-
/// Gets or sets the type of the communication between client and server.
229-
/// </summary>
230-
public MemcachedProtocol Protocol { get; set; }
231-
232-
#region [ interface ]
233-
234-
IList<System.Net.EndPoint> IMemcachedClientConfiguration.Servers
235-
{
236-
get { return this.Servers; }
237-
}
238-
239-
ISocketPoolConfiguration IMemcachedClientConfiguration.SocketPool
240-
{
241-
get { return this.SocketPool; }
242-
}
243-
244-
IAuthenticationConfiguration IMemcachedClientConfiguration.Authentication
245-
{
246-
get { return this.Authentication; }
247-
}
165+
}
166+
167+
/// <summary>
168+
/// Adds a new server to the pool.
169+
/// </summary>
170+
/// <param name="address">The address and the port of the server in the format 'host:port'.</param>
171+
public void AddServer(string address)
172+
{
173+
this.Servers.Add(ConfigurationHelper.ResolveToEndPoint(address));
174+
}
175+
176+
/// <summary>
177+
/// Adds a new server to the pool.
178+
/// </summary>
179+
/// <param name="address">The host name or IP address of the server.</param>
180+
/// <param name="port">The port number of the memcached instance.</param>
181+
public void AddServer(string host, int port)
182+
{
183+
this.Servers.Add(ConfigurationHelper.ResolveToEndPoint(host, port));
184+
}
185+
186+
/// <summary>
187+
/// Gets a list of <see cref="T:IPEndPoint"/> each representing a Memcached server in the pool.
188+
/// </summary>
189+
public IList<EndPoint> Servers { get; private set; }
190+
191+
/// <summary>
192+
/// Gets the configuration of the socket pool.
193+
/// </summary>
194+
public ISocketPoolConfiguration SocketPool { get; private set; }
195+
196+
/// <summary>
197+
/// Gets the authentication settings.
198+
/// </summary>
199+
public IAuthenticationConfiguration Authentication { get; private set; }
200+
201+
/// <summary>
202+
/// Gets or sets the <see cref="T:Enyim.Caching.Memcached.IMemcachedKeyTransformer"/> which will be used to convert item keys for Memcached.
203+
/// </summary>
204+
public IMemcachedKeyTransformer KeyTransformer
205+
{
206+
get { return this._keyTransformer ?? (this._keyTransformer = new DefaultKeyTransformer()); }
207+
set { this._keyTransformer = value; }
208+
}
209+
210+
/// <summary>
211+
/// Gets or sets the Type of the <see cref="T:Enyim.Caching.Memcached.IMemcachedNodeLocator"/> which will be used to assign items to Memcached nodes.
212+
/// </summary>
213+
/// <remarks>If both <see cref="M:NodeLocator"/> and <see cref="M:NodeLocatorFactory"/> are assigned then the latter takes precedence.</remarks>
214+
public Type NodeLocator
215+
{
216+
get { return this.nodeLocator; }
217+
set
218+
{
219+
ConfigurationHelper.CheckForInterface(value, typeof(IMemcachedNodeLocator));
220+
this.nodeLocator = value;
221+
}
222+
}
223+
224+
/// <summary>
225+
/// Gets or sets the NodeLocatorFactory instance which will be used to create a new IMemcachedNodeLocator instances.
226+
/// </summary>
227+
/// <remarks>If both <see cref="M:NodeLocator"/> and <see cref="M:NodeLocatorFactory"/> are assigned then the latter takes precedence.</remarks>
228+
public IProviderFactory<IMemcachedNodeLocator> NodeLocatorFactory { get; set; }
229+
230+
/// <summary>
231+
/// Gets or sets the <see cref="T:Enyim.Caching.Memcached.ITranscoder"/> which will be used serialize or deserialize items.
232+
/// </summary>
233+
public ITranscoder Transcoder
234+
{
235+
get { return _transcoder ?? (_transcoder = new DefaultTranscoder()); }
236+
set { _transcoder = value; }
237+
}
238+
239+
/// <summary>
240+
/// Gets or sets the type of the communication between client and server.
241+
/// </summary>
242+
public MemcachedProtocol Protocol { get; set; }
243+
244+
#region [ interface ]
245+
246+
IList<System.Net.EndPoint> IMemcachedClientConfiguration.Servers
247+
{
248+
get { return this.Servers; }
249+
}
250+
251+
ISocketPoolConfiguration IMemcachedClientConfiguration.SocketPool
252+
{
253+
get { return this.SocketPool; }
254+
}
255+
256+
IAuthenticationConfiguration IMemcachedClientConfiguration.Authentication
257+
{
258+
get { return this.Authentication; }
259+
}
248260

249261
IMemcachedKeyTransformer IMemcachedClientConfiguration.CreateKeyTransformer()
250-
{
251-
return this.KeyTransformer;
252-
}
262+
{
263+
return this.KeyTransformer;
264+
}
253265

254-
IMemcachedNodeLocator IMemcachedClientConfiguration.CreateNodeLocator()
255-
{
256-
var f = this.NodeLocatorFactory;
257-
if (f != null) return f.Create();
266+
IMemcachedNodeLocator IMemcachedClientConfiguration.CreateNodeLocator()
267+
{
268+
var f = this.NodeLocatorFactory;
269+
if (f != null) return f.Create();
258270

259-
return this.NodeLocator == null
260-
? new SingleNodeLocator()
271+
return this.NodeLocator == null
272+
? new SingleNodeLocator()
261273
: (IMemcachedNodeLocator)FastActivator.Create(this.NodeLocator);
262-
}
263-
264-
ITranscoder IMemcachedClientConfiguration.CreateTranscoder()
265-
{
266-
return this.Transcoder;
267-
}
268-
269-
IServerPool IMemcachedClientConfiguration.CreatePool()
270-
{
271-
switch (this.Protocol)
272-
{
273-
case MemcachedProtocol.Text: return new DefaultServerPool(this, new Memcached.Protocol.Text.TextOperationFactory(), _logger);
274-
case MemcachedProtocol.Binary: return new BinaryPool(this, _logger);
275-
}
276-
277-
throw new ArgumentOutOfRangeException("Unknown protocol: " + (int)this.Protocol);
278-
}
279-
280-
#endregion
281-
}
274+
}
275+
276+
ITranscoder IMemcachedClientConfiguration.CreateTranscoder()
277+
{
278+
return this.Transcoder;
279+
}
280+
281+
IServerPool IMemcachedClientConfiguration.CreatePool()
282+
{
283+
switch (this.Protocol)
284+
{
285+
case MemcachedProtocol.Text: return new DefaultServerPool(this, new Memcached.Protocol.Text.TextOperationFactory(), _logger);
286+
case MemcachedProtocol.Binary: return new BinaryPool(this, _logger);
287+
}
288+
289+
throw new ArgumentOutOfRangeException("Unknown protocol: " + (int)this.Protocol);
290+
}
291+
292+
#endregion
293+
}
282294
}
283295

284296
#region [ License information ]

Enyim.Caching/EnyimMemcachedServiceCollectionExtensions.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using Enyim.Caching;
22
using Enyim.Caching.Configuration;
3+
using Enyim.Caching.Memcached;
34
using Microsoft.Extensions.Caching.Distributed;
45
using Microsoft.Extensions.Configuration;
56
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.DependencyInjection.Extensions;
68
using System;
7-
using System.Collections.Generic;
8-
using System.Linq;
9-
using System.Threading.Tasks;
109

1110
namespace Microsoft.Extensions.DependencyInjection
1211
{
@@ -46,7 +45,10 @@ private static IServiceCollection AddEnyimMemcached(IServiceCollection services,
4645
{
4746
services.AddOptions();
4847
configure(services);
49-
services.AddTransient<IMemcachedClientConfiguration, MemcachedClientConfiguration>();
48+
49+
services.TryAddSingleton<ITranscoder, DefaultTranscoder>();
50+
services.TryAddSingleton<IMemcachedKeyTransformer, DefaultKeyTransformer>();
51+
services.TryAddTransient<IMemcachedClientConfiguration, MemcachedClientConfiguration>();
5052
services.AddSingleton<MemcachedClient, MemcachedClient>();
5153

5254
services.AddSingleton<IMemcachedClient>(factory => factory.GetService<MemcachedClient>());

0 commit comments

Comments
 (0)