• Redis 事件通知


    Reids 事件通知

    Redis从2.8.0版本后,推出Keyspace Notifications特性。

    此特性允许客户端以订阅/发布(Sub/Pub)模式,通过设置键空间通知来接收那些对数据库中的键和值有影响的操作事件。

    事件类型

    • keyspace:键空间通知
    • keyevent:键事件通知

    事件是用__keyspace@__:KeyPattern或者__keyevent@__:OpsType的格式来发布消息的。

    表示数据库编号;

    KeyPattern表示需要监控的键模式(可以用通配符);

    OpsType表示操作类型。

    例如,如果启用了keyspace事件通知,客户端对存储在Database 0中的键foo执行DEL操作,两条消息将通过Pub/Sub发布:

    PUBLISH __keyspace@0__:foo del
    
    PUBLISH __keyevent@0__:del foo
    
    • 1
    • 2
    • 3

    notify-keyspace-events 配置选项

    K     Keyspace events, published with __keyspace@__ prefix.(键空间通知)
    E     Keyevent events, published with __keyevent@__ prefix.(键事件通知)
    g     Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...(通用命令(非类型特定的),如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)(驱逐事件(当为maxmemory退出一个键时生成的事件))
    t     Stream commands(Stream命令)
    d     Module key type events(模块key类型事件)
    m     Key-miss events (Note: It is not included in the 'A' class)(Key-miss事件(当访问不存在的键时通知,不包含在A中))
    A     Alias for g$lshzxetd, so that the "AKE" string means all the events(Except key-miss events which are excluded from 'A' due to their unique nature)(用“AKE”可表示所有事件通知,除了特殊的Key-miss事件)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    键空间通知设置(过期键事件通知为例)

    1. 命令行方式(临时)
    redis-cli
    
    config set notify-keyspace-events Ex
    
    • 1
    • 2
    • 3

    注意:该设置在redis终端关闭后失效

    1. 配置文件方式
    vi redis.conf || redis.windows.conf
    
    执行`/notify-keyspace-events`找到`notify-keyspace-events`
    
    注释`notify-keyspace-events ""`或将`""`改为`Ex`
    
    设置`notify-keyspace-events Ex`
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    notify-keyspace-events ""为默认配置,双引号内参数为空,如无注释或修改,则键空间通知无法生效

    notify-keyspace-events Ex需顶格(不能有空格),否则启动会报错:“Invalid argument during startup: unknown conf file parameter”

    KE至少有一个存在,否则发布不了任何类型的消息

    键空间通知设置

    命令行实验

    1. 指定配置文件启动Redis
    redis-server redis.conf || redis.windows.conf
    
    • 1
    1. 订阅过期事件
    psubscribe __keyevent@0__:expired
    
    • 1

    订阅过期事件

    1. 添加过期键
    redis-cli
    
    #添加一个key为name,过期时间为10s,值为william的键
    
    setex name 10 william 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    添加过期键

    1) "psubscribe"                  # 返回值的类型:显示订阅成功
    2) "`__keyevent@0__:expired`"    # 订阅的Channel名
    3) (integer) 1                   # 目前已订阅的频道数量
    
    • 1
    • 2
    • 3
    1. 键过期通知

    键过期通知

    注意:对于psubscribe命令而言,消息会多一行

    1) "pmessage"                     # 返回值的类型:信息
    2) "`__keyevent@0__:expired`”     # 来源(从哪个ChannelPattern发送过来)
    3) "`__keyevent@0__:expired`"     # 实际的Channel
    4) "name"                         # 信息内容
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    css外边距塌陷(合并)
    (js)封装年月日获取方法,页面根据type判断显示当前年,年月,日期
    某Flutter-APP逆向分析
    更换网络ip地址怎么设置
    分布式全局唯一 ID生成器(百度UidGenerator)
    STM32存储左右互搏 SPI总线FATS文件读写FLASH W25QXX
    Python实现的天气预报APP舆情热词分析程序
    (数据结构)数据结构的三要素
    sklearn 笔记 SVM
    YOLOv9尝鲜测试五分钟极简配置
  • 原文地址:https://blog.csdn.net/qq991658923/article/details/125903641