1.订阅发布
这个功能和消息队列MQ的订阅发布几乎一样,都是订阅频道,然后向频道推送数据,订阅者就会收到推送的数据。
- 订阅频道,[channel ...]意思是可以同时订阅多个频道,空格分隔开就行
SUBSCRIBE channel [channel ...]
客户端订阅频道之后不能做别的操作了,推送消息需要再打开一个客户端。
127.0.0.1:6379> subscribe 111
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "111"
3) (integer) 1
- 向频道发送数据(消息)
PUBLISH channel message
我们向111
发送一个haod
127.0.0.1:6379> publish 111 nihao
(integer) 1
看一下订阅者的界面
127.0.0.1:6379> subscribe 111
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "111"
3) (integer) 1
1) "message"
2) "111"
3) "nihao"
ps:redis命令行工具是redis-cli
,去安装目录下找。
2.键空间通知
我们可以简单地将redis的通知理解为是redis的key发生了某些变化,然后redis将消息推送到了某些特定预设的频道上,客户端只要订阅了这些频道便可以获取这些消息。
redis的订阅发布虽然简单,但是有成熟的消息队列产品珠玉在前,一般不会使用redis做订阅发布。但是redis的键空间通知配合订阅发布却非常契合某些业务场景。我们会利用redis的key可以设置过期时间的特性来实现某些需求,当key过期的时候推送消息,然后执行后续操作。那么首先需要在配置文件
中修改配置打开通知(默认是关闭的)。
############################# EVENT NOTIFICATION ##############################
# Redis can notify Pub/Sub clients about events happening in the key space.
# This feature is documented at http://redis.io/topics/notifications
#
# For instance if keyspace events notification is enabled, and a client
# performs a DEL operation on key "foo" stored in the Database 0, two
# messages will be published via Pub/Sub:
#
# PUBLISH __keyspace@0__:foo del
# PUBLISH __keyevent@0__:del foo
#
# It is possible to select the events that Redis will notify among a set
# of classes. Every class is identified by a single character:
#
# K Keyspace events, published with __keyspace@<db>__ prefix.
# E Keyevent events, published with __keyevent@<db>__ prefix.
# g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
# $ String commands
# l List commands
# s Set commands
# h Hash commands
# z Sorted set commands
# x Expired events (events generated every time a key expires)
# e Evicted events (events generated when a key is evicted for maxmemory)
# A Alias for g$lshzxe, so that the "AKE" string means all the events.
#
# The "notify-keyspace-events" takes as argument a string that is composed
# of zero or multiple characters. The empty string means that notifications
# are disabled.
#
# Example: to enable list and generic events, from the point of view of the
# event name, use:
#
# notify-keyspace-events Elg
#
# Example 2: to get the stream of the expired keys subscribing to channel
# name __keyevent@0__:expired use:
#
# notify-keyspace-events Ex
#
# By default all notifications are disabled because most users don't need
# this feature and the feature has some overhead. Note that if you don't
# specify at least one of K or E, no events will be delivered.
# 键空间过期事件提醒
notify-keyspace-events Ex
配置文件中的解释很清楚。
现在我们来进行下尝试,在这之前我们介绍下redis的另一种订阅模式,匹配订阅。
- 匹配订阅就是会根据正则表达式匹配频道进行订阅,同样支持多订阅
PSUBSCRIBE pattern [pattern ...]
- 首先不使用匹配订阅,只准确订阅
expire
127.0.0.1:6379> subscribe __keyevent@0__:expired
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "__keyevent@0__:expired"
3) (integer) 1
- 设置一个有过期时间的key
127.0.0.1:6379> setex 111 2 123
OK
如果对设置过期时间不熟悉,可以参考redis 设置过期时间
- 看看效果
127.0.0.1:6379> subscribe __keyevent@0__:expired
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "__keyevent@0__:expired"
3) (integer) 1
1) "message"
2) "__keyevent@0__:expired"
3) "111"
再试试匹配规则的
127.0.0.1:6379> psubscribe __keyevent@*__:*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "__keyevent@*__:*"
3) (integer) 1
1) "pmessage"
2) "__keyevent@*__:*"
3) "__keyevent@0__:expired"
4) "111"
可以根据需求选择准确的或者匹配的订阅模式。