upsert for redis

This commit is contained in:
Anatoliy Chakkaev 2012-03-23 00:24:15 +04:00
parent c06f28f433
commit 95c0e7d5d0
2 changed files with 12 additions and 6 deletions

View File

@ -67,7 +67,7 @@ BridgeToRedis.prototype.updateIndexes = function (model, id, data, callback) {
if (schedule.length) { if (schedule.length) {
this.client.multi(schedule).exec(function (err) { this.client.multi(schedule).exec(function (err) {
callback(err); callback(err, data);
}); });
} else { } else {
callback(null); callback(null);
@ -75,9 +75,15 @@ BridgeToRedis.prototype.updateIndexes = function (model, id, data, callback) {
}; };
BridgeToRedis.prototype.create = function (model, data, callback) { BridgeToRedis.prototype.create = function (model, data, callback) {
if (data.id) return create.call(this, data.id, true);
var log = this.logger('INCR id:' + model); var log = this.logger('INCR id:' + model);
this.client.incr('id:' + model, function (err, id) { this.client.incr('id:' + model, function (err, id) {
log(); log();
create.call(this, id);
}.bind(this));
function create(id, upsert) {
data.id = id; data.id = id;
this.save(model, data, function (err) { this.save(model, data, function (err) {
if (callback) { if (callback) {
@ -87,9 +93,8 @@ BridgeToRedis.prototype.create = function (model, data, callback) {
// push the id to the list of user ids for sorting // push the id to the list of user ids for sorting
log('SADD s:' + model + ' ' + data.id); log('SADD s:' + model + ' ' + data.id);
this.client.sadd("s:" + model, data.id); this.client.sadd("s:" + model, upsert ? data : data.id);
}
}.bind(this));
}; };
BridgeToRedis.prototype.exists = function (model, id, callback) { BridgeToRedis.prototype.exists = function (model, id, callback) {
@ -106,7 +111,7 @@ BridgeToRedis.prototype.find = function find(model, id, callback) {
var t1 = Date.now(); var t1 = Date.now();
this.client.hgetall(model + ':' + id, function (err, data) { this.client.hgetall(model + ':' + id, function (err, data) {
this.log('HGETALL ' + model + ':' + id, t1); this.log('HGETALL ' + model + ':' + id, t1);
if (data && data.id) { if (data && Object.keys(data).length > 0) {
data.id = id; data.id = id;
} else { } else {
data = null; data = null;

View File

@ -37,8 +37,9 @@ Object.keys(schemas).forEach(function (schemaName) {
}); });
schema.log = function (a) { schema.log = function (a) {
// console.log(a); console.log(a);
}; };
testOrm(schema); testOrm(schema);
if (specificTest[schemaName]) specificTest[schemaName](schema); if (specificTest[schemaName]) specificTest[schemaName](schema);
}); });