/// <summary>
        /// Gets the configuration for working with Redis
        /// </summary>
        /// <param name="configSection"></param>
        /// <returns></returns>
        public static RedisClientConfiguration GetRedisConfiguration(this CacheConfigurationSectionHandler configSection)
        {
            var configuration = new RedisClientConfiguration();

            if (configSection.Section.SelectNodes("servers/add") is XmlNodeList servers)
            {
                foreach (XmlNode server in servers)
                {
                    if ("redis".Equals((server.Attributes["type"]?.Value ?? "Redis").Trim().ToLower()))
                    {
                        var address  = server.Attributes["address"]?.Value ?? "localhost";
                        var endpoint = (address.IndexOf(".") > 0 && address.IndexOf(":") > 0) || (address.IndexOf(":") > 0 && address.IndexOf("]:") > 0)
                                                        ? ConfigurationHelper.ResolveToEndPoint(address)
                                                        : ConfigurationHelper.ResolveToEndPoint(address, Int32.TryParse(server.Attributes["port"]?.Value ?? "6379", out int port) ? port : 6379);
                        configuration.Servers.Add(endpoint as IPEndPoint);
                    }
                }
            }

            if (configSection.Section.SelectSingleNode("options") is XmlNode options)
            {
                foreach (XmlAttribute option in options.Attributes)
                {
                    if (!string.IsNullOrWhiteSpace(option.Value))
                    {
                        configuration.Options += (configuration.Options != "" ? "," : "") + option.Name + "=" + option.Value;
                    }
                }
            }

            return(configuration);
        }
示例#2
0
        public CacheConfiguration(CacheConfigurationSectionHandler configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration), "No configuration is found");
            }

            this.Provider   = configuration.Section.Attributes["provider"]?.Value ?? "Redis";
            this.RegionName = configuration.Section.Attributes["region"]?.Value ?? "VIEApps-NGX-Cache";
            if (Int32.TryParse(configuration.Section.Attributes["expirationTime"]?.Value ?? "30", out int intValue))
            {
                this.ExpirationTime = intValue;
            }

            if (configuration.Section.SelectNodes("servers/add") is XmlNodeList servers)
            {
                foreach (XmlNode server in servers)
                {
                    var type = server.Attributes["type"]?.Value ?? "Redis";
                    this.Servers.Add(new CacheServer(server.Attributes["address"]?.Value ?? "localhost", Int32.TryParse(server.Attributes["port"]?.Value ?? (type.ToLower().Equals("redis") ? "6379" : "11211"), out int port) ? port : type.ToLower().Equals("redis") ? 6379 : 11211, type));
                }
            }

            if (configuration.Section.SelectSingleNode("options") is XmlNode options)
            {
                foreach (XmlAttribute option in options.Attributes)
                {
                    if (!string.IsNullOrWhiteSpace(option.Value))
                    {
                        this.Options += (this.Options != "" ? "," : "") + option.Name + "=" + option.Value;
                    }
                }
            }

            if (Enum.TryParse(configuration.Section.Attributes["protocol"]?.Value ?? "Binary", out MemcachedProtocol protocol))
            {
                this.Protocol = protocol;
            }

            if (configuration.Section.SelectSingleNode("socketPool") is XmlNode socketpool)
            {
                if (Int32.TryParse(socketpool.Attributes["maxPoolSize"]?.Value, out intValue))
                {
                    this.SocketPool.MaxPoolSize = intValue;
                }
                if (Int32.TryParse(socketpool.Attributes["minPoolSize"]?.Value, out intValue))
                {
                    this.SocketPool.MinPoolSize = intValue;
                }
                if (TimeSpan.TryParse(socketpool.Attributes["connectionTimeout"]?.Value, out TimeSpan timespanValue))
                {
                    this.SocketPool.ConnectionTimeout = timespanValue;
                }
                if (TimeSpan.TryParse(socketpool.Attributes["deadTimeout"]?.Value, out timespanValue))
                {
                    this.SocketPool.DeadTimeout = timespanValue;
                }
                if (TimeSpan.TryParse(socketpool.Attributes["queueTimeout"]?.Value, out timespanValue))
                {
                    this.SocketPool.QueueTimeout = timespanValue;
                }
                if (TimeSpan.TryParse(socketpool.Attributes["receiveTimeout"]?.Value, out timespanValue))
                {
                    this.SocketPool.ReceiveTimeout = timespanValue;
                }
                if (Boolean.TryParse(socketpool.Attributes["noDelay"]?.Value, out bool boolValue))
                {
                    this.SocketPool.NoDelay = boolValue;
                }

                if ("throttling" == socketpool.Attributes["failurePolicy"]?.Value)
                {
                    this.SocketPool.FailurePolicyFactory = new ThrottlingFailurePolicyFactory(Int32.TryParse(socketpool.Attributes["failureThreshold"]?.Value, out intValue) ? intValue : 4, TimeSpan.TryParse(socketpool.Attributes["resetAfter"]?.Value, out timespanValue) ? timespanValue : TimeSpan.FromSeconds(5));
                }
            }

            if (configuration.Section.SelectSingleNode("authentication") is XmlNode authentication)
            {
                if (authentication.Attributes["type"]?.Value != null)
                {
                    try
                    {
                        this.Authentication.Type = authentication.Attributes["type"].Value;
                        if (authentication.Attributes["zone"]?.Value != null)
                        {
                            this.Authentication.Parameters.Add("zone", authentication.Attributes["zone"].Value);
                        }
                        if (authentication.Attributes["userName"]?.Value != null)
                        {
                            this.Authentication.Parameters.Add("userName", authentication.Attributes["userName"].Value);
                        }
                        if (authentication.Attributes["password"]?.Value != null)
                        {
                            this.Authentication.Parameters.Add("password", authentication.Attributes["password"].Value);
                        }
                    }
                    catch { }
                }
            }

            if (configuration.Section.SelectSingleNode("keyTransformer") is XmlNode keyTransformer)
            {
                this.KeyTransformer = keyTransformer.Attributes["type"]?.Value;
            }

            if (configuration.Section.SelectSingleNode("transcoder") is XmlNode transcoder)
            {
                this.Transcoder = transcoder.Attributes["type"]?.Value;
            }

            if (configuration.Section.SelectSingleNode("nodeLocator") is XmlNode nodeLocator)
            {
                this.NodeLocator = nodeLocator.Attributes["type"]?.Value;
            }
        }
 /// <summary>
 /// Gets the configuration for working with Memcached
 /// </summary>
 /// <param name="configSection"></param>
 /// <param name="loggerFactory"></param>
 /// <returns></returns>
 public static MemcachedClientConfiguration GetMemcachedConfiguration(this CacheConfigurationSectionHandler configSection, ILoggerFactory loggerFactory = null)
 => new MemcachedClientConfiguration(loggerFactory, configSection);