How to receive Redis expire events with node?

前端 未结 2 1825
Happy的楠姐
Happy的楠姐 2020-12-30 09:01

I want to listen the expire events from Redis. I\'ve configured on my redis.conf the notify-keyspace-events \"AKE\" and this is my code on node:

2条回答
  •  时光取名叫无心
    2020-12-30 09:12

    It is in fact possible to listen to the "expired" type keyevent notification using a subscribed client to the specific channel ('__keyevent@db__:expired') and listening to its message event.

    no need for setInterval / setTimeout or additional libraries

    Proof-of-concept (working : tested with NodeJS v.9.4.0)

    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)
    }
    

提交回复
热议问题