C # Redis Series (iii)-redis Publishing subscriptions and client programming

Source: Internet
Author: User
Tags redis server

    1. Publish subscription model
    2. Publish subscriptions in Redis
    3. Client Programming examples
    4. 0.3 version Hredis
Publish subscription model

At the application level its role is to reduce dependencies, often also called observer patterns. The main point is to separate the coupling points out as a third party, isolating the easily changing sender and receiver.

Sender: Only responsible for sending messages to third parties. (the magazine gives the reader magazine to the Post Office)
Receiver: passively receives the message. (1: Subscribe to the Post Office Reader magazine, 2: The door to the magazine to receive mail)
Third-party functions: Store subscribers to the magazine and send it to the receiver when the magazine comes in. Post Office

C # example, the sender puts the magazine inside the Post office:

    if (QA. Addbug ())            emailnotify ();
The receiving party to the Post Office to register the address, has the magazine to come home delivery:
    Emailnotify + = () = {Console.WriteLine ("a June ");}    ; + = () = {Console.WriteLine ("b June ");};

The third-party post office receives the reader magazine subscription and dispatches it when it receives the magazine:

     Public Delegate void MessageHandler ();      Public Static Event MessageHandler  emailnotify;         if (QA. Addbug ())            emailnotify ();

When we zoom in on the observer pattern to the system level, it is the Publish subscription (Pub/sub). It is mainly used to reduce the coupling between publishers and subscribers and improve the throughput of the front-end system. Structure

Publish subscriptions in Redis

Redis implements a full publish-subscribe paradigm, which means that any Redis server can be used as a publishing subscriber after startup.

General Subscription

Start subscriber Client.

127.0. 0. 16379

Subscribe to the Bar channel. Format: SUBSCRIBE name1 name2.
A successful subscription response, corresponding to the subscription type, subscription channel, number of subscriptions.

127.0. 0.1:6379> SUBSCRIBE barreading messages ... (Press Ctrl-C to quit)1"subscribe"2"  Bar"31

A new publisher client, sending a message. Format: Publish ChannelName Message.

127.0. 0.1:6379>1

Subscribe to the client reply, corresponding to the message type, channel, message.

1 " message " 2 " Bar " 3 " Val "

Legend

Mode subscription

Redis supports pattern matching subscriptions, * for fuzzy matches.
Subscribe to all channel messages

Psubscribe *  

Subscribe to all channels that start with news.

Psubscribe news.*
Cancel Subscription

The command to cancel the normal and cancel mode subscriptions.

unsubscribe  barpunsubscribe  ba*

Cancellation cannot be simulated in the official connection tool.

View subscription information

Viewing subscription messages is one of the commands that Redis adds to the 2.8 center.

PubSub channels [pattern].

Returns all the channels that the current server is subscribed to.

127.0. 0.1:6379> pubsub channels1"bar"

Specify the matching parameters to return all channels that match the pattern.

127.0. 0.1:6379> PubSub channels ba*1"bar"
PubSub numsub [channel-1 channel-2 ... channel-n]

Accept as many channels as input parameters to return the number of subscribers to these channels.

127.0. 0.1:6379> pubsub numsub  bar bar21"bar"2  13"bar2"40
Client Programming examples
            New Redispubsub ("127.0.0.1"6381);             + = (obj) = {                Console.WriteLine ();            }            ; = (sender, Arcgs) ={                Console.WriteLine (ARCGS);            };             = (Exception) = {                 Console.WriteLine (exception.message);            };            Client. Subscribe ("bar");            Console.ReadLine ();
0.3 version Hredis Basic use
    using New Redisclient ("127.0.0.1"6381))        {            client. Set ("key""value");            Client. Get ("key");        }
Use connection pooling to automatically reclaim connections.
        New Poolredisclient (new  poolconfiguration ());        Prc. Single.set ("key""value");        Prc. Single.get ("key");
and the above subscription.

Open Source Address Https://github.com/mushroomsir/HRedis

C # Redis Series (iii)-redis Publishing subscriptions and client programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.