您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
新建项目后先引用库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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。