public void AddServer( ) { var configuration = ConfigurationSettings.GetRedisConfigurationSection( ); Assert.AreEqual(1, configuration.Servers.Count); configuration.Servers.Add(new RedisServer("abc", 123)); ConfigurationSettings.UpdateRedisConfigurationSection(configuration); Assert.AreEqual(2, configuration.Servers.Count); RedisServer server = configuration.Servers[0]; Assert.IsNotNull(server); Assert.AreEqual("localhost", server.HostName); Assert.AreEqual(6379, server.Port); server = configuration.Servers[1]; Assert.IsNotNull(server); Assert.AreEqual("abc", server.HostName); Assert.AreEqual(123, server.Port); configuration.Servers.Remove("abc"); ConfigurationSettings.UpdateRedisConfigurationSection(configuration); Assert.AreEqual(1, configuration.Servers.Count); }
public void GetServers( ) { var configuration = ConfigurationSettings.GetRedisConfigurationSection( ); Assert.AreEqual(1, configuration.Servers.Count); RedisServer server = configuration.Servers[0]; Assert.IsNotNull(server); Assert.AreEqual("localhost", server.HostName); Assert.AreEqual(6379, server.Port); }
public void RemoveServer( ) { var configuration = ConfigurationSettings.GetRedisConfigurationSection( ); Assert.AreEqual(1, configuration.Servers.Count); RedisServer server = configuration.Servers[0]; configuration.Servers.RemoveAt(0); ConfigurationSettings.UpdateRedisConfigurationSection(configuration); Assert.AreEqual(0, configuration.Servers.Count); configuration.Servers.Add(server); ConfigurationSettings.UpdateRedisConfigurationSection(configuration); Assert.AreEqual(1, configuration.Servers.Count); }
/// <summary> /// Connects to a redis server. /// </summary> public void Connect(bool allowAdmin = false) { lock ( _syncRoot ) { if (IsConnected) { return; } RedisConfiguration configuration = ConfigurationSettings.GetRedisConfigurationSection( ); if (configuration == null) { throw new RedisConnectionException("Invalid Redis configuration."); } if (configuration.Servers == null || configuration.Servers.Count <= 0) { throw new RedisConnectionException("No Redis servers found."); } ///// // Randomly pick a redis server from the pool. // (Load balance this at a later stage). ///// var random = new Random(Environment.TickCount); int serverId = random.Next(0, configuration.Servers.Count - 1); RedisServer redisServer = configuration.Servers[serverId]; if (redisServer == null || string.IsNullOrEmpty(redisServer.HostName)) { throw new RedisConnectionException("Invalid Redis server configuration."); } string host = string.Format("{0}:{1}", redisServer.HostName, redisServer.Port); ConfigurationOptions configurationOptions = ConfigurationOptions.Parse(host); configurationOptions.ClientName = GetClientName( ); configurationOptions.AbortOnConnectFail = false; configurationOptions.AllowAdmin = allowAdmin; EventLog.Application.WriteInformation("Connecting to Redis server ('{0}')...", host); try { ConnectionMultiplexer multiplexer = ConnectionMultiplexer.Connect(configurationOptions); ///// // Allow concurrent message processing. ///// multiplexer.PreserveAsyncOrder = false; multiplexer.ConnectionFailed += ConnectionFailed; multiplexer.ConnectionRestored += ConnectionRestored; EventLog.Application.WriteInformation(multiplexer.IsConnected ? "Connection to Redis server ('{0}') established." : "Connection to Redis server ('{0}') failed. Connection will be automatically reestablished when possible.", host); IServer server = multiplexer.GetServer(host); if (server != null) { PrepareScripts(server); } ActiveConnection = multiplexer; } catch (StackExchange.Redis.RedisConnectionException exc) { EventLog.Application.WriteWarning("Connection to Redis server '{0}' failed.\n{1}", host, exc); } } }