Subscribe To Redis Keyspaced Events (v2.8.x) Using Redis' Newly Released Keyspaced Events Feature You can now subscribe to events that the server emits Depending on the subscription mode you subscribe with when starting the Redis Server.
--notify-keyspace-events <options>
- K Keyspace events, published with keyspace@ prefix.
- E Keyevent events, published with keyevent@ 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.
Using NPM + Package.json, simply just run npm install
If you are using node_redis
pre v0.11.0
checkout the tag v0.1.2
Start Redis Server : redis-server CONF --notify-keyspace-events KExe
var redis = require('redis');
var RedisNotifier = require('redis-notifier');
var eventNotifier = new RedisNotifier(redis, {
redis : { host : '127.0.0.1', port : 6379 },
expired : true,
evicted : true,
logLevel : 'DEBUG' //Defaults To INFO
});
//Listen for event emission
eventNotifier.on('message', function(pattern, channelPattern, emittedKey) {
var channel = this.parseMessageChannel(channelPattern);
switch(channel.key) {
case 'expired':
console.info(`Expired Key ${key}`);
break;
case "evicted":
console.info(`Evicted Key ${key}`);
break;
default:
logger.debug("Unrecognized Channel Type:" + channel.type);
}
});
In lieu of a formal style-guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.
- 1.0.0 updated required dependencies
- 0.2.0 updated node_redis connection args, added deinit method
- 0.1.2 updated logger interface
- 0.1.1 changed expire attribute to expired
- 0.1.0 Initial release
Licensed under the Apache 2.0 license.
Chris Miller