@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.
62 lines (61 loc) • 2.58 kB
TypeScript
/**
* @module Lock
*/
import { type Result, type Redis } from "ioredis";
import { type IReadableContext } from "../../../../execution-context/contracts/_module.js";
import { type ILockAdapter, type ILockAdapterState } from "../../../../lock/contracts/_module.js";
import { type TimeSpan } from "../../../../time-span/implementations/_module.js";
declare module "ioredis" {
interface RedisCommander<Context> {
/**
*
* @param key
* @param lockId
* @param expiration As unix timestamp in miliseconds
*/
daiso_lock_acquire(key: string, lockId: string, expiration: number | null): Result<1 | 0, Context>;
daiso_lock_release(key: string, lockId: string): Result<1 | 0, Context>;
/**
*
* @param key
* @param lockId
* @param expiration As unix timestamp in miliseconds
*/
daiso_lock_refresh(key: string, lockId: string, expiration: number): Result<1 | 0, Context>;
/**
* @returns {string} {@link IRedisJsonLockState | `IRedisJsonLockState`} or `null` as json string.
*/
daiso_lock_get_state(key: string): Result<string, Context>;
}
}
/**
* To utilize the `RedisLockAdapter`, you must install the [`"ioredis"`](https://www.npmjs.com/package/ioredis) package.
*
* Note in order to use `RedisLockAdapter` correctly, ensure you use a single, consistent database across all server instances.
*
* IMPORT_PATH: `"@daiso-tech/core/lock/redis-lock-adapter"`
* @group Adapters
*/
export declare class RedisLockAdapter implements ILockAdapter {
private readonly database;
/**
* @example
* ```ts
* import { RedisLockAdapter } from "@daiso-tech/core/lock/redis-lock-adapter";
* import Redis from "ioredis";
*
* const database = new Redis("YOUR_REDIS_CONNECTION_STRING");
* const lockAdapter = new RedisLockAdapter(database);
* ```
*/
constructor(database: Redis);
private initAquireCommand;
private initReleaseCommand;
private initRefreshComand;
private initGetStateComand;
acquire(_context: IReadableContext, key: string, lockId: string, ttl: TimeSpan | null): Promise<boolean>;
release(_context: IReadableContext, key: string, lockId: string): Promise<boolean>;
forceRelease(_context: IReadableContext, key: string): Promise<boolean>;
refresh(_context: IReadableContext, key: string, lockId: string, ttl: TimeSpan): Promise<boolean>;
getState(_context: IReadableContext, key: string): Promise<ILockAdapterState | null>;
}