Skip to content
This repository has been archived by the owner on Feb 18, 2021. It is now read-only.

Commit

Permalink
Updating readme with new customMethods functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Ellis committed Jul 16, 2013
1 parent d659249 commit d8c11e4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
34 changes: 32 additions & 2 deletions README.md
Expand Up @@ -20,7 +20,23 @@ var myRedisServers = new RedisBroadcast({
secondary: [6379, 'some.other.server'],
tertiary: [6379, 'and.another.server'],
quaternary: [6379, 'and.another']
}, { useChildProcesses: false });
}, {
useChildProcesses: false,
customMethods: {
setnxOnce: [
function setnxOnce(key, val, callback) {
this.setnx(key, val, function(err, result) {
if(err || !result) {
callback(err || new Error('key already defined'));
} else {
callback(null, result);
}
});
},
'set'
]
}
});

// Write to only one server
var writePrimary = myRedisServers.writeTo('primary');
Expand Down Expand Up @@ -67,10 +83,24 @@ writePrimaryThenRemaining.set('distributedOnlyIfSuccessful', true, callback);
// { primary: 'OK' },
// {
// secondary: 'OK',
// teritary: 'OK',
// tertiary: 'OK',
// quaternary: 'OK'
// }
// ]

// Write to primary, then all remaining servers in parallel if key doesn't already exist.
// NOTE: Custom functions don't fully work on child processes, yet. High on TODO list.
var writePrimaryThenRemainingIfNew = myRedisServers.writeLocally('primary').thenTo('remaining');
writePrimaryThenRemainingIfNew.setnxOnce('newkey', true, callback);
// Gets an array, the first array element gets the results of setnx, the second gets the results of set, if called
// [
// { primary: 1 },
// {
// secondary: 'OK',
// tertiary: 'OK',
// quaternary: 'OK
// }
// ]
```

## License (MIT)
Expand Down
32 changes: 31 additions & 1 deletion test/test.js
Expand Up @@ -129,7 +129,37 @@ exports.customMethods = function(test) {
test.equal(result[1].secondary, 'OK');
myWriter.setnxOnce('test', 'now', function(err) {
test.ok(!!err);
myServers.shutdown({ killChildProc: true}, test.done.bind(test));
myServers.shutdown(test.done.bind(test));
});
});
};

exports.customMethods2 = function(test) {
test.expect(4);
var myServers = new RedisBroadcast({
primary: [6379, 'localhost'],
secondary: [6379, 'localhost']
}, {
customMethods: {
setnxOnce: [ function setnxOnce(key, val, callback) {
this.setnx(key, val, function(err, result) {
if(err || !result) {
callback(err || new Error('key already set'));
} else {
callback(null, result);
}
});
}, 'set' ]
}
});
var myWriter = myServers.writeLocally('primary').thenTo('secondary');
myWriter.setnxOnce('test2', 'now', function(err, result) {
test.equal(result.length, 2);
test.equal(result[0].primary, 1);
test.equal(result[1].secondary, 'OK');
myWriter.setnxOnce('test2', 'now', function(err) {
test.ok(!!err);
myServers.shutdown({ killChildProc: true }, test.done.bind(test));
});
});
};
Expand Down

0 comments on commit d8c11e4

Please sign in to comment.