Skip to content

Commit

Permalink
Merge pull request #19 from gep13/ticket/master/GH-3_memcached-with-u…
Browse files Browse the repository at this point in the history
…ser-pass

(GH-3) Allow usage of MemcachedCloud (redislabs.com)
  • Loading branch information
glav authored Mar 20, 2021
2 parents 5c5f405 + 5002086 commit 2bc2084
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
33 changes: 32 additions & 1 deletion Glav.CacheAdapter/Distributed/memcached/memcachedCacheFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class memcachedCacheFactory : CacheConstructionFactoryBase
private int _minPoolSize = 10;
private int _maxPoolSize = 20;
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 @@ -31,6 +35,9 @@ public memcachedCacheFactory(ILogging logger, CacheConfig config = null)
public int MinimumPoolSize { get { return _minPoolSize; } }
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 @@ -63,8 +70,23 @@ private ICache CreateCacheEngine()
// Note: Tried using the Binary protocol here but I consistently got unreliable results in tests.
// TODO: Need to investigate further why Binary protocol is unreliable in this scenario.
// Could be related to memcached version and/or transcoder.
config.Protocol = MemcachedProtocol.Text;
var protocol = MemcachedProtocol.Text;
if (!string.IsNullOrWhiteSpace(Protocol))
{
Enum.TryParse(Protocol, true, out protocol);
}
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 @@ -122,6 +144,15 @@ private void ExtractCacheSpecificConfig()
_maxPoolSize = value;
}

var protocol = CacheConfiguration.GetConfigValueFromProviderSpecificValues(memcachedConstants.CONFIG_Protocol);
if (!string.IsNullOrWhiteSpace(protocol))
{
_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
7 changes: 5 additions & 2 deletions Glav.CacheAdapter/Distributed/memcached/memcachedConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class memcachedConstants
public const string CONFIG_MinimumConnectionPoolSize = "MinPoolSize";
public const string CONFIG_MaximumConnectionPoolSize = "MaxPoolSize";
public const string CONFIG_ConnectionTimeout = "ConnectionTimeout";
public const string CONFIG_DeadNodeTimeout = "DeadNodeTimeout";
}
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 2bc2084

Please sign in to comment.