Skip to content

refactor: introduce MemcachedClientOptions<T> #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions sample/SampleWebApp/appsettings.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
}
],
"SocketPool": {
"minPoolSize": 5,
"maxPoolSize": 25,
"connectionTimeout": "00:00:15",
"receiveTimeout": "00:00:15",
"deadTimeout": "00:00:15",
"queueTimeout": "00:00:00.150"
"MinPoolSize": 5,
"MaxPoolSize": 25,
"ConnectionTimeout": "00:00:15",
"ReceiveTimeout": "00:00:15",
"DeadTimeout": "00:00:15",
"QueueTimeout": "00:00:00.150"
},
"SuppressException": false,
"UseSslStream": false,
Expand All @@ -23,14 +23,14 @@
"Authentication": {
"Type": "Enyim.Caching.Memcached.PlainTextAuthenticator",
"Parameters": {
"zone": "",
"userName": "username",
"password": "password"
"Zone": "",
"UserName": "username",
"Password": "password"
}
}
},

"postbodyMemcached": {
"PostbodyMemcached": {
"Servers": [
{
"Address": "memcached",
Expand Down
18 changes: 9 additions & 9 deletions sample/SampleWebApp/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
"Port": 11211
}
],
"socketPool": {
"minPoolSize": 5,
"maxPoolSize": 10,
"connectionTimeout": "00:00:15",
"receiveTimeout": "00:00:15",
"deadTimeout": "00:00:15",
"queueTimeout": "00:00:00.150"
"SocketPool": {
"MinPoolSize": 5,
"MaxPoolSize": 10,
"ConnectionTimeout": "00:00:15",
"ReceiveTimeout": "00:00:15",
"DeadTimeout": "00:00:15",
"QueueTimeout": "00:00:00.150"
},
"suppressException": false,
"SuppressException": false,
"UseLegacyNodeLocator": false,
"Transcoder": "MessagePackTranscoder"
},

"postbodyMemcached": {
"PostbodyMemcached": {
"Servers": [
{
"Address": "memcached",
Expand Down
21 changes: 10 additions & 11 deletions src/Enyim.Caching/Configuration/MemcachedClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,24 @@ public MemcachedClientConfiguration(
var authenticationType = Type.GetType(options.Authentication.Type);
if (authenticationType != null)
{
_logger.LogDebug($"Authentication type is {authenticationType}.");
_logger.LogDebug("Authentication type is {authenticationType}.", authenticationType);

Authentication = new AuthenticationConfiguration();
Authentication.Type = authenticationType;
foreach (var parameter in options.Authentication.Parameters)
{
Authentication.Parameters[parameter.Key] = parameter.Value;
_logger.LogDebug($"Authentication {parameter.Key} is '{parameter.Value}'.");
_logger.LogDebug("Authentication {Key} is '{Value}'.", parameter.Key, parameter.Value);
}
}
else
{
_logger.LogError($"Unable to load authentication type {options.Authentication.Type}.");
_logger.LogError("Unable to load authentication type '{AuthenticationType}'.", options.Authentication.Type);
}
}
catch (Exception ex)
{
_logger.LogError(new EventId(), ex,
$"Unable to load authentication type {options.Authentication.Type}.");
_logger.LogError(ex, "Unable to load authentication type '{AuthenticationType}'.", options.Authentication.Type);
}
}

Expand All @@ -145,18 +144,18 @@ public MemcachedClientConfiguration(
if (keyTransformerType != null)
{
KeyTransformer = Activator.CreateInstance(keyTransformerType) as IMemcachedKeyTransformer;
_logger.LogDebug($"Use '{options.KeyTransformer}' KeyTransformer");
_logger.LogDebug("Use '{KeyTransformer}' KeyTransformer", options.KeyTransformer);
}
}
catch (Exception ex)
{
_logger.LogError(new EventId(), ex, $"Unable to load '{options.KeyTransformer}' KeyTransformer");
_logger.LogError(ex, "Unable to load '{KeyTransformer}' KeyTransformer", options.KeyTransformer);
}
}
else if (keyTransformer != null)
{
_keyTransformer = keyTransformer;
_logger.LogDebug($"Use KeyTransformer Type : '{keyTransformer}'");
_logger.LogDebug("Use KeyTransformer Type : '{keyTransformer}'", keyTransformer);
}

if (!string.IsNullOrEmpty(options.Transcoder))
Expand All @@ -176,18 +175,18 @@ public MemcachedClientConfiguration(
if (transcoderType != null)
{
Transcoder = Activator.CreateInstance(transcoderType) as ITranscoder;
_logger.LogDebug($"Use '{options.Transcoder}'");
_logger.LogDebug("Use '{Transcoder}'", options.Transcoder);
}
}
catch (Exception ex)
{
_logger.LogError(new EventId(), ex, $"Unable to load '{options.Transcoder}'");
_logger.LogError(new EventId(), ex, "Unable to load '{Transcoder}'", Transcoder);
}
}
else if (transcoder != null)
{
_transcoder = transcoder;
_logger.LogDebug($"Use Transcoder Type : '{transcoder}'");
_logger.LogDebug("Use Transcoder Type : '{transcoder}'", transcoder);
}

if (options.NodeLocatorFactory != null)
Expand Down
4 changes: 4 additions & 0 deletions src/Enyim.Caching/Configuration/MemcachedClientOptionsT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Enyim.Caching.Configuration;

public class MemcachedClientOptions<T> : MemcachedClientOptions
{ }
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static IServiceCollection AddEnyimMemcached<T>(
}

return services.AddEnyimMemcached<T>(
s => s.AddOptions<MemcachedClientOptions>().BindConfiguration(sectionKey));
s => s.AddOptions<MemcachedClientOptions<T>>().BindConfiguration(sectionKey));
}

public static IServiceCollection AddEnyimMemcached<T>(
Expand All @@ -154,7 +154,7 @@ public static IServiceCollection AddEnyimMemcached<T>(
}

return services.AddEnyimMemcached<T>(
s => s.Configure<MemcachedClientOptions>(configuration.GetSection(sectionKey)));
s => s.Configure<MemcachedClientOptions<T>>(configuration.GetSection(sectionKey)));
}
#endif

Expand All @@ -171,7 +171,7 @@ public static IServiceCollection AddEnyimMemcached<T>(
services.TryAddSingleton<IMemcachedClient<T>>(sp =>
{
var loggerFactory = sp.GetRequiredService<ILoggerFactory>();
var options = sp.GetRequiredService<IOptions<MemcachedClientOptions>>();
var options = sp.GetRequiredService<IOptions<MemcachedClientOptions<T>>>();
var conf = new MemcachedClientConfiguration(loggerFactory, options);
return new MemcachedClient<T>(loggerFactory, conf);
});
Expand Down
6 changes: 3 additions & 3 deletions src/Enyim.Caching/IMemcachedClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using Enyim.Caching.Memcached;
using Enyim.Caching.Memcached;
using Enyim.Caching.Memcached.Results;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Enyim.Caching.Memcached.Results;

namespace Enyim.Caching
{
Expand Down
Loading