Package Exports
- node-redis-warlock
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-redis-warlock) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
warlock
Battle-hardened distributed locking using redis.
Requirements
- node-redis compatible with
v0.10 - Redis
v2.6or above
Install
npm install node-redis-warlockUsage
var Warlock = require('node-redis-warlock');
var redis = require('redis');
// Establish a redis client and pass it to warlock
var redis = redis.createClient();
var warlock = Warlock(redis);
// Set a lock
var key = 'test-lock';
var ttl = 10000;
warlock.lock(key, ttl, function(err, unlock){
if (err) {
// Something went wrong and we weren't able to set a lock
return;
}
if (typeof unlock === 'function') {
// If the lock is set successfully by this process, an unlock function is passed to our callback.
// Do the work that required lock protection, and then unlock() when finished...
//
// do stuff...
//
unlock();
} else {
// Otherwise, the lock was not established by us so we must decide what to do
// Perhaps wait a bit & retry...
}
});
ProTips
- Warlock uses Lua scripting to achieve transactional locking on Redis
v2.6.0upwards. If you're running Redisv2.6.12or above you could use the additional PX and NX arguments for the SET operation as an alternative. - Read my Distributed locks using Redis article and Redis' author's A proposal for more reliable locks using Redis to learn more about the theory of distributed locks using Redis.