首页 > 数据库 >redis订阅和键空间通知

redis订阅和键空间通知

时间:2023-01-15 15:22:16浏览次数:62  
标签:__ 订阅 keyevent 和键 redis 111 events

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"

可以根据需求选择准确的或者匹配的订阅模式。

参考文章:

  1. Redis源码学习(40)-Redis发布与订阅
  2. Redis源码学习(41)-Redis键空间事件通知

标签:__,订阅,keyevent,和键,redis,111,events
From: https://www.cnblogs.com/datangguanjunhou/p/17053557.html

相关文章

  • java:Redis持久化
    一.redis持久化的介绍Redis的持久化指的是将内存中redis数据库运行的数据,写到硬盘文件上。Redis持久化的意义主要在于故障恢复,比如你部署一个Redis,作为缓存有可能里边有......
  • Redisson的看门狗机制
    背景据Redisson官网的介绍,Redisson是一个JavaRedis客户端,与Spring提供给我们的RedisTemplate工具没有本质的区别,可以把它看做是一个功能更强大的客户端(虽然官网上声称Re......
  • .NetCore下基于FreeRedis实现的Redis6.0客户端缓存之缓存键条件优雅过滤
    前言众所周知内存缓存(MemoryCache)数据是从内存中获取,性能表现上是最优的,但是内存缓存有一个缺点就是不支持分布式,数据在各个部署节点上各存一份,每份缓存的过期时间不一......
  • Shiro+SpringBoot+Mybatis+Redis实现权限系统
     这个项目涉及到的技术:SpringBoot,Thymeleaf,MyBaits,Redis,Shiro,JavaMail,EasyUI,Excel导入、导出 下面展示一下界面: 主页:  用户列表:  角色权限授予: 资源列表:  用户角......
  • 12. Redis 之 AOF持久化方式
    Redis之AOF持久化方式AOF简介Redis的持久化方式之一RDB是通过保存数据库的键值对数据来记录数据库的状态。而另一种持久化策略AOF就是通过保存Redis服务器所执行的命令来......
  • 4. redis数据类型之String-bit
    Redis字符串string-bit前面我们介绍了Redis中的字符串类型的基本命令操作。但是没有涉及到bit相关的命令。本文我们来看几个bit相关的命令。bit相关的命令指的是bitcoun......
  • Redis 高级篇 Part 2
    ......
  • Redis 高级篇 Part 4
    ......
  • Redis 高级篇 Part 3
    ......
  • Redis 高级篇 Part 2
    Redis高级篇Part2......