StackExchange.Redis 当我收到委托时没有检索到匹配的模式

StackExchange.Redis not retrieving the matched pattern when I receive the delegate

我正在实现由 Redis 的 Pub/Sub 提供的 signalR。 为了与 Redis 交互,我使用了 StackExchange.Redis-1.2.6。

这里的问题是,当我在 signalR 集线器上订阅模式时,我创建了一个包含我感兴趣的 ConnectionId 和主题的组,并在 Redis Pub/Sub 上执行相同的操作。

当我收到消息时,我需要回溯并通知所有感兴趣的订阅者,但问题是 Redis 没有给我匹配的模式,而是给我发布的主题。

这是代码示例:

        ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
        ISubscriber sub = redis.GetSubscriber();

        RedisChannel channelWithLiteral = new RedisChannel("messages", RedisChannel.PatternMode.Literal);
        sub.Subscribe(channelWithLiteral, (channel, message) => 
        {
            Console.WriteLine($"Literal -> channel: '{channel}' message: '{message}'");
        });

        RedisChannel channelWithPattern = new RedisChannel("mess*", RedisChannel.PatternMode.Pattern);
        sub.Subscribe(channelWithPattern, (channel, message) => 
        {
            Console.WriteLine($"Pattern -> channel: '{channel}' message: '{message}'");
        });

        sub.Publish("messages", "hello");
        Console.ReadLine();

输出为:

Literal -> channel: 'messages' message: 'hello'

Pattern -> channel: 'messages' message: 'hello'

我是什么expecting/needed:

Literal -> channel: 'messages' message: 'hello'

Pattern -> channel: 'mess*' message: 'hello'

https://redis.io/topics/pubsub 上它说当使用 PSUBSCRIBE 时我们会收到通知:原始主题和匹配的模式。

示例如下:

StackExchange.Redis 上有什么方法可以接收匹配的模式吗?

我在 GitHub 上开了一个问题:RedisChannel is not retrieving the matched pattern when I receive the delegate

你可以暂时看到一个变通的解决方案。

"现在,唯一的方法是在订阅时"capture"订阅频道。"

解决方法如下:

  1. 在创建 Action 委托之前,声明一个临时变量以 将模式存储在临时的委托操作引用上 变量

  2. 每次有人尝试订阅时都会创建一个新的实例 该变量并与 Redis 一起传递该信息 频道

这是代码示例:

public class MyTest
    {
        private readonly ISubscriber subscriber; 

        public MyTest()
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
            this.subscriber = redis.GetSubscriber();
        }

        public void SubscribeWithPattern(string pattern)
        {
            RedisChannel channelWithPattern = new RedisChannel(pattern, RedisChannel.PatternMode.Pattern);
            string originalChannelSubcription = pattern;
            this.subscriber.Subscribe(channelWithPattern, (channel, message) =>
            {
                Console.WriteLine($"OriginalChannelSubcription '{originalChannelSubcription}");
                Console.WriteLine($"Channel: '{channel}");
                Console.WriteLine($"Message: '{message}'");
            });
        }

        public void Publish(string channel, string message)
        {
            this.subscriber.Publish(channel, message);
        }
    }