private static void TestList() { using (var client = new RedisClient("127.0.0.1", 6379)) { #region "List类型" client.AddItemToList("HQF.Tutorial.Redis:userInfoId1", "123"); client.AddItemToList("HQF.Tutorial.Redis:userInfoId1", "1234"); Console.WriteLine("List数据项条数:" + client.GetListCount("HQF.Tutorial.Redis:userInfoId1")); Console.WriteLine("List数据项第一条数据:" + client.GetItemFromList("HQF.Tutorial.Redis:userInfoId1", 0)); Console.WriteLine("List所有数据"); client.GetAllItemsFromList("HQF.Tutorial.Redis:userInfoId1").ForEach(e => Console.WriteLine(e)); #endregion #region "List类型做为队列和栈使用" Console.WriteLine(client.GetListCount("HQF.Tutorial.Redis:userInfoId1")); //队列先进先出 //Console.WriteLine(client.DequeueItemFromList("userInfoId1")); //Console.WriteLine(client.DequeueItemFromList("userInfoId1")); //栈后进先出 Console.WriteLine("出栈" + client.PopItemFromList("HQF.Tutorial.Redis:userInfoId1")); Console.WriteLine("出栈" + client.PopItemFromList("HQF.Tutorial.Redis:userInfoId1")); #endregion } }
public void Can_failover_MqServer_at_runtime() { const int iterations = 100; var failoverHost = "redis-failover:6379"; var localClient = new RedisClient("localhost:6379"); localClient.FlushDb(); var failoverClient = new RedisClient(failoverHost); failoverClient.FlushDb(); var clientManager = new PooledRedisClientManager(new[] { "localhost" }); var mqHost = new RedisMqServer(clientManager); var map = new Dictionary<string, int>(); var received = 0; mqHost.RegisterHandler<Msg>(c => { var dto = c.GetBody(); received++; int count; map.TryGetValue(dto.Host, out count); map[dto.Host] = count + 1; lock (clientManager) { "Received #{0} from {1}".Print(received, dto.Host); if (received == iterations) Monitor.Pulse(clientManager); } return null; }); mqHost.Start(); RunMqInLoop(mqHost, iterations: iterations, callback: () => { lock (clientManager) "{0} msgs were published.".Print(iterations); }); Thread.Sleep(500); clientManager.FailoverTo(failoverHost); lock (clientManager) Monitor.Wait(clientManager); "localclient inq: {0}, outq: {1}".Print( localClient.GetListCount("mq:Msg.inq"), localClient.GetListCount("mq:Msg.outq")); "failoverClient inq: {0}, outq: {1}".Print( failoverClient.GetListCount("mq:Msg.inq"), failoverClient.GetListCount("mq:Msg.outq")); Assert.That(received, Is.EqualTo(100)); Assert.That(map.Count, Is.EqualTo(2)); var msgsFromAllHosts = 0; foreach (var count in map.Values) { Assert.That(count, Is.GreaterThan(0)); msgsFromAllHosts += count; } Assert.That(msgsFromAllHosts, Is.EqualTo(iterations)); }