/** * CacheService - Singleton Cache Manager * * @description Manages cache instance for the entire application using a singleton pattern. * This service wraps the CacheManager from @plyaz/core/base/cache and provides a centralized * way to initialize and access caching across all backend services. * * **Architecture:** * - Supports multiple strategies: Memory (in-process), Redis (distributed) * - Configurable TTL, key prefixing, and strategy-specific options * - Singleton pattern ensures single cache instance across application * * **Strategy Configuration:** * - **Memory**: In-process LRU cache, no external dependencies * - **Redis**: Distributed cache using ioredis, requires Redis server * * **Required Environment Variables (Redis):** * - REDIS_URL: Redis connection string (e.g., redis://localhost:6379) * - Or REDIS_HOST, REDIS_PORT, REDIS_PASSWORD for individual config * * @example Using with Core.initialize() (Recommended) * ```typescript * import { Core } from '@plyaz/core'; * * await Core.initialize({ * envPath: '.env', * cache: { * strategy: 'memory', * isEnabled: true, * ttl: 300, // 5 minutes default * }, * }); * * // Access via Core.cache * const cache = Core.cache.getCacheManager(); * await cache.set('user:123', userData, 600); // 10 minutes TTL * const cached = await cache.get('user:123'); * ``` * * @example With Redis Strategy * ```typescript * await Core.initialize({ * envPath: '.env', * cache: { * strategy: 'redis', * isEnabled: true, * ttl: 300, * prefix: 'myapp', * redis: { * url: process.env.REDIS_URL, * // Or individual options: * // host: process.env.REDIS_HOST, * // port: parseInt(process.env.REDIS_PORT), * // password: process.env.REDIS_PASSWORD, * }, * }, * }); * ``` * * @example Direct Usage * ```typescript * import { CacheService } from '@plyaz/core/services'; * * await CacheService.initialize({ * strategy: 'memory', * isEnabled: true, * ttl: 300, * }); * * const cache = CacheService.getInstance().getCacheManager(); * await cache.set('key', value); * ``` * * @example Skip Cache (Disable Caching) * ```typescript * await Core.initialize({ * skipCache: true, // Cache will not be initialized * }); * ``` * * @fileoverview Cache service singleton * @module services/CacheService */ import { CacheManager } from '../base/cache'; import type { CoreCacheConfig, CoreCacheServiceInstance } from '@plyaz/types/core'; /** * Singleton service for managing application-wide cache * * Provides centralized access to cache functionality across all backend services. * Automatically integrates with BaseBackendDomainService when initialized. */ export declare class CacheService implements CoreCacheServiceInstance { /** Cache manager instance */ private cacheManager; /** Configuration used to initialize cache */ private config; /** Logger instance */ private logger; /** Private constructor to enforce singleton */ private constructor(); /** * Emits a cache error event via CoreEventManager. * Called when cache operations fail to integrate with global error handling. */ private emitCacheError; /** * Initialize the cache service with configuration * * @param config - Cache configuration * @throws {CorePackageError} If already initialized or config is invalid * * @example * ```typescript * await CacheService.initialize({ * strategy: 'redis', * isEnabled: true, * ttl: 300, * prefix: 'app', * redis: { url: process.env.REDIS_URL }, * }); * ``` */ static initialize(config: CoreCacheConfig): Promise; /** * Get the singleton instance * * @throws {CorePackageError} If not initialized * @returns CacheService singleton instance * * @example * ```typescript * const cacheService = CacheService.getInstance(); * const cache = cacheService.getCacheManager(); * ``` */ static getInstance(): CacheService; /** * Get the cache manager instance * * @throws {CorePackageError} If cache not initialized * @returns CacheManager instance * * @example * ```typescript * const cache = CacheService.getInstance().getCacheManager(); * await cache.set('user:123', userData); * const user = await cache.get('user:123'); * ``` */ getCacheManager(): CacheManager; /** * Get current cache configuration * * @returns Current cache configuration or null if not initialized */ getConfig(): CoreCacheConfig | null; /** * Check if cache is initialized and enabled * * @returns True if cache is ready to use */ isInitialized(): boolean; /** * Reset the cache service (useful for testing) * * @internal */ static reset(): void; /** * Validate cache configuration * * @param config - Configuration to validate * @throws {CorePackageError} If configuration is invalid * @private */ private validateConfig; } export declare const getCacheService: () => CacheService; //# sourceMappingURL=CacheService.d.ts.map