@abeai/node-redlock 中文文档教程
Wrapper for node-redlock.
Overview
这个包在 node-redlock 之上提供了一个实用的界面。
Installation
npm install @abeai/node-redlock --save
Instantiation
const Redlock = require('@abeai/node-redlock');
const redis = require('@abeai/node-redlock');
redis.init(host, options);
const redlock = new Redlock(redis.client());
redlock.lock('lockkey', 1000, () => {
...
});
Interface
Redlock(redisClients[, options])
创建一个新的 redlock 实例。
redisClients
可以是单个 Redis 客户端或一组 Redis 客户端。
Options
defaultTtl
如果在 redlock.lock
调用中省略了 ttl
,则应使用以毫秒为单位的默认 TTL。
driftFactor
以毫秒为单位的预期时钟漂移。 默认值:0.01
有关详细信息,请参阅 http://redis.io/topics/distlock。
retryCount
Redlock 在出错前尝试锁定资源的最大次数。 默认值:300
retryDelay
尝试之间的时间(以毫秒为单位)。 默认值:50
retryJitter
以毫秒为单位随机添加到重试以提高高争用性能的最长时间。 默认值:50
有关更多详细信息,请参阅 https://www.awsarchitectureblog.com/2015/03/backoff.html。
redlock.lock(resource[, ttl], fn)
使用 Redlock 算法锁定给定资源。 接受一个资源字符串,应该持有锁的毫秒 TTL,以及资源被锁定时应该执行的函数。
如果省略 ttl
,则使用 options.defaultTtl
。
Wrapper for node-redlock.
Overview
This package provides a pragmatic interface on top of node-redlock.
Installation
npm install @abeai/node-redlock --save
Instantiation
const Redlock = require('@abeai/node-redlock');
const redis = require('@abeai/node-redlock');
redis.init(host, options);
const redlock = new Redlock(redis.client());
redlock.lock('lockkey', 1000, () => {
...
});
Interface
Redlock(redisClients[, options])
Creates a new redlock instance.
redisClients
can be a single Redis client or an array of Redis clients.
Options
defaultTtl
Default TTL in ms which should be used if ttl
is obmitted in redlock.lock
call.
driftFactor
The expected clock drift in ms. Default: 0.01
For more details see http://redis.io/topics/distlock.
retryCount
The max number of times Redlock will attempt to lock a resource before erroring. Default: 300
retryDelay
The time in ms between attempts. Default: 50
retryJitter
The max time in ms randomly added to retries to improve performance under high contention. Default: 50
For more details see https://www.awsarchitectureblog.com/2015/03/backoff.html.
redlock.lock(resource[, ttl], fn)
Locks a given resource using the Redlock algorithm. Accepts a resource string, TTL of milliseconds that the lock should be held, and a function that should be executed while the resource is locked.
If ttl
is obmitted, options.defaultTtl
is used instead.