當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript redlock.lock函數代碼示例

本文整理匯總了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();
    });
開發者ID:WordToken,項目名稱:voice-web,代碼行數:20,代碼來源:lazy-cache.ts

示例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();
開發者ID:Crevil,項目名稱:DefinitelyTyped,代碼行數:31,代碼來源:redlock-tests.ts


注:本文中的redlock.lock函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。