开发者

In Laymen's terms, what is Redis Pub/Sub?

Why would I use it? G开发者_StackOverflowive some very basic examples.


A redis client subscribes to receive messages marked with a specific tag, termed channel. Other clients publish to this channel. Redis notifies each subscribing client each time a message is published by anyone to the channel.

You can also subscribe to a channel pattern - think regex matching.

This helps make code distributable. It allows bits of code to run in different processes, and potentially even different machines, and to communicate with each other via these queues.

This feature comes from repeated user requests. There is an example use-case given here:

a news-related site needs to update the cached copy of its home page every time that a new article is published.

The background cache worker process subscribes to all channels that begin with ‘new.article.’:

redis> PSUBSCRIBE new.article.*

The article publishing process creates a new technology article (in this example, this article has ID ‘1021’), adds the article’s ID to the set of all technology articles, and publishes the article’s ID to the ‘new.article.technology’ channel:

redis> MULTI
OK
redis> SET article.technology.1021 "In today's technology news, ..."
QUEUED
redis> SADD article.technology 1021
QUEUED
redis> PUBLISH new.article.technology 1021
QUEUED
redis> EXEC
1. OK
2. (integer) 1
3. (integer) 1

At this point, the background cache worker process will receive a message and know immediately that a new technology article was published, subsequently executing the appropriate callback to re-generate the home page.

http://redis.io/topics/pubsub

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