warlock
Battle-hardened distributed locking using redis.
Requirements
- node-redis compatible with
v0.10
- Redis
v2.6.12
or above. If you're running a Redis version from v2.6.0
to v2.6.11
inclusive use v0.0.7
of this module.
Install
npm install node-redis-warlock
Usage
var Warlock = require('node-redis-warlock');
var redis = require('redis');
var redis = redis.createClient();
var warlock = Warlock(redis);
var key = 'test-lock';
var ttl = 10000;
warlock.lock(key, ttl, function(err, unlock){
if (err) {
return;
}
if (typeof unlock === 'function') {
unlock();
} else {
}
});
var key = 'opt-lock';
var ttl = 10000;
var maxAttempts = 4;
var wait = 1000;
warlock.optimistic(key, ttl, maxAttempts, wait, function(err, unlock) {});
var key = 'test-lock-2';
var ttl = 10000;
var lockId;
warlock.lock(key, ttl, function(err, _, id) {
lockId = id;
});
warlock.unlock(key, lockId, function(err, result) {
if(result == 1) {
}
});
ProTips