コード例 #1
0
        public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
        {
            var client = ClientFactory(config);

            if (master && RedisConfig.VerifyMasterConnections)
            {
                var       firstAttempt  = DateTime.UtcNow;
                Exception firstEx       = null;
                var       retryTimeSpan = TimeSpan.FromMilliseconds(config.RetryTimeout);
                var       i             = 0;
                while (DateTime.UtcNow - firstAttempt < retryTimeSpan)
                {
                    try
                    {
                        client = GetValidMaster(client, config);
                        return(client);
                    }
                    catch (Exception ex)
                    {
                        if (!RedisConfig.RetryReconnectOnFailedMasters)
                        {
                            throw;
                        }

                        firstEx ??= ex;
                        ExecUtils.SleepBackOffMultiplier(++i);
                        client?.Dispose();
                        client = ClientFactory(config);
                    }
                }
                throw new TimeoutException($"Could not resolve master instance within {config.RetryTimeout}ms RetryTimeout", firstEx);
            }

            return(client);
        }
コード例 #2
0
        public void SaveItem(Item item)
        {
            var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
            using (IRedisClient client = new RedisClient(conf))
            {
                var itemClient = client.As<Item>();
                var itemList = itemClient.Lists["urn:item:" + item.ProductID];
                item.Id = itemClient.GetNextSequence();
                itemList.Add(item);

                client.AddItemToSortedSet("urn:Rank", item.Name, item.Price);

                //Publis top 5 Ranked Items
                IDictionary<string, double> Data = client.GetRangeWithScoresFromSortedSet("urn:Rank", 0, 4);
                List<Item> RankList = new List<Item>();
                int counter = 0;
                foreach (var itm in Data)
                {
                    counter++;
                    RankList.Add(new Item() { Name = itm.Key, Price = (int)itm.Value, Id = counter });
                }

                var itemJson = JsonConvert.SerializeObject(RankList);
                client.PublishMessage("Rank", itemJson);
                //---------------------------------------------
            }
        }
コード例 #3
0
        /// <summary>
        /// Called within a lock
        /// </summary>
        /// <returns></returns>
        private RedisClient GetInActiveWriteClient()
        {
            var desiredIndex = WritePoolIndex % writeClients.Length;

            //this will loop through all hosts in readClients once even though there are 2 for loops
            //both loops are used to try to get the prefered host according to the round robin algorithm
            for (int x = 0; x < ReadWriteHosts.Count; x++)
            {
                var           nextHostIndex = (desiredIndex + x) % ReadWriteHosts.Count;
                RedisEndpoint nextHost      = ReadWriteHosts[nextHostIndex];
                for (var i = nextHostIndex; i < writeClients.Length; i += ReadWriteHosts.Count)
                {
                    if (writeClients[i] != null && !writeClients[i].Active && !writeClients[i].HadExceptions)
                    {
                        return(writeClients[i]);
                    }
                    else if (writeClients[i] == null || writeClients[i].HadExceptions)
                    {
                        if (writeClients[i] != null)
                        {
                            writeClients[i].DisposeConnection();
                        }

                        var client = InitNewClient(nextHost);
                        writeClients[i] = client;

                        return(client);
                    }
                }
            }
            return(null);
        }
コード例 #4
0
        private RedisClient InitNewClient(RedisEndpoint nextHost)
        {
            var client = RedisClientFactory.CreateRedisClient(nextHost);

            client.Id = Interlocked.Increment(ref RedisClientCounter);
            client.ConnectionFilter = ConnectionFilter;
            if (this.ConnectTimeout != null)
            {
                client.ConnectTimeout = this.ConnectTimeout.Value;
            }
            if (this.SocketSendTimeout.HasValue)
            {
                client.SendTimeout = this.SocketSendTimeout.Value;
            }
            if (this.SocketReceiveTimeout.HasValue)
            {
                client.ReceiveTimeout = this.SocketReceiveTimeout.Value;
            }
            if (this.IdleTimeOutSecs.HasValue)
            {
                client.IdleTimeOutSecs = this.IdleTimeOutSecs.Value;
            }
            if (this.NamespacePrefix != null)
            {
                client.NamespacePrefix = NamespacePrefix;
            }
            if (client.Db != Db) //Reset database to default if changed
            {
                client.ChangeDb(Db);
            }

            return(client);
        }
