UNPKG

ts-api-core

Version:

Nodejs api framework core

66 lines (65 loc) 2.24 kB
/// <reference types="node" /> import { BaseObject } from '../context/base.object'; import { RequestContext } from '../context/request.context'; declare enum LockStateValue { /** 未加锁 */ unlock = 0, /** 已加锁 */ locked = 1 } declare class LockState { last: number; state: LockStateValue; owner: string; constructor(data?: string); } declare class LockedState { initTimer(doUpdateState: (key: string, v: LockState) => void): void; unlocked: boolean; timer: NodeJS.Timeout | undefined; v: LockState; key: string; } /** * MySQL 分布式锁 */ export declare class MysqlDistributedLock extends BaseObject<RequestContext> { protected readonly table: string; protected readonly keyFieldName: string; protected readonly valueFieldName: string; protected readonly owner: string; protected readonly timeout: number; protected readonly state: Record<string, LockedState | undefined>; protected readonly prefixStr: string; /** * MySQL 分布式锁 * @param context 上下文对象 * @param table 表名 * @param keyFieldName key 字段名称 * @param valueFieldName value 字段名称 * @param keyName 加锁的 key 名称 * @param timeout 锁超时 秒 (默认 60) * @param prefix 锁的前缀标识 */ constructor(context: RequestContext, table: string, keyFieldName: string, valueFieldName: string, timeout?: number, prefix?: string, flag?: string); private getStateValue; private initTimer; /** * 加锁 * @param key KEY * @param waitTime 等待确认时间,默认 `500`ms * @returns */ lock(key: string, waitTime?: number): Promise<boolean>; /** 释放分布式锁 */ unlock(key: string, deleteLock?: boolean): Promise<boolean>; /** 判断是否已经被锁定 */ isLocked(key: string): Promise<boolean>; /** 获取锁状态 */ protected getState(key: string): Promise<LockState>; /** 更新锁状态 */ protected setState(key: string, state: LockState): Promise<boolean>; /** 删除锁 */ protected deleteLock(key: string): Promise<boolean>; } export {};