Using the Publish subscription feature of Redis
Another common use of redis is the ability to publish subscriptions. It is very simple and connectionmultiplexer will automatically re-subscribe when the connection fails.
Isubscriber sub = Redis. Getsubscriber ();
The Getsubscriber method returns an instance of the Isubscriber type. The Publish subscription feature does not have a database concept, and we can provide a async-state for it. All subscriptions are global:
Isubscriber instances are not their lifecycle, and the features of the publish subscription are defined in Redis as "channels", and channels do not need to be pre-defined in the database. Subscription operation requires a channel
Name and a callback function to process the published message.
Sub. Subscribe ("Messages", (channel, message) + = {
Console.WriteLine ((string) message);
});
You can post a message to the specified channel:
Sub. Publish ("Messages", "Hello");
He will publish the message "Hello" to all clients subscribed to the messages channel (almost in real time). As before, the name and message of the channel can be binary as well.
Specify the order in which messages are published (Message order)
When using the Pub/sub API, you can specify whether the message is parallel or sequential.
Orderly means you don't have to think about thread safety, but it also means that messages are passed through the queue exactly in the order you publish them, which inevitably leads to a delay in the message.
Parallel processing, there is no guarantee that the message is processed in the order of publication, and your code must also ensure that the program runs normally when there is concurrency.
The order of messages is usually irrelevant, and parallel processing can achieve better performance and scalability.
To ensure security, message delivery is ordered by default. For better performance, it is strongly recommended that you use parallel operations. This is very simple.
Multiplexer. Preserveasyncorder = false;
The recommendation is not the reason you are configuring this option, and whether it is suitable depends entirely on the code that you subscribe to the message.
Stackexchange.redis Use-Publish Subscription (II)