コード例 #5
0
        static void Main(string[] args)
        {
            var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
            using (IRedisClient client = new RedisClient(conf))
            {
                IRedisSubscription sub = null;
                using (sub = client.CreateSubscription())
                {
                    sub.OnMessage += (channel, message) =>
                    {
                        try
                        {
                            List<Item> items = JsonConvert.DeserializeObject<List<Item>>(message);
                            Console.WriteLine((string)message);
                            SignalRClass sc = new SignalRClass();
                            sc.SendRank(items);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    };
                }
                sub.SubscribeToChannels(new string[] { "Rank" });
            }

            Console.ReadLine();
        }
コード例 #6
0
        private RedisClient InitNewClient(RedisEndpoint nextHost)
        {
            var client = RedisClientFactory.CreateRedisClient(nextHost);

            client.Id               = Interlocked.Increment(ref RedisClientCounter);
            client.ClientManager    = this;
            client.ConnectionFilter = ConnectionFilter;

            return(client);
        }
コード例 #7
0
 public void SaveProduct(Product product)
 {
     var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
     using (IRedisClient client = new RedisClient(conf))
     {
         var userClient = client.As<Product>();
         product.Id = userClient.GetNextSequence();
         userClient.Store(product);
     }
 }
コード例 #8
0
        public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
        {
            var client = ClientFactory(config);

            if (master && RedisConfig.VerifyMasterConnections)
            {
                var role = client.GetServerRole();
                if (role != RedisServerRole.Master)
                {
                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);
                    log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role));
                    var         newMasters   = new List <RedisEndpoint>();
                    var         newSlaves    = new List <RedisEndpoint>();
                    RedisClient masterClient = null;
                    foreach (var hostConfig in allHosts)
                    {
                        try
                        {
                            var testClient = ClientFactory(hostConfig);
                            testClient.ConnectTimeout = RedisConfig.HostLookupTimeoutMs;
                            var testRole = testClient.GetServerRole();
                            switch (testRole)
                            {
                            case RedisServerRole.Master:
                                newMasters.Add(hostConfig);
                                if (masterClient == null)
                                {
                                    masterClient = testClient;
                                }
                                break;

                            case RedisServerRole.Slave:
                                newSlaves.Add(hostConfig);
                                break;
                            }
                        }
                        catch { /* skip */ }
                    }

                    if (masterClient == null)
                    {
                        Interlocked.Increment(ref RedisState.TotalNoMastersFound);
                        var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString()));
                        log.Error(errorMsg);
                        throw new Exception(errorMsg);
                    }

                    ResetMasters(newMasters);
                    ResetSlaves(newSlaves);
                    return(masterClient);
                }
            }

            return(client);
        }
コード例 #9
0
        public RedisSentinelWorker(RedisSentinel sentinel, RedisEndpoint sentinelEndpoint)
        {
            this.sentinel = sentinel;

            //Sentinel Servers doesn't support DB, reset to 0
            var timeoutMs = (int) sentinel.SentinelWorkerTimeout.TotalMilliseconds;
            this.sentinelClient = new RedisClient(sentinelEndpoint) { Db = 0, ConnectTimeout = timeoutMs };

            if (Log.IsDebugEnabled)
                Log.Debug("Set up Redis Sentinel on {0}".Fmt(sentinelEndpoint));
        }
コード例 #10
0
        public RedisSentinelWorker(RedisSentinel sentinel, RedisEndpoint sentinelEndpoint)
        {
            this.sentinel = sentinel;
            this.sentinelClient = new RedisClient(sentinelEndpoint) {
                Db = 0, //Sentinel Servers doesn't support DB, reset to 0
                ConnectTimeout = sentinel.SentinelWorkerTimeoutMs,
            };

            if (Log.IsDebugEnabled)
                Log.Debug("Set up Redis Sentinel on {0}".Fmt(sentinelEndpoint));
        }
