import { dragonflyConfig } from '@stock-bot/config'; import { RedisCache } from './providers/redis-cache'; import { MemoryCache } from './providers/memory-cache'; import { HybridCache } from './providers/hybrid-cache'; import type { CacheProvider, CacheOptions, CacheConfig } from './types'; /** * Factory for creating cache providers with smart defaults * * @param type 'redis' | 'memory' | 'hybrid' | 'auto' * @param options configuration for the cache */ export function createCache( type: 'redis' | 'memory' | 'hybrid' | 'auto' = 'auto', options: CacheOptions = {} ): CacheProvider { // Auto-detect best cache type based on environment if (type === 'auto') { try { // Try to use hybrid cache if Redis/Dragonfly is configured if (dragonflyConfig.DRAGONFLY_HOST) { type = 'hybrid'; } else { type = 'memory'; } } catch { // Fallback to memory if config is not available type = 'memory'; } } switch (type) { case 'redis': return new RedisCache(options); case 'memory': return new MemoryCache(options); case 'hybrid': return new HybridCache(options); default: throw new Error(`Unknown cache type: ${type}`); } } /** * Create a cache instance with trading-optimized defaults */ export function createTradingCache(options: Partial = {}): CacheProvider { const defaultOptions: CacheOptions = { keyPrefix: 'trading:', ttl: 3600, // 1 hour default memoryTTL: 300, // 5 minutes for memory cache maxMemoryItems: 2000, // More items for trading data enableMetrics: true, ...options }; return createCache('auto', defaultOptions); } /** * Create a cache for market data with appropriate settings */ export function createMarketDataCache(options: Partial = {}): CacheProvider { const defaultOptions: CacheOptions = { keyPrefix: 'market:', ttl: 300, // 5 minutes for market data memoryTTL: 60, // 1 minute in memory maxMemoryItems: 5000, // Lots of market data enableMetrics: true, ...options }; return createCache('auto', defaultOptions); } /** * Create a cache for indicators with longer TTL */ export function createIndicatorCache(options: Partial = {}): CacheProvider { const defaultOptions: CacheOptions = { keyPrefix: 'indicators:', ttl: 1800, // 30 minutes for indicators memoryTTL: 600, // 10 minutes in memory maxMemoryItems: 1000, enableMetrics: true, ...options }; return createCache('auto', defaultOptions); } // Export types and classes export type { CacheProvider, CacheOptions, CacheConfig, CacheStats, CacheKey, SerializationOptions } from './types'; export { RedisCache } from './providers/redis-cache'; export { MemoryCache } from './providers/memory-cache'; export { HybridCache } from './providers/hybrid-cache'; export { CacheKeyGenerator } from './key-generator'; export { Cacheable, CacheEvict, CacheWarm, CacheMarketData, CacheIndicator, CacheStrategy } from './decorators/cacheable'; // Default export for convenience export default createCache;