Skip to content

Commit 98769d7

Browse files
committed
Simplify the usages of Transcoder and KeyTransformer.
1 parent f6faa40 commit 98769d7

File tree

3 files changed

+154
-172
lines changed

3 files changed

+154
-172
lines changed

Enyim.Caching/Configuration/MemcachedClientConfiguration.cs

Lines changed: 138 additions & 161 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,181 +106,156 @@ public MemcachedClientConfiguration(
104106
}
105107
}
106108

107-
if(!string.IsNullOrEmpty(options.KeyTransformer))
109+
if (keyTransformer != null)
108110
{
109-
try
110-
{
111-
var keyTransformerType = Type.GetType(options.KeyTransformer);
112-
if (keyTransformerType != null)
113-
{
114-
KeyTransformer = Activator.CreateInstance(keyTransformerType) as IMemcachedKeyTransformer;
115-
_logger.LogDebug($"Use '{options.KeyTransformer}' KeyTransformer");
116-
}
117-
}
118-
catch(Exception ex)
119-
{
120-
_logger.LogError(new EventId(), ex, $"Unable to load '{options.KeyTransformer}' KeyTransformer");
121-
}
111+
this._keyTransformer = keyTransformer;
112+
_logger.LogDebug($"Use KeyTransformer Type : '{keyTransformer.ToString()}'");
122113
}
123114

124-
if(NodeLocator == null)
115+
if (NodeLocator == null)
125116
{
126117
NodeLocator = options.Servers.Count > 1 ? typeof(DefaultNodeLocator) : typeof(SingleNodeLocator);
127118
}
128119

129-
if(!string.IsNullOrEmpty(options.Transcoder))
120+
if (transcoder != null)
130121
{
131-
try
132-
{
133-
if (options.Transcoder == "BinaryFormatterTranscoder")
134-
options.Transcoder = "Enyim.Caching.Memcached.Transcoders.BinaryFormatterTranscoder";
135-
136-
var transcoderType = Type.GetType(options.Transcoder);
137-
if (transcoderType != null)
138-
{
139-
Transcoder = Activator.CreateInstance(transcoderType) as ITranscoder;
140-
_logger.LogDebug($"Use '{options.Transcoder}'");
141-
}
142-
}
143-
catch (Exception ex)
144-
{
145-
_logger.LogError(new EventId(), ex, $"Unable to load '{options.Transcoder}'");
146-
}
122+
this._transcoder = transcoder;
123+
_logger.LogDebug($"Use Transcoder Type : '{transcoder.ToString()}'");
147124
}
148125

