当前位置: 首页>>代码示例>>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;未经允许,请勿转载。