Skip to content

Commit

Permalink
(glavGH-3) memcached - Allow User/Pass configuration
Browse files Browse the repository at this point in the history
Previously a user could not control username/password when working with
memcached data stores, even though Enyim Memcached Client supports the
ability for user/pass configuration. All setting username / password as
part of the specific configuration data that can be provided in a
config file.
  • Loading branch information
ferventcoder committed Oct 3, 2018
1 parent ce00ffc commit 90a96f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Glav.CacheAdapter/Distributed/memcached/memcachedCacheFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class memcachedCacheFactory : CacheConstructionFactoryBase
private TimeSpan _connectTimeout = TimeSpan.FromSeconds(5);
// this is set to text by default due to issues with Binary and Transcoder
private string _protocol = "Text";
private string _userName = "";
private string _password = "";
private TimeSpan _deadNodeTimeout = TimeSpan.FromSeconds(30);
private static bool _isInitialised;
private static readonly object _lockRef = new object();
Expand All @@ -34,6 +36,8 @@ public memcachedCacheFactory(ILogging logger, CacheConfig config = null)
public int MaximumPoolSize { get { return _maxPoolSize; } }
public TimeSpan ConnectTimeout { get { return _connectTimeout; } }
public string Protocol { get { return _protocol; } }
public string UserName { get { return _userName; } }
public string Password { get { return _password; } }
public TimeSpan DeadNodeTimeout { get { return _deadNodeTimeout; } }

public override CacheFactoryComponentResult CreateCacheComponents()
Expand Down Expand Up @@ -74,6 +78,15 @@ private ICache CreateCacheEngine()
config.Protocol = protocol;

config.Transcoder = new DataContractTranscoder();

if (!string.IsNullOrWhiteSpace(UserName) && !string.IsNullOrWhiteSpace(Password))
{
config.Authentication.Type = typeof(PlainTextAuthenticator);
config.Authentication.Parameters.Add("userName", UserName);
config.Authentication.Parameters.Add("password", Password);
config.Authentication.Parameters.Add("zone", string.Empty);
}

_client = new MemcachedClient(config);
Logger.WriteInfoMessage("memcachedAdapter initialised.");
LogManager.AssignFactory(new LogFactoryAdapter(Logger));
Expand Down Expand Up @@ -137,6 +150,9 @@ private void ExtractCacheSpecificConfig()
_protocol = protocol;
}

_userName = CacheConfiguration.GetConfigValueFromProviderSpecificValues(memcachedConstants.CONFIG_UserName);
_password = CacheConfiguration.GetConfigValueFromProviderSpecificValues(memcachedConstants.CONFIG_Password);

var connectTimeoutValue = CacheConfiguration.GetConfigValueFromProviderSpecificValues(memcachedConstants.CONFIG_ConnectionTimeout);
var deadTimeoutValue = CacheConfiguration.GetConfigValueFromProviderSpecificValues(memcachedConstants.CONFIG_DeadNodeTimeout);

Expand Down
2 changes: 2 additions & 0 deletions Glav.CacheAdapter/Distributed/memcached/memcachedConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class memcachedConstants
public const string CONFIG_MaximumConnectionPoolSize = "MaxPoolSize";
public const string CONFIG_ConnectionTimeout = "ConnectionTimeout";
public const string CONFIG_Protocol = "Protocol";
public const string CONFIG_UserName = "UserName";
public const string CONFIG_Password = "Password";
public const string CONFIG_DeadNodeTimeout = "DeadNodeTimeout";
}
}

0 comments on commit 90a96f1

Please sign in to comment.