/// <summary> /// 加入订阅 /// </summary> /// <param name="clientId">客户端id</param> /// <param name="channel">订阅名称</param> public void SubscribeChannel(Guid clientId, string channel) { string channelKey = RedisKeyFormatUtil.GetSubscribeChannelKey(_appId, channel); string ClientChannelKey = RedisKeyFormatUtil.GetClientChannelKey(_appId, clientId); _redis.StartPipe(a => a .HSet(channelKey, clientId.ToString(), 0) .HSet(ClientChannelKey, channel, 0) .HIncrBy(this.ChannelListKey, channel, 1)); }
/// <summary> /// 取消订阅 /// </summary> /// <param name="clientId">客户端id</param> /// <param name="channels">订阅名称</param> public void UnSubscribeChannel(Guid clientId, params string[] channels) { if (channels?.Any() != true) { return; } using (var pipe = _redis.StartPipe()) { string ClientChannelKey = RedisKeyFormatUtil.GetClientChannelKey(_appId, clientId); foreach (var channel in channels) { string channelKey = RedisKeyFormatUtil.GetSubscribeChannelKey(_appId, channel); pipe.HDel(channelKey, clientId.ToString()) .HDel(ClientChannelKey, channel) .Eval($"if redis.call('HINCRBY', KEYS[1], '{channel}', '-1') <= 0 then redis.call('HDEL', KEYS[1], '{channel}') end return 1", this.ChannelListKey); } } }
/// <summary> /// 获取用户参与的所有订阅 /// </summary> /// <param name="clientId">客户端id</param> /// <returns></returns> public string[] GetChanListByClientId(Guid clientId) { string ClientChannelKey = RedisKeyFormatUtil.GetClientChannelKey(_appId, clientId); return(_redis.HKeys(ClientChannelKey)); }