Skip to content

Commit e670108

Browse files
authored
Merge pull request #260 from cnblogs/refactor-generic-memcached-client
refactor: introduce MemcachedClientOptions<T>
2 parents ee4507f + 16ad153 commit e670108

File tree

6 files changed

+39
-36
lines changed

6 files changed

+39
-36
lines changed

sample/SampleWebApp/appsettings.example.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
}
88
],
99
"SocketPool": {
10-
"minPoolSize": 5,
11-
"maxPoolSize": 25,
12-
"connectionTimeout": "00:00:15",
13-
"receiveTimeout": "00:00:15",
14-
"deadTimeout": "00:00:15",
15-
"queueTimeout": "00:00:00.150"
10+
"MinPoolSize": 5,
11+
"MaxPoolSize": 25,
12+
"ConnectionTimeout": "00:00:15",
13+
"ReceiveTimeout": "00:00:15",
14+
"DeadTimeout": "00:00:15",
15+
"QueueTimeout": "00:00:00.150"
1616
},
1717
"SuppressException": false,
1818
"UseSslStream": false,
@@ -23,14 +23,14 @@
2323
"Authentication": {
2424
"Type": "Enyim.Caching.Memcached.PlainTextAuthenticator",
2525
"Parameters": {
26-
"zone": "",
27-
"userName": "username",
28-
"password": "password"
26+
"Zone": "",
27+
"UserName": "username",
28+
"Password": "password"
2929
}
3030
}
3131
},
3232

