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
const Warlock = require('node-redis-warlock');
const Redis = require('redis');
const redis = Redis.createClient();
const warlock = Warlock(redis);
const key = 'test-lock';
const ttl = 10000;
warlock.lock(key, ttl, (err, unlock) => {
if (err) {
return;
}
if (typeof unlock === 'function') {
unlock();
} else {
}
});
const key = 'opt-lock';
const ttl = 10000;
const maxAttempts = 4;
const wait = 1000;
warlock.optimistic(key, ttl, maxAttempts, wait, (err, unlock) => {});
var key = 'test-lock-2';
var ttl = 10000;
let lockId;
warlock.lock(key, ttl, (err, _, id) => {
lockId = id;
});
warlock.unlock(key, lockId, (err, result) => {
if (result == 1) {
}
});
ProTips