/// <summary> /// Initializes a new instance of the <see cref="T:MemcachedClientConfiguration"/> class. /// </summary> public MemcachedClientConfiguration( ILoggerFactory loggerFactory, IOptions <MemcachedClientOptions> optionsAccessor, IConfiguration configuration = null, ITranscoder transcoder = null, IMemcachedKeyTransformer keyTransformer = null) { if (optionsAccessor == null) { throw new ArgumentNullException(nameof(optionsAccessor)); } _logger = loggerFactory.CreateLogger <MemcachedClientConfiguration>(); var options = optionsAccessor.Value; if ((options == null || options.Servers.Count == 0) && configuration != null) { var section = configuration.GetSection("enyimMemcached"); if (section.Exists()) { section.Bind(options); } else { _logger.LogWarning($"No enyimMemcached setting in appsetting.json. Use default configuration"); options.AddDefaultServer(); } } ConfigureServers(options); SocketPool = new SocketPoolConfiguration(); if (options.SocketPool != null) { options.SocketPool.CheckPoolSize(); options.SocketPool.CheckTimeout(); SocketPool.MinPoolSize = options.SocketPool.MinPoolSize; _logger.LogInformation($"{nameof(SocketPool.MinPoolSize)}: {SocketPool.MinPoolSize}"); SocketPool.MaxPoolSize = options.SocketPool.MaxPoolSize; _logger.LogInformation($"{nameof(SocketPool.MaxPoolSize)}: {SocketPool.MaxPoolSize}"); SocketPool.ConnectionTimeout = options.SocketPool.ConnectionTimeout; _logger.LogInformation($"{nameof(SocketPool.ConnectionTimeout)}: {SocketPool.ConnectionTimeout}"); SocketPool.ReceiveTimeout = options.SocketPool.ReceiveTimeout; _logger.LogInformation($"{nameof(SocketPool.ReceiveTimeout)}: {SocketPool.ReceiveTimeout}"); SocketPool.DeadTimeout = options.SocketPool.DeadTimeout; _logger.LogInformation($"{nameof(SocketPool.DeadTimeout)}: {SocketPool.DeadTimeout}"); SocketPool.QueueTimeout = options.SocketPool.QueueTimeout; _logger.LogInformation($"{nameof(SocketPool.QueueTimeout)}: {SocketPool.QueueTimeout}"); SocketPool.InitPoolTimeout = options.SocketPool.InitPoolTimeout; } Protocol = options.Protocol; if (options.Authentication != null && !string.IsNullOrEmpty(options.Authentication.Type)) { try { var authenticationType = Type.GetType(options.Authentication.Type); if (authenticationType != null) { _logger.LogDebug($"Authentication type is {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}'."); } } else { _logger.LogError($"Unable to load authentication type {options.Authentication.Type}."); } } catch (Exception ex) { _logger.LogError(new EventId(), ex, $"Unable to load authentication type {options.Authentication.Type}."); } } if (!string.IsNullOrEmpty(options.KeyTransformer)) { try { var keyTransformerType = Type.GetType(options.KeyTransformer); if (keyTransformerType != null) { KeyTransformer = Activator.CreateInstance(keyTransformerType) as IMemcachedKeyTransformer; _logger.LogDebug($"Use '{options.KeyTransformer}' KeyTransformer"); } } catch (Exception ex) { _logger.LogError(new EventId(), ex, $"Unable to load '{options.KeyTransformer}' KeyTransformer"); } } else if (keyTransformer != null) { this._keyTransformer = keyTransformer; _logger.LogDebug($"Use KeyTransformer Type : '{keyTransformer.ToString()}'"); } if (NodeLocator == null) { NodeLocator = options.Servers.Count > 1 ? typeof(DefaultNodeLocator) : typeof(SingleNodeLocator); } if (!string.IsNullOrEmpty(options.Transcoder)) { try { if (options.Transcoder == "BinaryFormatterTranscoder") { options.Transcoder = "Enyim.Caching.Memcached.Transcoders.BinaryFormatterTranscoder"; } var transcoderType = Type.GetType(options.Transcoder); if (transcoderType != null) { Transcoder = Activator.CreateInstance(transcoderType) as ITranscoder; _logger.LogDebug($"Use '{options.Transcoder}'"); } } catch (Exception ex) { _logger.LogError(new EventId(), ex, $"Unable to load '{options.Transcoder}'"); } } else if (transcoder != null) { this._transcoder = transcoder; _logger.LogDebug($"Use Transcoder Type : '{transcoder.ToString()}'"); } if (options.NodeLocatorFactory != null) { NodeLocatorFactory = options.NodeLocatorFactory; } }
/// <summary> /// Initializes a new instance of the <see cref="T:MemcachedClientConfiguration"/> class. /// </summary> public MemcachedClientConfiguration( ILogFactory loggerFactory, IOptions <MemcachedClientOptions> optionsAccessor) { if (optionsAccessor == null) { throw new ArgumentNullException(nameof(optionsAccessor)); } _logger = loggerFactory.GetLogger <MemcachedClientConfiguration>(); var options = optionsAccessor.Value; Servers = new List <EndPoint>(); foreach (var server in options.Servers) { IPAddress address; if (IPAddress.TryParse(server.Address, out address)) { Servers.Add(new IPEndPoint(address, server.Port)); } else { Servers.Add(new DnsEndPoint(server.Address, server.Port)); } } SocketPool = options.SocketPool; Protocol = options.Protocol; if (options.Authentication != null && !string.IsNullOrEmpty(options.Authentication.Type)) { try { var authenticationType = Type.GetType(options.Authentication.Type); if (authenticationType != null) { _logger.Debug($"Authentication type is {authenticationType}."); Authentication = new AuthenticationConfiguration(); Authentication.Type = authenticationType; foreach (var parameter in options.Authentication.Parameters) { Authentication.Parameters[parameter.Key] = parameter.Value; _logger.Debug($"Authentication {parameter.Key} is '{parameter.Value}'."); } } else { _logger.Error($"Unable to load authentication type {options.Authentication.Type}."); } } catch (Exception ex) { _logger.Error($"Unable to load authentication type {options.Authentication.Type}.", ex); } } if (!string.IsNullOrEmpty(options.KeyTransformer)) { try { var keyTransformerType = Type.GetType(options.KeyTransformer); if (keyTransformerType != null) { KeyTransformer = Activator.CreateInstance(keyTransformerType) as IMemcachedKeyTransformer; _logger.Debug($"Use '{options.KeyTransformer}' KeyTransformer"); } } catch (Exception ex) { _logger.Error($"Unable to load '{options.KeyTransformer}' KeyTransformer", ex); } } if (options.Transcoder != null) { _transcoder = options.Transcoder; } if (options.NodeLocatorFactory != null) { NodeLocatorFactory = options.NodeLocatorFactory; } }