コード例 #11
0
        public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
        {
            var client = ClientFactory(config);

            if (master && RedisConfig.VerifyMasterConnections)
            {
                var role = client.GetServerRole();
                if (role != RedisServerRole.Master)
                {
                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);
                    log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role));
                    var newMasters = new List<RedisEndpoint>();
                    var newSlaves = new List<RedisEndpoint>();
                    RedisClient masterClient = null;
                    foreach (var hostConfig in allHosts)
                    {
                        try
                        {
                            var testClient = ClientFactory(hostConfig);
                            testClient.ConnectTimeout = RedisConfig.HostLookupTimeoutMs;
                            var testRole = testClient.GetServerRole();
                            switch (testRole)
                            {
                                case RedisServerRole.Master:
                                    newMasters.Add(hostConfig);
                                    if (masterClient == null)
                                        masterClient = testClient;
                                    break;
                                case RedisServerRole.Slave:
                                    newSlaves.Add(hostConfig);
                                    break;
                            }

                        }
                        catch { /* skip */ }
                    }

                    if (masterClient == null)
                    {
                        Interlocked.Increment(ref RedisState.TotalNoMastersFound);
                        var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString()));
                        log.Error(errorMsg);
                        throw new Exception(errorMsg);
                    }

                    ResetMasters(newMasters);
                    ResetSlaves(newSlaves);
                    return masterClient;
                }
            }

            return client;
        }
コード例 #12
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 &&
            IdleTimeOutSecs == other.IdleTimeOutSecs &&
            Db == other.Db &&
            string.Equals(Client, other.Client) &&
            string.Equals(Password, other.Password) &&
            string.Equals(NamespacePrefix, other.NamespacePrefix));
 }
コード例 #13
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 
         && IdleTimeOutSecs == other.IdleTimeOutSecs 
         && Db == other.Db 
         && string.Equals(Client, other.Client) 
         && string.Equals(Password, other.Password) 
         && string.Equals(NamespacePrefix, other.NamespacePrefix);
 }
コード例 #14
0
        private RedisClient InitNewClient(RedisEndpoint nextHost)
        {
            var client = RedisClientFactory.CreateRedisClient(nextHost);

            client.Id               = Interlocked.Increment(ref RedisClientCounter);
            client.ClientManager    = this;
            client.ConnectionFilter = ConnectionFilter;
            if (NamespacePrefix != null)
            {
                client.NamespacePrefix = NamespacePrefix;
            }

            return(client);
        }
コード例 #15
0
        public RedisSentinelWorker(RedisSentinel sentinel, RedisEndpoint sentinelEndpoint)
        {
            this.sentinel       = sentinel;
            this.sentinelClient = new RedisClient(sentinelEndpoint)
            {
                Db             = 0, //Sentinel Servers doesn't support DB, reset to 0
                ConnectTimeout = sentinel.SentinelWorkerConnectTimeoutMs,
                ReceiveTimeout = sentinel.SentinelWorkerReceiveTimeoutMs,
                SendTimeout    = sentinel.SentinelWorkerSendTimeoutMs,
            };

            if (Log.IsDebugEnabled)
            {
                Log.Debug("Set up Redis Sentinel on {0}".Fmt(sentinelEndpoint));
            }
        }
コード例 #16
0
        public void UpdateItem(Item item)
        {
            var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
            using (IRedisClient client = new RedisClient(conf))
            {
                IRedisTypedClient<Item> itemClient = client.As<Item>();
                IRedisList<Item> itemList = itemClient.Lists["urn:item:" + item.ProductID];

                var index = itemList.Select((Value, Index) => new { Value, Index })
                 .Single(p => p.Value.Id == item.Id).Index;

                var toUpdateItem = itemList.First(x => x.Id == item.Id);

                //var index = itemList.IndexOf(toUpdateItem);

                toUpdateItem.Name = item.Name;
                toUpdateItem.Price = item.Price;

                itemList.RemoveAt(index);
                if (itemList.Count - 1 < index)
                    itemList.Add(toUpdateItem);
                else itemList.Insert(index, toUpdateItem);

                client.RemoveItemFromSortedSet("urn:Rank", item.Name);
                client.AddItemToSortedSet("urn:Rank", item.Name, item.Price);

                //Publis top 5 Ranked Items
                IDictionary<string, double> Data = client.GetRangeWithScoresFromSortedSet("urn:Rank", 0, 4);
                List<Item> RankList = new List<Item>();
                int counter = 0;
                foreach (var itm in Data)
                {
                    counter++;
                    RankList.Add(new Item() { Name = itm.Key, Price = (int)itm.Value, Id = counter });
                }

                var itemJson = JsonConvert.SerializeObject(RankList);
                client.PublishMessage("Rank", itemJson);
                //---------------------------------------------
            }
        }
