UNPKG

@zenweb/cache

Version:
31 lines (30 loc) 728 B
import { Redis, RedisKey } from 'ioredis'; /** * 基于 Redis 的分布式锁 */ export declare class Locker { private redis; private key; private timeout; private value; /** * @param redis Redis 实例 * @param key 锁KEY * @param timeout 锁超时(毫秒) 默认 10秒 */ constructor(redis: Redis, key: RedisKey, timeout?: number); /** * 取得锁 * @returns true 取得成功, 否则失败 */ acquire(): Promise<boolean>; /** * 释放锁 */ release(): Promise<boolean>; /** * 使用锁回调 * @param callback 获取结果回调 true: 成功 false: 失败 */ using(callback: (ok: boolean) => any): Promise<void>; }