十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
新建项目后先引用库ServiceStack.redis
公司主营业务:网站设计、网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出罗湖免费做网站回馈大家。
1. 参数配置
/* 连接字符串格式 * localhost * 127.0.0.1:6379 * redis://localhost:6379 * password@localhost:6379 * clientid:password@localhost:6379 * redis://clientid:password@localhost:6380?ssl=true&db=1 */ var redis_servre = "192.168.1.104:6379"; var redis_channel = "test@#"; var channel_exit = "exit";
2. 建立redis连接池
var rwHosts = new string[] { redis_servre }; var rHosts = new string[] { }; var redisPool = new PooledRedisClientManager(rwHosts, rHosts, new RedisClientManagerConfig() { MaxWritePoolSize = 5, MaxReadPoolSize = 5, AutoStart = true });
3. 订阅channels
var t_sub = new Thread(() => { //using (var redisClient = new RedisClient("192.168.1.104", 6379)) using (var redisClient = redisPool.GetClient()) { using (var sub = redisClient.CreateSubscription()) { sub.OnMessage = (channel, msg) => { Console.WriteLine("recv {0} from {1}", msg, channel); if (msg == channel_exit) { sub.UnSubscribeFromAllChannels(); } }; //会阻塞线程 sub.SubscribeToChannels(redis_channel); } } Console.WriteLine("sub exit"); }); t_sub.Start();
4. 向channel发布消息
using (var redisClient = redisPool.GetClient()) { redisClient.PublishMessage(redis_channel, "hello"); Console.ReadKey(); redisClient.PublishMessage(redis_channel, channel_exit); }
5. 运行程序查看效果,可以通过redis-cli程序发送消息试试看
>publish test@# hello >publish test@# exit