33-
"postbodyMemcached": {
33+
"PostbodyMemcached": {
3434
"Servers": [
3535
{
3636
"Address": "memcached",

sample/SampleWebApp/appsettings.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
"Port": 11211
1111
}
1212
],
13-
"socketPool": {
14-
"minPoolSize": 5,
15-
"maxPoolSize": 10,
16-
"connectionTimeout": "00:00:15",
17-
"receiveTimeout": "00:00:15",
18-
"deadTimeout": "00:00:15",
19-
"queueTimeout": "00:00:00.150"
13+
"SocketPool": {
14+
"MinPoolSize": 5,
15+
"MaxPoolSize": 10,
16+
"ConnectionTimeout": "00:00:15",
17+
"ReceiveTimeout": "00:00:15",
18+
"DeadTimeout": "00:00:15",
19+
"QueueTimeout": "00:00:00.150"
2020
},
21-
"suppressException": false,
21+
"SuppressException": false,
2222
"UseLegacyNodeLocator": false,
2323
"Transcoder": "MessagePackTranscoder"
2424
},
2525

26-
"postbodyMemcached": {
26+
"PostbodyMemcached": {
2727
"Servers": [
2828
{
2929
"Address": "memcached",

src/Enyim.Caching/Configuration/MemcachedClientConfiguration.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,24 @@ public MemcachedClientConfiguration(
108108
var authenticationType = Type.GetType(options.Authentication.Type);
109109
if (authenticationType != null)
110110
{
111-
_logger.LogDebug($"Authentication type is {authenticationType}.");
111+
_logger.LogDebug("Authentication type is {authenticationType}.", authenticationType);
112112

113113
Authentication = new AuthenticationConfiguration();
114114
Authentication.Type = authenticationType;
115115
foreach (var parameter in options.Authentication.Parameters)
116116
{
117117
Authentication.Parameters[parameter.Key] = parameter.Value;
118-
_logger.LogDebug($"Authentication {parameter.Key} is '{parameter.Value}'.");
118+
_logger.LogDebug("Authentication {Key} is '{Value}'.", parameter.Key, parameter.Value);
119119
}
120120
}
121121
else
122122
{
123-
_logger.LogError($"Unable to load authentication type {options.Authentication.Type}.");
123+
_logger.LogError("Unable to load authentication type '{AuthenticationType}'.", options.Authentication.Type);
124124
}
125125
}
126126
catch (Exception ex)
127127
{
128-
_logger.LogError(new EventId(), ex,
129-
$"Unable to load authentication type {options.Authentication.Type}.");
128+
_logger.LogError(ex, "Unable to load authentication type '{AuthenticationType}'.", options.Authentication.Type);
130129
}
131130
}
132131

@@ -145,18 +144,18 @@ public MemcachedClientConfiguration(
145144
if (keyTransformerType != null)
146145
{
147146
KeyTransformer = Activator.CreateInstance(keyTransformerType) as IMemcachedKeyTransformer;
148-
_logger.LogDebug($"Use '{options.KeyTransformer}' KeyTransformer");
147+
_logger.LogDebug("Use '{KeyTransformer}' KeyTransformer", options.KeyTransformer);
149148
}
150149
}
151150
catch (Exception ex)
152151
{
153-
_logger.LogError(new EventId(), ex, $"Unable to load '{options.KeyTransformer}' KeyTransformer");
152+
_logger.LogError(ex, "Unable to load '{KeyTransformer}' KeyTransformer", options.KeyTransformer);
154153
}
155154
}
156155
else if (keyTransformer != null)
157156
{
158157
_keyTransformer = keyTransformer;
159-
_logger.LogDebug($"Use KeyTransformer Type : '{keyTransformer}'");
158+
_logger.LogDebug("Use KeyTransformer Type : '{keyTransformer}'", keyTransformer);
160159
}
161160

162161
if (!string.IsNullOrEmpty(options.Transcoder))
@@ -176,18 +175,18 @@ public MemcachedClientConfiguration(
176175
if (transcoderType != null)
177176
{
178177
Transcoder = Activator.CreateInstance(transcoderType) as ITranscoder;
179-
_logger.LogDebug($"Use '{options.Transcoder}'");
178+
_logger.LogDebug("Use '{Transcoder}'", options.Transcoder);
180179
}
181180
}
182181
catch (Exception ex)
183182
{
184-
_logger.LogError(new EventId(), ex, $"Unable to load '{options.Transcoder}'");
183+
_logger.LogError(new EventId(), ex, "Unable to load '{Transcoder}'", Transcoder);
185184
}
186185
}
187186
else if (transcoder != null)
188187
{
189188
_transcoder = transcoder;
190-
_logger.LogDebug($"Use Transcoder Type : '{transcoder}'");
189+
_logger.LogDebug("Use Transcoder Type : '{transcoder}'", transcoder);
191190
}
192191

193192
if (options.NodeLocatorFactory != null)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace Enyim.Caching.Configuration;
2+
3+
public class MemcachedClientOptions<T> : MemcachedClientOptions
4+
{ }

src/Enyim.Caching/EnyimMemcachedServiceCollectionExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public static IServiceCollection AddEnyimMemcached<T>(
135135
}
136136

137137
return services.AddEnyimMemcached<T>(
138-
s => s.AddOptions<MemcachedClientOptions>().BindConfiguration(sectionKey));
138+
s => s.AddOptions<MemcachedClientOptions<T>>().BindConfiguration(sectionKey));
139139
}
140140

141141
public static IServiceCollection AddEnyimMemcached<T>(
@@ -154,7 +154,7 @@ public static IServiceCollection AddEnyimMemcached<T>(
154154
}
155155

156156
return services.AddEnyimMemcached<T>(
157-
s => s.Configure<MemcachedClientOptions>(configuration.GetSection(sectionKey)));
157+
s => s.Configure<MemcachedClientOptions<T>>(configuration.GetSection(sectionKey)));
158158
}
159159
#endif
160160

@@ -171,7 +171,7 @@ public static IServiceCollection AddEnyimMemcached<T>(
171171
services.TryAddSingleton<IMemcachedClient<T>>(sp =>
172172
{
173173
var loggerFactory = sp.GetRequiredService<ILoggerFactory>();
174-
var options = sp.GetRequiredService<IOptions<MemcachedClientOptions>>();
174+
var options = sp.GetRequiredService<IOptions<MemcachedClientOptions<T>>>();
175175
var conf = new MemcachedClientConfiguration(loggerFactory, options);
176176
return new MemcachedClient<T>(loggerFactory, conf);
177177
});

src/Enyim.Caching/IMemcachedClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using System;
2-
using Enyim.Caching.Memcached;
1+
using Enyim.Caching.Memcached;
2+
using Enyim.Caching.Memcached.Results;
3+
using System;
34
using System.Collections.Generic;
45
using System.Threading.Tasks;
5-
using Enyim.Caching.Memcached.Results;
66

77
namespace Enyim.Caching
88
{

0 commit comments

Comments
 (0)