Exemple #1
0
        public static RedisEndpoint ToRedisEndpoint(this string connectionString, int?defaultPort = null)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException("connectionString");
            }
            if (connectionString.StartsWith("redis://"))
            {
                connectionString = connectionString.Substring("redis://".Length);
            }

            var domainParts    = connectionString.SplitOnLast('@');
            var qsParts        = domainParts.Last().SplitOnFirst('?');
            var hostParts      = qsParts[0].SplitOnLast(':');
            var useDefaultPort = true;
            var port           = defaultPort.GetValueOrDefault(RedisConfig.DefaultPort);

            if (hostParts.Length > 1)
            {
                port           = int.Parse(hostParts[1]);
                useDefaultPort = false;
            }

            var endpoint = new RedisEndpoint(hostParts[0], port);

            if (domainParts.Length > 1)
            {
                var authParts = domainParts[0].SplitOnFirst(':');
                if (authParts.Length > 1)
                {
                    endpoint.Client = authParts[0];
                }

                endpoint.Password = authParts.Last();
            }

            if (qsParts.Length > 1)
            {
                var qsParams = qsParts[1].Split('&');
                foreach (var param in qsParams)
                {
                    var entry = param.Split('=');
                    var value = entry.Length > 1 ? WebUtility.UrlDecode(entry[1]) : null;
                    if (value == null)
                    {
                        continue;
                    }

                    var name = entry[0].ToLower();
                    HandleEndpointByName(endpoint, useDefaultPort, name, value);
                }
            }

            return(endpoint);
        }
Exemple #2
0
        private static void HandleEndpointByName(RedisEndpoint endpoint, bool useDefaultPort, string name, string value)
        {
            switch (name)
            {
            case "db":
                endpoint.Db = int.Parse(value);
                break;

            case "ssl":
                endpoint.Ssl = bool.Parse(value);
                if (useDefaultPort)
                {
                    endpoint.Port = RedisConfig.DefaultPortSsl;
                }
                break;

            case "client":
                endpoint.Client = value;
                break;

            case "password":
                endpoint.Password = value;
                break;

            case "namespaceprefix":
                endpoint.NamespacePrefix = value;
                break;

            case "connecttimeout":
                endpoint.ConnectTimeout = int.Parse(value);
                break;

            case "sendtimeout":
                endpoint.SendTimeout = int.Parse(value);
                break;

            case "receivetimeout":
                endpoint.ReceiveTimeout = int.Parse(value);
                break;

            case "retrytimeout":
                endpoint.RetryTimeout = int.Parse(value);
                break;

            case "idletimeout":
            case "idletimeoutsecs":
                endpoint.IdleTimeOutSecs = int.Parse(value);
                break;
            }
        }
Exemple #3
0
 protected bool Equals(RedisEndpoint other)
 {
     return(string.Equals(Host, other.Host) &&
            Port == other.Port &&
            Ssl.Equals(other.Ssl) &&
            ConnectTimeout == other.ConnectTimeout &&
            SendTimeout == other.SendTimeout &&
            ReceiveTimeout == other.ReceiveTimeout &&
            RetryTimeout == other.RetryTimeout &&
            IdleTimeOutSecs == other.IdleTimeOutSecs &&
            Db == other.Db &&
            string.Equals(Client, other.Client) &&
            string.Equals(Password, other.Password) &&
            string.Equals(NamespacePrefix, other.NamespacePrefix));
 }
Exemple #4
0
        public static RedisEndpoint ToRedisEndpoint(this string connectionString, int?defaultPort = null)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException("connectionString");
            }
            if (connectionString.StartsWith("redis://"))
            {
                connectionString = connectionString.Substring("redis://".Length);
            }

            var domainParts    = connectionString.SplitOnLast('@');
            var qsParts        = domainParts.Last().SplitOnFirst('?');
            var hostParts      = qsParts[0].SplitOnLast(':');
            var useDefaultPort = true;
            var port           = defaultPort.GetValueOrDefault(RedisConfig.DefaultPort);

            if (hostParts.Length > 1)
            {
                port           = int.Parse(hostParts[1]);
                useDefaultPort = false;
            }

            var endpoint = new RedisEndpoint(hostParts[0], port);

            if (domainParts.Length > 1)
            {
                var authParts = domainParts[0].SplitOnFirst(':');
                if (authParts.Length > 1)
                {
                    endpoint.Client = authParts[0];
                }

                endpoint.Password = authParts.Last();
            }

            if (qsParts.Length > 1)
            {
                var qsParams = qsParts[1].Split('&');
                foreach (var param in qsParams)
                {
                    var entry = param.Split('=');
                    var value = entry.Length > 1 ? WebUtility.UrlDecode(entry[1]) : null;
                    if (value == null)
                    {
                        continue;
                    }

                    var name = entry[0].ToLower();
                    switch (name)
                    {
                    case "db":
                        endpoint.Db = int.Parse(value);
                        break;

                    case "ssl":
                        endpoint.Ssl = bool.Parse(value);
                        if (useDefaultPort)
                        {
                            endpoint.Port = RedisConfig.DefaultPortSsl;
                        }
                        break;

                    case "client":
                        endpoint.Client = value;
                        break;

                    case "password":
                        endpoint.Password = value;
                        break;

                    case "namespaceprefix":
                        endpoint.NamespacePrefix = value;
                        break;

                    case "connecttimeout":
                        endpoint.ConnectTimeout = int.Parse(value);
                        break;

                    case "sendtimeout":
                        endpoint.SendTimeout = int.Parse(value);
                        break;

                    case "receivetimeout":
                        endpoint.ReceiveTimeout = int.Parse(value);
                        break;

                    case "retrytimeout":
                        endpoint.RetryTimeout = int.Parse(value);
                        break;

                    case "idletimeout":
                    case "idletimeoutsecs":
                        endpoint.IdleTimeOutSecs = int.Parse(value);
                        break;
                    }
                }
            }

            return(endpoint);
        }