I need to check whether a redis value has been changed by another process and return it's value if it has been changed and false if it hasn't.
I believe that the watch function may be designed for this but I am struggling to understand how to implement it as the watch always returns OK irrespective of whether the variable was changed or not.
Here's what I have now, but this always returns the variable even if not changed.
function changed(){
client.watch("tally", function (err,reply) {
if(reply=="OK") client.get("tally", function(err, reply) { console.log(reply);});
else console.log('nochange');
});
var x = setTimeout(function(){ changed(); },1000);
}
changed();
Watch always returns OK; see http://redis.io/commands/watch – it is used only in combination with MULTI/EXEC (transactions) to check EXEC return value.
You can monitor/poll a Redis key value like this:
var redis = require('redis');
var client = redis.createClient();
var keytracker;
function changed() {
client.get("tally", function(err, reply) {
if (err)
console.log('A Problem with Redis: ' + err);
if (reply == keytracker) {
console.log('nochange');
} else {
keytracker = reply;
console.log(reply);
}
});
setTimeout(function() { changed(); }, 1000);
}
changed();