Database Reference
In-Depth Information
This allows outside processing systems to push updates to the client by
sending a
publish
event through Redis for the appropriate channel. It is
alsoallowsthe
set
and
increment
eventstobefurtherdecoupledfromthe
Counter
implementation:
Counter.prototype.set = function(counter,value,cb) {
var self = this;
this.client.hset(this.counterName,counter,value,
function(err,data) {
if(err) return cb(err,data);
self.client.publish(self.counterName,counter);
});
};
Counter.prototype.increment =
function(counter,amount,cb) {
var self = this;
this.client.hincrby(this.counterName,
counter,amount,function(err,data) {
if(err) return cb(err,data);
self.client.publish(self.counterName,counter);
cb(err,data);
});
};
Note that the
increment
method no longer relies on the
get
and
set
methods as Redis provides its own primitives for atomically incrementing
values. The get and state methods now simply have to query the Redis
store, revealing why the callback structure was used in the original
implementation despite not being strictly necessary:
Counter.prototype.state = function(cb) {
this.client.hgetall(this.counterName,cb);
};
Counter.prototype.get = function(counter,cb) {
this.client.hget(this.counterName,counter,cb);
};
After starting the dashboard again, running the commands that resulted in
Figure 7.3
should return the desired results. To see events being updated