コード例 #17
0
        public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool readWrite)
        {
            var client = RedisConfig.ClientFactory(config);

            if (readWrite)
            {
                var role = client.GetServerRole();
                if (role != RedisServerRole.Master)
                {
                    try
                    {
                        var stopwatch = Stopwatch.StartNew();
                        while (true)
                        {
                            var masterConfig = sentinel.GetMaster();
                            var master = RedisConfig.ClientFactory(masterConfig);
                            var masterRole = master.GetServerRole();
                            if (masterRole == RedisServerRole.Master)
                                return master;

                            if (stopwatch.Elapsed > sentinel.MaxWaitBetweenSentinelLookups)
                                throw new TimeoutException("Max Wait Between Sentinel Lookups Elapsed: {0}"
                                    .Fmt(sentinel.MaxWaitBetweenSentinelLookups.ToString()));

                            Thread.Sleep(sentinel.WaitBetweenSentinelLookups);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role), ex);
                        var newMasters = new List<RedisEndpoint>();
                        var newSlaves = new List<RedisEndpoint>();
                        RedisClient masterClient = null;
                        foreach (var hostConfig in allHosts)
                        {
                            try
                            {
                                var testClient = new RedisClient(hostConfig)
                                {
                                    ConnectTimeout = RedisConfig.HostLookupTimeout
                                };
                                var testRole = testClient.GetServerRole();
                                switch (testRole)
                                {
                                    case RedisServerRole.Master:
                                        newMasters.Add(hostConfig);
                                        if (masterClient == null)
                                            masterClient = testClient;
                                        break;
                                    case RedisServerRole.Slave:
                                        newSlaves.Add(hostConfig);
                                        break;
                                }

                            }
                            catch { /* skip */ }
                        }

                        if (masterClient == null)
                        {
                            var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString()));
                            log.Error(errorMsg);
                            throw new Exception(errorMsg);
                        }

                        ResetMasters(newMasters);
                        ResetSlaves(newSlaves);
                        return masterClient;
                    }
                }
            }

            return client;
        }
コード例 #18
0
 public JsonResult GetProducts()
 {
     var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
     using (IRedisClient client = new RedisClient(conf))
     {
         var productClient = client.As<Product>();
         var products = productClient.GetAll();
         return Json(products, JsonRequestBehavior.AllowGet);
     }
 }
コード例 #19
0
 public RedisClient CreateRedisClient(RedisEndpoint config, bool master)
 {
     return(ClientFactory(config));
 }
コード例 #20
0
 public JsonResult GetRanks()
 {
     var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
     using (IRedisClient client = new RedisClient(conf))
     {
         IDictionary<string, double> Data = client.GetRangeWithScoresFromSortedSet("urn:Rank",0,4);                
         List<Item> RankList = new List<Item>();
         int counter = 0;
         foreach(var item in Data)
         {
             counter++;
             RankList.Add(new Item() { Name = item.Key, Price = (int)item.Value, Id=counter });
         }                
         return Json(RankList, JsonRequestBehavior.AllowGet);
     }
 }
コード例 #21
0
 public static RedisClient CreateRedisClient(this IRedisResolver resolver, RedisEndpoint config, bool master)
 {
     return ((IRedisResolverExtended)resolver).CreateRedisClient(config, master);
 }
コード例 #22
0
 public static RedisClient CreateRedisClient(this IRedisResolver resolver, RedisEndpoint config, bool master)
 {
     return(((IRedisResolverExtended)resolver).CreateRedisClient(config, master));
 }
コード例 #23
0
 public RedisClient CreateRedisClient(RedisEndpoint config, bool readWrite)
 {
     return RedisConfig.ClientFactory(config);
 }