149126
if (options.NodeLocatorFactory != null)
150127
{
151128
NodeLocatorFactory = options.NodeLocatorFactory;
152129
}
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-
}
130+
}
131+
132+
/// <summary>
133+
/// Adds a new server to the pool.
134+
/// </summary>
135+
/// <param name="address">The address and the port of the server in the format 'host:port'.</param>
136+
public void AddServer(string address)
137+
{
138+
this.Servers.Add(ConfigurationHelper.ResolveToEndPoint(address));
139+
}
140+
141+
/// <summary>
142+
/// Adds a new server to the pool.
143+
/// </summary>
144+
/// <param name="address">The host name or IP address of the server.</param>
145+
/// <param name="port">The port number of the memcached instance.</param>
146+
public void AddServer(string host, int port)
147+
{
148+
this.Servers.Add(ConfigurationHelper.ResolveToEndPoint(host, port));
149+
}
150+
151+
/// <summary>
152+
/// Gets a list of <see cref="T:IPEndPoint"/> each representing a Memcached server in the pool.
153+
/// </summary>
154+
public IList<EndPoint> Servers { get; private set; }
155+
156+
/// <summary>
157+
/// Gets the configuration of the socket pool.
158+
/// </summary>
159+
public ISocketPoolConfiguration SocketPool { get; private set; }
160+
161+
/// <summary>
162+
/// Gets the authentication settings.
163+
/// </summary>
164+
public IAuthenticationConfiguration Authentication { get; private set; }
165+
166+
/// <summary>
167+
/// Gets or sets the <see cref="T:Enyim.Caching.Memcached.IMemcachedKeyTransformer"/> which will be used to convert item keys for Memcached.
168+
/// </summary>
169+
public IMemcachedKeyTransformer KeyTransformer
170+
{
171+
get { return this._keyTransformer ?? (this._keyTransformer = new DefaultKeyTransformer()); }
172+
set { this._keyTransformer = value; }
173+
}
174+
175+
/// <summary>
176+
/// Gets or sets the Type of the <see cref="T:Enyim.Caching.Memcached.IMemcachedNodeLocator"/> which will be used to assign items to Memcached nodes.
177+
/// </summary>
178+
/// <remarks>If both <see cref="M:NodeLocator"/> and <see cref="M:NodeLocatorFactory"/> are assigned then the latter takes precedence.</remarks>
179+
public Type NodeLocator
180+
{
181+
get { return this.nodeLocator; }
182+
set
183+
{
184+
ConfigurationHelper.CheckForInterface(value, typeof(IMemcachedNodeLocator));
185+
this.nodeLocator = value;
186+
}
187+
}
188+
189+
/// <summary>
190+
/// Gets or sets the NodeLocatorFactory instance which will be used to create a new IMemcachedNodeLocator instances.
191+
/// </summary>
192+
/// <remarks>If both <see cref="M:NodeLocator"/> and <see cref="M:NodeLocatorFactory"/> are assigned then the latter takes precedence.</remarks>
193+
public IProviderFactory<IMemcachedNodeLocator> NodeLocatorFactory { get; set; }
194+
195+
/// <summary>
196+
/// Gets or sets the <see cref="T:Enyim.Caching.Memcached.ITranscoder"/> which will be used serialize or deserialize items.
197+
/// </summary>
198+
public ITranscoder Transcoder
199+
{
200+
get { return _transcoder ?? (_transcoder = new DefaultTranscoder()); }
201+
set { _transcoder = value; }
202+
}
203+
204+
/// <summary>
205+
/// Gets or sets the type of the communication between client and server.
206+
/// </summary>
207+
public MemcachedProtocol Protocol { get; set; }
208+
209+
#region [ interface ]
210+
211+
IList<System.Net.EndPoint> IMemcachedClientConfiguration.Servers
212+
{
213+
get { return this.Servers; }
214+
}
215+
216+
ISocketPoolConfiguration IMemcachedClientConfiguration.SocketPool
217+
{
218+
get { return this.SocketPool; }
219+
}
220+
221+
IAuthenticationConfiguration IMemcachedClientConfiguration.Authentication
222+
{
223+
get { return this.Authentication; }
224+
}
248225

249226
IMemcachedKeyTransformer IMemcachedClientConfiguration.CreateKeyTransformer()
250-
{
251-
return this.KeyTransformer;
252-
}
227+
{
228+
return this.KeyTransformer;
229+
}
253230

254-
IMemcachedNodeLocator IMemcachedClientConfiguration.CreateNodeLocator()
255-
{
256-
var f = this.NodeLocatorFactory;
257-
if (f != null) return f.Create();
231+
IMemcachedNodeLocator IMemcachedClientConfiguration.CreateNodeLocator()
232+
{
233+
var f = this.NodeLocatorFactory;
234+
if (f != null) return f.Create();
258235

259-
return this.NodeLocator == null
260-
? new SingleNodeLocator()
236+
return this.NodeLocator == null
237+
? new SingleNodeLocator()
261238
: (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-
}
239+
}
240+
241+
ITranscoder IMemcachedClientConfiguration.CreateTranscoder()
242+
{
243+
return this.Transcoder;
244+
}
245+
246+
IServerPool IMemcachedClientConfiguration.CreatePool()
247+
{
248+
switch (this.Protocol)
249+
{
250+
case MemcachedProtocol.Text: return new DefaultServerPool(this, new Memcached.Protocol.Text.TextOperationFactory(), _logger);
251+
case MemcachedProtocol.Binary: return new BinaryPool(this, _logger);
252+
}
253+
254+
throw new ArgumentOutOfRangeException("Unknown protocol: " + (int)this.Protocol);
255+
}
256+
257+
#endregion
258+
}
282259
}
283260

284261
#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)