1. 程式人生 > >redis實現消息隊列

redis實現消息隊列

redis集群 端口 new etc 管理 deque try list github

        //版本2:使用Redis的客戶端管理器(對象池)
        public static IRedisClientsManager redisClientManager = new PooledRedisClientManager(
        new[]
        {
            // 讀寫鏈接

            //如果是Redis集群則配置多個{IP地址:端口號}即可
            "127.0.0.1:6379","10.0.0.1:6379","10.0.0.2:6379","10.0.0.3:6379"
        }, new[]
        {
            
// 只讀鏈接 //如果是Redis集群則配置多個{IP地址:端口號}即可 "127.0.0.1:6379","10.0.0.1:6379","10.0.0.2:6379","10.0.0.3:6379" }, 10); //從池中獲取Redis客戶端實例 public static IRedisClient redisClient = redisClientManager.GetClient(); static void Main(string[] args) {
#region 客戶端添加消息 redisClient.EnqueueItemOnList("Log", "1111"); redisClient.EnqueueItemOnList("Log", "222"); redisClient.EnqueueItemOnList("Log", "333"); #endregion #region 服務器端掃碼消息 ThreadPool.QueueUserWorkItem(o => {
while (true) { try { if (redisClient.GetListCount("Log") > 0) { string log = redisClient.DequeueItemFromList("Log"); if (!string.IsNullOrEmpty(log)) { Console.WriteLine(log); } } else { Thread.Sleep(1000); //為避免CPU空轉,在隊列為空時休息1秒 } } catch (Exception ex) { redisClient.EnqueueItemOnList("Log", ex.ToString()); } } }); #endregion Console.ReadLine(); }

github地址:https://github.com/842549829/RedisMessage

redis實現消息隊列