コード例 #24
0
        public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
        {
            var client = ClientFactory(config);
            if (master)
            {
                var role = RedisServerRole.Unknown;
                try
                {
                    role = client.GetServerRole();
                    if (role == RedisServerRole.Master)
                    {
                        lastValidMasterFromSentinelAt = DateTime.UtcNow;
                        return client;
                    }
                }
                catch (Exception ex)
                {
                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);

                    if (client.GetHostString() == lastInvalidMasterHost)
                    {
                        lock (oLock)
                        {
                            if (DateTime.UtcNow - lastValidMasterFromSentinelAt > sentinel.WaitBeforeForcingMasterFailover)
                            {
                                lastInvalidMasterHost = null;
                                lastValidMasterFromSentinelAt = DateTime.UtcNow;

                                log.Error("Valid master was not found at '{0}' within '{1}'. Sending SENTINEL failover...".Fmt(
                                    client.GetHostString(), sentinel.WaitBeforeForcingMasterFailover), ex);

                                Interlocked.Increment(ref RedisState.TotalForcedMasterFailovers);

                                sentinel.ForceMasterFailover();
                                TaskUtils.Sleep(sentinel.WaitBetweenFailedHosts);
                                role = client.GetServerRole();
                            }
                        }
                    }
                    else
                    {
                        lastInvalidMasterHost = client.GetHostString();
                    }
                }

                if (role != RedisServerRole.Master && RedisConfig.VerifyMasterConnections)
                {
                    try
                    {
                        var stopwatch = Stopwatch.StartNew();
                        while (true)
                        {
                            try
                            {
                                var masterConfig = sentinel.GetMaster();
                                var masterClient = ClientFactory(masterConfig);
                                masterClient.ConnectTimeout = sentinel.SentinelWorkerConnectTimeoutMs;

                                var masterRole = masterClient.GetServerRole();
                                if (masterRole == RedisServerRole.Master)
                                {
                                    lastValidMasterFromSentinelAt = DateTime.UtcNow;
                                    return masterClient;
                                }
                                else
                                {
                                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);
                                }
                            }
                            catch { /* Ignore errors until MaxWait */ }

                            if (stopwatch.Elapsed > sentinel.MaxWaitBetweenFailedHosts)
                                throw new TimeoutException("Max Wait Between Sentinel Lookups Elapsed: {0}"
                                    .Fmt(sentinel.MaxWaitBetweenFailedHosts.ToString()));

                            TaskUtils.Sleep(sentinel.WaitBetweenFailedHosts);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role), ex);

                        var newMasters = new List<RedisEndpoint>();
                        var newSlaves = new List<RedisEndpoint>();
                        RedisClient masterClient = null;
                        foreach (var hostConfig in allHosts)
                        {
                            try
                            {
                                var testClient = ClientFactory(hostConfig);
                                testClient.ConnectTimeout = RedisConfig.HostLookupTimeoutMs;
                                var testRole = testClient.GetServerRole();
                                switch (testRole)
                                {
                                    case RedisServerRole.Master:
                                        newMasters.Add(hostConfig);
                                        if (masterClient == null)
                                            masterClient = testClient;
                                        break;
                                    case RedisServerRole.Slave:
                                        newSlaves.Add(hostConfig);
                                        break;
                                }

                            }
                            catch { /* skip past invalid master connections */ }
                        }

                        if (masterClient == null)
                        {
                            Interlocked.Increment(ref RedisState.TotalNoMastersFound);
                            var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString()));
                            log.Error(errorMsg);
                            throw new Exception(errorMsg);
                        }

                        ResetMasters(newMasters);
                        ResetSlaves(newSlaves);
                        return masterClient;
                    }
                }
            }

            return client;
        }
コード例 #25
0
 private void Init(RedisEndpoint config)
 {
     Host = config.Host;
     Port = config.Port;
     ConnectTimeout = config.ConnectTimeout;
     SendTimeout = config.SendTimeout;
     ReceiveTimeout = config.ReceiveTimeout;
     RetryTimeout = config.RetryTimeout;
     Password = config.Password;
     NamespacePrefix = config.NamespacePrefix;
     Client = config.Client;
     Db = config.Db;
     Ssl = config.Ssl;
     IdleTimeOutSecs = config.IdleTimeOutSecs;
     ServerVersionNumber = RedisConfig.AssumeServerVersion.GetValueOrDefault();
 }
コード例 #26
0
 public JsonResult GetItems(int? productID)
 {
     var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
     using (IRedisClient client = new RedisClient(conf))
     {
         var itemClient = client.As<Item>();
         var itemList = itemClient.Lists["urn:item:" + productID];
         var items = itemList.GetAll();
         return Json(items, JsonRequestBehavior.AllowGet);
     }
 }
