.Net Core WebApi Redis消息订阅与发布

帮助类

 public class RedisHelper : IDisposable
    {
        //连接字符串
        private string _connectionString;
        //实例名称
        private string _instanceName;
        //默认数据库
        public  int _defaultDB;
        private ConcurrentDictionary<string, ConnectionMultiplexer> _connections;
        public RedisHelper(string connectionString, string instanceName, int defaultDB = 0)
        {
            _connectionString = connectionString;
            _instanceName = instanceName;
            _defaultDB = defaultDB;
            _connections = new ConcurrentDictionary<string, ConnectionMultiplexer>();
        }
        public RedisHelper()
        {

        }
        /// <summary>
        /// 获取ConnectionMultiplexer
        /// </summary>
        /// <returns></returns>
        protected ConnectionMultiplexer GetConnect()
        {
            return _connections.GetOrAdd(_instanceName, p => ConnectionMultiplexer.Connect(_connectionString));
        }
 /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="channel">频道</param>
        /// <param name="handle">事件</param>
        public void Subscribe(RedisChannel channel, Action<RedisChannel, RedisValue> handle)
        {
            //getSubscriber() 获取到指定服务器的发布者订阅者的连接
            var sub = GetConnect().GetSubscriber();
            //订阅执行某些操作时改变了 优先/主动 节点广播
            sub.Subscribe(channel, handle);
        }
        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public long Publish(RedisChannel channel, RedisValue message)
        {
            var sub = GetConnect().GetSubscriber();
            return sub.Publish(channel, message);
        }
/// <summary>
        /// 发布(使用序列化)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="channel"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public long Publish<T>(RedisChannel channel, T message)
        {
            var sub = GetConnect().GetSubscriber();
            return sub.Publish(channel, JsonConvert.SerializeObject(message));
        }

        /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="redisChannel"></param>
        /// <param name="handle"></param>
        /// <returns></returns>
        public async Task SubscribeAsync(RedisChannel redisChannel, Action<RedisChannel, RedisValue> handle)
        {
            var sub = GetConnect().GetSubscriber();
            await sub.SubscribeAsync(redisChannel, handle);
        }
        /// <summary>
        /// 取消订阅
        /// </summary>
        /// <param name="redisChannel"></param>
        /// <param name="handle"></param>
        /// <returns></returns>
        public async Task UnsubscribeAsync(RedisChannel redisChannel) 
        {
            var sub = GetConnect().GetSubscriber();
            await sub.UnsubscribeAsync(redisChannel);
        }
        /// <summary>
        /// 取消订阅
        /// </summary>
        /// <param name="redisChannel"></param>
        /// <param name="handle"></param>
        /// <returns></returns>
        public void Unsubscribe(RedisChannel redisChannel)
        {
            var sub = GetConnect().GetSubscriber();
            sub.Unsubscribe(redisChannel);
        }
}

消息发布

        消息发布比较简单,直接 _helper.Publish(“channel_name”, "TestData"); 即可实现。

消息订阅

        webApi方法中,不能直接订阅消息,所以,我们需要借助IHostedService来实现。

详见另一篇帖子:.Net Core WebApi Redis消息订阅

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值