[Transfer] Redis publish and subscribe

Pub / Sub in Redis command

Redis implements the event push publish and subscribe command in versions after 2.0

The following are the relevant commands provided by Redis regarding publish and subscribe

SUBSCRIBE channel [channel ...]

Subscribe to the information of a given channel or channels.

PSUBSCRIBE pattern [pattern ...]

Subscribe to one or more channels that match a given pattern .

PUBLISH channel message

The information  message  sent to the designated channel  Channel  .

PUBSUB <subcommand> [argument [argument ...]]

View all active channels

PUNSUBSCRIBE [pattern [pattern ...]]

The client unsubscribes from all subscribed channels of a given mode

UNSUBSCRIBE [channel [channel ...]]

Instruct the client to unsubscribe from the given channel.

 

The channels here can be divided into two categories, one is the channel with the specified name, and the other is the fuzzy matching channel (pattern). The commands required to subscribe and unsubscribe the two types of channels are different

The following demonstrates the basic usage of these commands

The client on the left in the picture above subscribes to the msg channel. Another client publishes a message to the msg channel, and the subscribed client successfully receives the message.

We use the pubsub command to query all active channels. There is currently an active channel msg

The client on the left here subscribes to a mode channel that matches the given mode news. *, And another client sends a message to news.it and receives it successfully.

It is empty when calling pusub to view the active channel, because the subscription of the mode channel is not counted in the active channel

After subscribing under the dos command, the interface is stuck waiting for messages, so there is no way to demonstrate the command to cancel the subscription. No more pictures

 

Publish and subscribe in StackExchange.Redis

To use StackExchange.Redis to publish and subscribe, you first need to obtain a Subscriber through connection . This class implements the ISubscriber interface, which defines the corresponding Subscribe, Publish and other related functions.

Usage is relatively simple, directly on the code

Copy code
 public class RedisSubPub
    {
        public void SubScribe(string cnl)
        {
            Console.WriteLine("主线程:" + Thread.CurrentThread.ManagedThreadId);
            var sub = StackExchangeRedisHelper.Instance.GetSubscriber();
            sub.Subscribe(cnl, SubHandel);
            Console.WriteLine("订阅了一个频道:"+ cnl);
        }
        public void SubHandel(RedisChannel cnl, RedisValue val)
        {
            Console.WriteLine();
            Console.WriteLine("Channel: " + cnl + " \ tReceived message: " + val);; 
            Console.WriteLine ( " Thread: " + Thread.CurrentThread.ManagedThreadId + " , whether the thread pool: " + Thread.CurrentThread.IsThreadPoolThread);
             if (val == " close " ) 
                StackExchangeRedisHelper.Instance.GetSubscriber (). Unsubscribe (cnl); 
            if (val == " closeall " ) 
                StackExchangeRedisHelper.Instance.GetSubscriber().UnsubscribeAll();
        } 
    }
Copy code

 

Copy code
 static void Main(string[] args)
        {
            RedisSubPub r = new RedisSubPub();
            r.SubScribe("it");
            r.SubScribe("news");
            r.SubScribe("sport");
            r.SubScribe("others*");
            Console.Read();
        }
Copy code

 

I used the code in the program as a subscriber, subscribed to four channels, and then sent a message to the response channel in the Dos command line window

After the program runs, StackExchange.Redis subscribes to a __Booksleeve_MasterChanged channel by default . This subscription is mainly triggered when the master-slave configuration of the Redis cluster changes, we ignore it

On the client, I subscribed to three channels, and one others * mode channel. 3 channels were queried through the pubsub command

Subscribers call Unsubscribe to unsubscribe from specific channels, and UnsubscribeAll to unsubscribe from all channels

Redis does not provide a command to unsubscribe all channels. StackExchange.Redis adds each subscribed channel to a dictionary, and then when you cancel, it will query the name of the channel to be canceled from the dictionary.

StackExchange.Redis source code snippet is as follows

This code can also be seen as the premise to keep the subscription channel working correctly is to maintain a static connection instance!

 