コード例 #27
0
 private void Init(RedisEndpoint config)
 {
     Host = config.Host;
     Port = config.Port;
     ConnectTimeout = config.ConnectTimeout;
     SendTimeout = config.SendTimeout;
     ReceiveTimeout = config.ReceiveTimeout;
     Password = config.Password;
     NamespacePrefix = config.NamespacePrefix;
     Client = config.Client;
     Db = config.Db;
     Ssl = config.Ssl;
     IdleTimeOutSecs = config.IdleTimeOutSecs;
 }
コード例 #28
0
 public JsonResult GetEditItem(int ProductID, int Id)
 {
     var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
     using (IRedisClient client = new RedisClient(conf))
     {
         var itemClient = client.As<Item>();
         var itemList = itemClient.Lists["urn:item:" + ProductID];
         var toUpdateItem = itemList.First(x => x.Id == Id);
         return Json(toUpdateItem, JsonRequestBehavior.AllowGet);
     }
 }        
コード例 #29
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(RedisNativeClient.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 ? entry[1].UrlDecode() : 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 = RedisNativeClient.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 "idletimeout":
                        case "idletimeoutsecs":
                            endpoint.IdleTimeOutSecs = int.Parse(value);
                            break;
                    }
                }
            }

            return endpoint;
        }
コード例 #30
0
        private RedisClient InitNewClient(RedisEndpoint nextHost)
        {
            var client = RedisClientFactory.CreateRedisClient(nextHost);
            client.Id = Interlocked.Increment(ref RedisClientCounter);
            client.ClientManager = this;
            client.ConnectionFilter = ConnectionFilter;

            return client;
        }
コード例 #31
0
 public RedisClient CreateRedisClient(RedisEndpoint config)
 {
     return(new RedisClient(config));
 }
コード例 #32
0
ファイル: ConfigTests.cs プロジェクト: Skynetfy/MyTestProgram
 private static void AssertClient(RedisClient redis, RedisEndpoint expected)
 {
     Assert.That(redis.Host, Is.EqualTo(expected.Host));
     Assert.That(redis.Port, Is.EqualTo(expected.Port));
     Assert.That(redis.Ssl, Is.EqualTo(expected.Ssl));
     Assert.That(redis.Client, Is.EqualTo(expected.Client));
     Assert.That(redis.Password, Is.EqualTo(expected.Password));
     Assert.That(redis.Db, Is.EqualTo(expected.Db));
     Assert.That(redis.ConnectTimeout, Is.EqualTo(expected.ConnectTimeout));
     Assert.That(redis.SendTimeout, Is.EqualTo(expected.SendTimeout));
     Assert.That(redis.ReceiveTimeout, Is.EqualTo(expected.ReceiveTimeout));
    // Assert.That(redis.RetryTimeout, Is.EqualTo(expected.RetryTimeout));
     Assert.That(redis.IdleTimeOutSecs, Is.EqualTo(expected.IdleTimeOutSecs));
     Assert.That(redis.NamespacePrefix, Is.EqualTo(expected.NamespacePrefix));
 }
コード例 #33
0
 public RedisNativeClient(RedisEndpoint config)
 {
     Init(config);
 }
コード例 #34
0
ファイル: ConfigTests.cs プロジェクト: Skynetfy/MyTestProgram
        private static void AssertClientManager(IRedisClientsManager redisManager, RedisEndpoint expected)
        {
            using (var readWrite = (RedisClient)redisManager.GetClient())
            using (var readOnly = (RedisClient)redisManager.GetReadOnlyClient())
            using (var cacheClientWrapper = (RedisClientManagerCacheClient)redisManager.GetCacheClient())
            {
                AssertClient(readWrite, expected);
                AssertClient(readOnly, expected);

                using (var cacheClient = (RedisClient)cacheClientWrapper.GetClient())
                {
                    AssertClient(cacheClient, expected);
                }
            }
        }
コード例 #35
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 ? entry[1].UrlDecode() : 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);
        }
コード例 #36
0
 public RedisClient CreateRedisClient(RedisEndpoint config)
 {
     return new RedisClient(config);
 }
