本文整理汇总了TypeScript中redlock.lock函数的典型用法代码示例。如果您正苦于以下问题:TypeScript lock函数的具体用法?TypeScript lock怎么用?TypeScript lock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了lock函数的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: Promise
return new Promise(async resolve => {
const lock = await redlock.lock(
key + '-lock',
1000 * 60 * 60 * 3 /*3 minutes*/
);
const result = await redis.get(key);
if (result) {
const cached = JSON.parse(result);
renewCache = isExpired(cached.at, timeMs);
resolve(cached.value);
}
if (renewCache) {
value = await f(...args);
await redis.set(key, JSON.stringify({ at: Date.now(), value }));
resolve(value);
}
await lock.unlock();
});
示例2: Redlock
import {RedisClient} from 'redis';
let redlock: Redlock;
let client: RedisClient;
let lock: Lock;
redlock = new Redlock([client]);
redlock = new Redlock([client], {
driftFactor: 0.1,
retryCount: 2,
retryDelay: 3
});
redlock.acquire('resource', 30).then((lock: Lock) => { });
redlock.acquire('resource', 30, (err: any, lock: Lock) => { });
redlock.lock('resource', 30).then((lock: Lock) => { });
redlock.lock('resource', 30, (err: any, lock: Lock) => { });
// There is currently no way to test the disposer as the bluebird typings does not
// expose the .using method.
// promise.using(redlock.disposer('resource', 30), (lock: Lock) => {});
redlock.release(lock);
redlock.release(lock, (err: any) => { });
redlock.unlock(lock);
redlock.unlock(lock, (err: any) => { });
redlock.extend(lock, 30).then((lock: Lock) => { });
redlock.extend(lock, 30, (err: any, lock: Lock) => { });
lock.unlock();