UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

81 lines (80 loc) 4.37 kB
/** * @module SharedLock */ import { type Redis, type Result } from "ioredis"; import { type IReadableContext } from "../../../../execution-context/contracts/_module.js"; import { type ISharedLockAdapter, type ISharedLockAdapterState, type SharedLockAcquireSettings } from "../../../../shared-lock/contracts/_module.js"; import { type TimeSpan } from "../../../../time-span/implementations/_module.js"; declare module "ioredis" { interface RedisCommander<Context> { daiso_shared_lock_acquire_writer(key: string, lockId: string, expiration: number | null): Result<1 | 0, Context>; daiso_shared_lock_release_writer(key: string, lockId: string): Result<1 | 0, Context>; daiso_shared_lock_refresh_writer(key: string, lockId: string, expiration: number): Result<1 | 0, Context>; daiso_shared_lock_force_release_writer(key: string): Result<1 | 0, Context>; daiso_shared_lock_acquire_reader(key: string, lockId: string, limit: number, expiration: number | null, now: number): Result<1 | 0, Context>; daiso_shared_lock_release_reader(key: string, lockId: string, now: number): Result<1 | 0, Context>; daiso_shared_lock_refresh_reader(key: string, lockId: string, expiration: number, now: number): Result<1 | 0, Context>; daiso_shared_lock_force_release_all_readers(key: string, now: number): Result<1 | 0, Context>; daiso_shared_lock_force_release(key: string, now: number): Result<1 | 0, Context>; /** * Returns {@link IRedisJsonSharedLockState | `IRedisJsonSharedLockState | null`} as json string. */ daiso_shared_lock_get_state(key: string, now: number): Result<string, Context>; } } /** * To utilize the `RedisSharedLockAdapter`, you must install the [`"ioredis"`](https://www.npmjs.com/package/ioredis) package. * * Note in order to use `RedisSharedLockAdapter` correctly, ensure you use a single, consistent database across all server instances. * * IMPORT_PATH: `"@daiso-tech/core/shared-lock/redis-shared-lock-adapter"` * @group Adapters */ export declare class RedisSharedLockAdapter implements ISharedLockAdapter { private readonly database; /** * @example * ```ts * import { RedisSharedLockAdapter } from "@daiso-tech/core/shared-lock/redis-shared-lock-adapter"; * import Redis from "ioredis"; * * const database = new Redis("YOUR_REDIS_CONNECTION_STRING"); * const sharedLockAdapter = new RedisSharedLockAdapter(database); * ``` */ constructor(database: Redis); private static getWriterKey; private static getReaderKey; private initAquireWriterCommand; private initReleaseWriterCommand; private initRefreshWriterCommand; private static _forceReleaseWriter; private initForceReleaseWriterCommand; private static getLimitKey; private static removeExpiredSlots; private static getOrSetLimit; private static isLimitReached; private static hasSlotId; private static addSlot; private static updateKeyExpiration; private static removeSemaphoreIfEmpty; private initAquireReaderCommand; private initReleaseReaderCommand; private initRefreshReaderCommand; private static _forceReleaseAllReaders; private initForceReleaseAllReadersCommand; private initForceReleaseCommand; private static getWriterState; private static getReaderState; private initGetStateCommand; acquireWriter(_context: IReadableContext, key: string, lockId: string, ttl: TimeSpan | null): Promise<boolean>; releaseWriter(_context: IReadableContext, key: string, lockId: string): Promise<boolean>; refreshWriter(_context: IReadableContext, key: string, lockId: string, ttl: TimeSpan): Promise<boolean>; forceReleaseWriter(_context: IReadableContext, key: string): Promise<boolean>; acquireReader(settings: SharedLockAcquireSettings): Promise<boolean>; releaseReader(_context: IReadableContext, key: string, lockId: string): Promise<boolean>; refreshReader(_context: IReadableContext, key: string, lockId: string, ttl: TimeSpan): Promise<boolean>; forceReleaseAllReaders(_context: IReadableContext, key: string): Promise<boolean>; forceRelease(_context: IReadableContext, key: string): Promise<boolean>; getState(_context: IReadableContext, key: string): Promise<ISharedLockAdapterState | null>; }