A simple and convienient wrapper for Redlock, written in TypeScript.
npm i withlock
import Redlock from 'withlock';
OR
const Redlock = require('withlock');
See initialzation options below
interface ConstructorOptions {
delay?: number
timeout?: number
clientOptions?: RedisOptions
redisClient?: PickedRedisClient
}
const lock = new Redlock ()
const clientOptions = {
port: 6379, // Redis port
host: "127.0.0.1", // Redis host
username: "default", // needs Redis >= 6
password: "my-top-secret",
db: 0, // Defaults to 0
};
const lock = new Redlock ({ clientOptions })
NOTE: This redis client must satisfy the type of ioredis's set
method.
import redis from 'ioredis';
const redisClient = new redis ({
port: 6379, // Redis port
host: "127.0.0.1", // Redis host
username: "default", // needs Redis >= 6
password: "my-top-secret",
db: 0, // Defaults to 0
});
const lock = new Redlock ({ redisClient })
const lock = new Redlock ();
const cb = (): number => {
// do some stuff
return 1;
}
// withLock infers return type from cb
const lockedCb: Promise<number> = lock.withLock('KEY', cb);
const lock = new Redlock ();
const cb = async (): number => {
// do some stuff
return 1;
}
// withLock infers return type from cb
const lockedCb: Promise<number> = lock.withLock('KEY', cb);