/** * Lock Strategy Examples * * This file demonstrates the different lock acquisition strategies available in Redis Lock module. */ import { RedisLockService } from '../index'; /** * SKIP strategy - Skip execution if lock cannot be acquired * This is the default behavior and is useful for scheduled tasks where you don't * want to queue up requests if a previous execution is still running. */ export declare class SkipStrategyExample { private readonly lockService; private readonly logger; constructor(lockService: RedisLockService); /** * Example 1: Using decorator with SKIP strategy (default) */ scheduledTask(): Promise; /** * Example 2: Using service directly with SKIP strategy */ processWithSkip(): Promise<{ skipped: boolean; success?: undefined; } | { success: boolean; skipped?: undefined; }>; /** * Example 3: SKIP with retries */ processWithRetries(): Promise<{ skipped: boolean; success?: undefined; } | { success: boolean; skipped?: undefined; }>; private simulateLongRunning; } /** * THROW strategy - Throw an error if lock cannot be acquired * Use this for critical operations that must have exclusive access. * If the lock cannot be acquired, an exception will be thrown. */ export declare class ThrowStrategyExample { private readonly lockService; private readonly logger; constructor(lockService: RedisLockService); /** * Example 1: Using decorator with THROW strategy */ criticalOperation(): Promise; /** * Example 2: Using service with error handling */ processWithThrow(): Promise<{ success: boolean; }>; /** * Example 3: Critical payment processing */ processPayment(orderId: string, amount: number): Promise<{ success: boolean; }>; private processData; private chargePayment; private updateOrderStatus; } /** * WAIT strategy - Block/wait until lock can be acquired * Use this when you want to queue execution and ensure the operation eventually runs. * The function will wait indefinitely (or until waitTimeout) for the lock to become available. */ export declare class WaitStrategyExample { private readonly lockService; private readonly logger; constructor(lockService: RedisLockService); /** * Example 1: Using decorator with WAIT strategy */ sequentialTask(): Promise; /** * Example 2: WAIT with timeout */ processWithWaitTimeout(): Promise<{ timeout: boolean; success?: undefined; } | { success: boolean; timeout?: undefined; }>; /** * Example 3: Sequential data processing * Perfect for operations that must be processed in order */ processDataSequentially(userId: string, data: any): Promise<{ success: boolean; }>; /** * Example 4: Queue-like behavior * Multiple requests will queue and execute one by one */ processQueue(queueName: string, item: any): Promise<{ success: boolean; }>; /** * Example 5: Database migration with WAIT * Ensures migrations run sequentially across all instances */ runMigration(migrationName: string): Promise<{ success: boolean; }>; private processInOrder; private validateData; private saveData; private notifyUser; private processQueueItem; private executeMigration; } /** * USE SKIP WHEN: * - Running scheduled tasks where you don't want overlapping executions * - Cron jobs that should skip if previous run is still in progress * - Non-critical background tasks * - Operations where it's okay to skip if busy * * USE THROW WHEN: * - Critical operations that require exclusive access * - Financial transactions * - Operations where failure to acquire lock is an error * - When you want to alert/fail fast if resource is busy * * USE WAIT WHEN: * - Operations that must execute eventually * - Sequential processing requirements * - Queue-like behavior needed * - Database migrations * - Operations where order matters * - When you want to ensure the operation runs (with optional timeout) */ /** * Example: Combining strategies in a single service */ export declare class CombinedStrategiesExample { private readonly lockService; private readonly logger; constructor(lockService: RedisLockService); /** * Background cleanup - SKIP if already running */ cleanupOldData(): Promise; /** * Payment processing - THROW if cannot get exclusive access */ processPayment(orderId: string): Promise; /** * Report generation - WAIT to ensure it runs eventually */ generateReport(reportId: string): Promise; }