Permalink
Browse files

Fix typo, tune redis2

  • Loading branch information...
1602 committed Sep 9, 2012
1 parent 4459111 commit a2618dd3a2a0f0f5e0190ef167a34c1803b434b1
Showing with 22 additions and 7 deletions.
  1. +1 −1 lib/adapters/redis.js
  2. +21 −6 lib/adapters/redis2.js
View
@@ -6,7 +6,7 @@ var safeRequire = require('../utils').safeRequire;
var redis = safeRequire('redis');
exports.initialize = function initializeSchema(schema, callback) {
- console.log('GOOD NEWS! Redis this redis adapter version is deprecated, use redis2 instead. A lot of improvements, and new indexes incompatible with old (sorry about that): now we only store id and not ModelName:id in indexes. Also dates format in indexes changed to unix timestamp for better sorting and filtering performance');
+ console.log('GOOD NEWS! This redis adapter version is deprecated, use redis2 instead. A lot of improvements, and new indexes incompatible with old (sorry about that): now we only store id and not ModelName:id in indexes. Also dates format in indexes changed to unix timestamp for better sorting and filtering performance');
if (!redis) return;
if (schema.settings.url) {
View
@@ -86,20 +86,20 @@ commands.forEach(function (cmd) {
});
Client.prototype.multi = function (commands, callback) {
- if (commands.length === 0) return callback();
+ if (commands.length === 0) return callback && callback();
if (commands.length === 1) {
return this[commands[0].shift().toLowerCase()].call(
this,
commands[0],
- function (e, r) { callback(e, [r]) });
+ callback && function (e, r) { callback(e, [r]) });
}
var log = this._adapter.logger('MULTI\n ' + commands.map(function (x) {
return x.join(' ');
}).join('\n ') + '\nEXEC');
this._client.multi(commands).exec(function (err, replies) {
if (err) console.log(err);
log();
- callback(err, replies);
+ callback && callback(err, replies);
});
};
@@ -260,13 +260,28 @@ BridgeToRedis.prototype.create = function (model, data, callback) {
});
// push the id to the list of user ids for sorting
- this.client.sadd(["s:" + model, upsert ? data : data.id]);
+ this.client.sadd(['s:' + model, data.id]);
}
};
BridgeToRedis.prototype.updateOrCreate = function (model, data, callback) {
- if (!data.id) return this.create(model, data, callback);
- this.save(model, data, callback);
+ if (!data.id) {
+ return this.create(model, data, callback);
+ }
+ var client = this.client;
+ this.save(model, data, function (error, obj) {
+ var key = 'id:' + model;
+ client.get(key, function (err, id) {
+ if (!id || data.id > parseInt(id, 10)) {
+ client.set([key, data.id], ok);
+ } else {
+ ok();
+ }
+ });
+ function ok() {
+ callback(error, obj);
+ }
+ });
};
BridgeToRedis.prototype.exists = function (model, id, callback) {

0 comments on commit a2618dd

Please sign in to comment.