本文整理匯總了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();