简体   繁体   English

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

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

I'm implementing signalR that is feed by the Pub/Sub of Redis. 我正在实现由Redis的Pub / Sub提供的signalR。 To interact with Redis I've used StackExchange.Redis-1.2.6. 为了与Redis进行交互,我使用了StackExchange.Redis-1.2.6。

The issue here is that when I subscribe a pattern on the signalR hub I create a group with ConnectionId and topic that I'm interested and do the same on Redis Pub/Sub. 这里的问题是,当我在signalR集线器上订阅模式时,我会创建一个具有ConnectionId和我感兴趣的主题的组,并在Redis Pub / Sub上进行相同的操作。

When I receive the message I need to trace back and notify all the interested subscribers, but the problem is that Redis is not giving me the matched pattern but the published topic instead. 当我收到消息时,我需要追溯并通知所有感兴趣的订户,但是问题是Redis不是给我匹配的模式,而是给我发布的主题。

Here is the code sample: 这是代码示例:

        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();

The output is: 输出为:

Literal -> channel: 'messages' message: 'hello' 文字->频道:“ messages”消息:“ hello”

Pattern -> channel: 'messages' message: 'hello' 模式->频道:“ messages”消息:“ hello”

What I was expecting/needed: 我的期望/需求:

Literal -> channel: 'messages' message: 'hello' 文字->频道:“ messages”消息:“ hello”

Pattern -> channel: 'mess*' message: 'hello' 模式->频道:“ mess *”消息:“ hello”

On https://redis.io/topics/pubsub it says when using PSUBSCRIBE we are notified of both: original topic, and matched pattern. https://redis.io/topics/pubsub上说,使用PSUBSCRIBE时,我们会同时收到以下信息:原始主题和匹配的模式。

Here is the example: 这是示例:

在此处输入图片说明

Is there any way on StackExchange.Redis to receive the matched pattern? StackExchange.Redis是否有任何方法可以接收匹配的模式?

I've opened an issue at GitHub: RedisChannel is not retrieving the matched pattern when I receive the delegate : 我在GitHub上发布了一个问题: 收到委托时,RedisChannel没有检索匹配的模式

You can see there a workaround solution for the time being. 您可以暂时看到一种解决方法。

" Right now, the only way to do this would be to "capture" the subscription channel when subscribing. " 目前,唯一的方法就是在订阅时“捕获”订阅频道。

Here is the workaround approach: 这是解决方法:

  1. Before creating the Action delegate, declare a temporary variable to store the pattern On the delegate Action reference that temporary variable 在创建Action委托之前,声明一个临时变量以存储模式。在委托Action上引用该临时变量

  2. Each time someone tries to subscribe will create a new instance of that variable and deliver that info together with Redis channel 每当有人尝试订阅时,都会创建该变量的新实例,并将该信息与Redis频道一起传递

Here is the code example: 这是代码示例:

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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM