zoukankan      html  css  js  c++  java
  • Node.js 中监听 redis key 过期事件

    It is in fact possible to listen to the “expired” type keyevent notification using a subscribed client to the specific channel and listening to its message event.

    通过 subscribe client 可以监听 __keyevent@db__:expired 频道来接收过期的事件。

    const redis = require('redis');
    const CONF = {db:3};
    var pub, sub;
    //.: Activate "notify-keyspace-events" for expired type events
    pub = redis.createClient(CONF);
    pub.send_command('config', ['set','notify-keyspace-events','Ex'], SubscribeExpired);
    //.: Subscribe to the "notify-keyspace-events" channel used for expired type events
    function SubscribeExpired(e,r){
        sub = redis.createClient(CONF);
        const expired_subKey = '__keyevent@'+CONF.db+'__:expired'
        sub.subscribe(expired_subKey,function(){
        console.log(' [i] Subscribed to "'+expired_subKey+'" event channel : '+r);
        sub.on('message',function (chan,msg){
            console.log('[expired]',msg);
       })
        TestKey();
       })
    }
    //.: For example (create a key & set to expire in 10 seconds)
    function TestKey(){
        pub.set('testing','redis notify-keyspace-events : expired')
        pub.expire('testing',10)
    }
  • 相关阅读:
    Finding Palindromes POJ
    吉哥系列故事——完美队形II HDU
    Period II FZU
    生日礼物&&Supermarket
    炮兵阵地[状态压缩DP]
    最小表示法 P1368
    Period
    最长异或路径
    Luogu P5490 扫描线
    解方程
  • 原文地址:https://www.cnblogs.com/zhxzh/p/9732716.html
Copyright © 2011-2022 走看看