• redis实现消息队列


            //版本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

  • 相关阅读:
    八大算法手写
    Hql总结
    设计模式
    数据库连接失败(1)
    什么是ORM
    C++标准库之右值引用与交付语义
    C++标准库第二版笔记 2
    C++标准库第二版笔记 1
    Effective C++ 笔记:条款 32 确定你的public继承塑造出正确的is-a关系
    Effective C++ 笔记:条款 31 将编译关系降至最低
  • 原文地址:https://www.cnblogs.com/liuxiaoji/p/9288102.html
Copyright © 2020-2023  润新知