コード例 #37
0
        public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
        {
            var client = ClientFactory(config);

            if (master)
            {
                var role = RedisServerRole.Unknown;
                try
                {
                    role = client.GetServerRole();
                    if (role == RedisServerRole.Master)
                    {
                        lastValidMasterFromSentinelAt = DateTime.UtcNow;
                        return(client);
                    }
                }
                catch (Exception ex)
                {
                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);

                    if (client.GetHostString() == lastInvalidMasterHost)
                    {
                        lock (oLock)
                        {
                            if (DateTime.UtcNow - lastValidMasterFromSentinelAt > sentinel.WaitBeforeForcingMasterFailover)
                            {
                                lastInvalidMasterHost         = null;
                                lastValidMasterFromSentinelAt = DateTime.UtcNow;

                                log.Error("Valid master was not found at '{0}' within '{1}'. Sending SENTINEL failover...".Fmt(
                                              client.GetHostString(), sentinel.WaitBeforeForcingMasterFailover), ex);

                                Interlocked.Increment(ref RedisState.TotalForcedMasterFailovers);

                                sentinel.ForceMasterFailover();
                                Thread.Sleep(sentinel.WaitBetweenSentinelLookups);
                                role = client.GetServerRole();
                            }
                        }
                    }
                    else
                    {
                        lastInvalidMasterHost = client.GetHostString();
                    }
                }

                if (role != RedisServerRole.Master)
                {
                    try
                    {
                        var stopwatch = Stopwatch.StartNew();
                        while (true)
                        {
                            try
                            {
                                var masterConfig = sentinel.GetMaster();
                                var masterClient = ClientFactory(masterConfig);
                                masterClient.ConnectTimeout = sentinel.SentinelWorkerConnectTimeoutMs;

                                var masterRole = masterClient.GetServerRole();
                                if (masterRole == RedisServerRole.Master)
                                {
                                    lastValidMasterFromSentinelAt = DateTime.UtcNow;
                                    return(masterClient);
                                }
                                else
                                {
                                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);
                                }
                            }
                            catch { /* Ignore errors until MaxWait */ }

                            if (stopwatch.Elapsed > sentinel.MaxWaitBetweenSentinelLookups)
                            {
                                throw new TimeoutException("Max Wait Between Sentinel Lookups Elapsed: {0}"
                                                           .Fmt(sentinel.MaxWaitBetweenSentinelLookups.ToString()));
                            }

                            Thread.Sleep(sentinel.WaitBetweenSentinelLookups);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role), ex);

                        var         newMasters   = new List <RedisEndpoint>();
                        var         newSlaves    = new List <RedisEndpoint>();
                        RedisClient masterClient = null;
                        foreach (var hostConfig in allHosts)
                        {
                            try
                            {
                                var testClient = ClientFactory(hostConfig);
                                testClient.ConnectTimeout = RedisConfig.HostLookupTimeoutMs;
                                var testRole = testClient.GetServerRole();
                                switch (testRole)
                                {
                                case RedisServerRole.Master:
                                    newMasters.Add(hostConfig);
                                    if (masterClient == null)
                                    {
                                        masterClient = testClient;
                                    }
                                    break;

                                case RedisServerRole.Slave:
                                    newSlaves.Add(hostConfig);
                                    break;
                                }
                            }
                            catch { /* skip past invalid master connections */ }
                        }

                        if (masterClient == null)
                        {
                            Interlocked.Increment(ref RedisState.TotalNoMastersFound);
                            var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString()));
                            log.Error(errorMsg);
                            throw new Exception(errorMsg);
                        }

                        ResetMasters(newMasters);
                        ResetSlaves(newSlaves);
                        return(masterClient);
                    }
                }
            }

            return(client);
        }
コード例 #38
0
 public RedisClient CreateRedisClient(RedisEndpoint config, bool master)
 {
     return ClientFactory(config);
 }
コード例 #39
0
 public RedisClient(RedisEndpoint config)
     : base(config)
 {
     Init();
 }
コード例 #40
0
        private RedisClient InitNewClient(RedisEndpoint nextHost, bool readWrite)
        {
            var client = ClientFactory != null
                ? ClientFactory(nextHost)
                : RedisResolver.CreateRedisClient(nextHost, readWrite:true);

            client.Id = Interlocked.Increment(ref RedisClientCounter);
            client.ClientManager = this;
            client.ConnectionFilter = ConnectionFilter;

            return client;
        }