Pub / Sub in Redis command

Redis implements the event push publish and subscribe command in versions after 2.0

The following are the relevant commands provided by Redis regarding publish and subscribe

SUBSCRIBE channel [channel ...]

Subscribe to the information of a given channel or channels.

PSUBSCRIBE pattern [pattern ...]

Subscribe to one or more channels that match a given pattern .

PUBLISH channel message

The information  message  sent to the designated channel  Channel  .

PUBSUB <subcommand> [argument [argument ...]]

View all active channels

PUNSUBSCRIBE [pattern [pattern ...]]

The client unsubscribes from all subscribed channels of a given mode

UNSUBSCRIBE [channel [channel ...]]

Instruct the client to unsubscribe from the given channel.

 

The channels here can be divided into two categories, one is the channel with the specified name, and the other is the fuzzy matching channel (pattern). The commands required to subscribe and unsubscribe the two types of channels are different

The following demonstrates the basic usage of these commands

The client on the left in the picture above subscribes to the msg channel. Another client publishes a message to the msg channel, and the subscribed client successfully receives the message.

We use the pubsub command to query all active channels. There is currently an active channel msg

The client on the left here subscribes to a mode channel that matches the given mode news. *, And another client sends a message to news.it and receives it successfully.

It is empty when calling pusub to view the active channel, because the subscription of the mode channel is not counted in the active channel

After subscribing under the dos command, the interface is stuck waiting for messages, so there is no way to demonstrate the command to cancel the subscription. No more pictures

 

Publish and subscribe in StackExchange.Redis

To use StackExchange.Redis to publish and subscribe, you first need to obtain a Subscriber through connection . This class implements the ISubscriber interface, which defines the corresponding Subscribe, Publish and other related functions.

Usage is relatively simple, directly on the code

Copy code
 public class RedisSubPub
    {
        public void SubScribe(string cnl)
        {
            Console.WriteLine("主线程:" + Thread.CurrentThread.ManagedThreadId);
            var sub = StackExchangeRedisHelper.Instance.GetSubscriber();
            sub.Subscribe(cnl, SubHandel);
            Console.WriteLine("订阅了一个频道:"+ cnl);
        }
        public void SubHandel(RedisChannel cnl, RedisValue val)
        {
            Console.WriteLine();
            Console.WriteLine("频道:" + cnl + "\t收到消息:" + val); ;
            Console.WriteLine("线程:" + Thread.CurrentThread.ManagedThreadId + ",是否线程池:" + Thread.CurrentThread.IsThreadPoolThread);
            if (val == "close")
                StackExchangeRedisHelper.Instance.GetSubscriber().Unsubscribe(cnl);
            if (val == "closeall")
                StackExchangeRedisHelper.Instance.GetSubscriber().UnsubscribeAll();
        }
    }
Copy code

 

Copy code
 static void Main(string[] args)
        {
            RedisSubPub r = new RedisSubPub();
            r.SubScribe("it");
            r.SubScribe("news");
            r.SubScribe("sport");
            r.SubScribe("others*");
            Console.Read();
        }
Copy code

 

I used the code in the program as a subscriber, subscribed to four channels, and then sent a message to the response channel in the Dos command line window

After the program runs, StackExchange.Redis subscribes to a __Booksleeve_MasterChanged channel by default . This subscription is mainly triggered when the master-slave configuration of the Redis cluster changes, we ignore it

On the client, I subscribed to three channels, and one others * mode channel. 3 channels were queried through the pubsub command

Subscribers call Unsubscribe to unsubscribe from specific channels, and UnsubscribeAll to unsubscribe from all channels

Redis does not provide a command to unsubscribe all channels. StackExchange.Redis adds each subscribed channel to a dictionary, and then when you cancel, it will query the name of the channel to be canceled from the dictionary.

StackExchange.Redis source code snippet is as follows

This code can also be seen as the premise to keep the subscription channel working correctly is to maintain a static connection instance!

 

Guess you like

Origin www.cnblogs.com/hurui1/p/12729221.html
Recommended