index.ts•4.62 kB
/**
* GEPA Caching System - Main Entry Point
* Exports all caching components and utilities for easy integration
*/
import type { CacheIntegration } from './cache-integration';
export { CacheManager, type CacheConfig, type CacheEntry, type CacheStatistics } from './cache-manager';
export {
EvolutionEngineCache,
ParetoFrontierCache,
LLMAdapterCache,
TrajectoryStoreCache,
ReflectionEngineCache,
CacheRegistry
} from './component-caches';
export {
PatternMatcher,
BatchAnalysisProcessor,
AnalysisMemoryManager,
CacheStatsAggregator
} from './cache-optimizations';
export {
CacheIntegration,
CacheIntegrationFactory,
type CacheIntegrationConfig,
getCacheIntegration,
initializeGlobalCache,
shutdownGlobalCache
} from './cache-integration';
// Re-export commonly used types
export type {
CacheWarmingStrategy
} from './cache-manager';
/**
* Quick start function for initializing caching with sensible defaults
*/
export async function initializeGEPACache(environment: 'development' | 'production' | 'test' = 'development'): Promise<CacheIntegration> {
const { CacheIntegrationFactory, initializeGlobalCache } = await import('./cache-integration.js');
let config;
switch (environment) {
case 'production':
config = CacheIntegrationFactory.createProductionConfig();
break;
case 'test':
config = CacheIntegrationFactory.createTestConfig();
break;
default:
config = CacheIntegrationFactory.createDevelopmentConfig();
}
return await initializeGlobalCache(config);
}
/**
* Cache utilities and helpers
*/
export const CacheUtils = {
/**
* Create cache key from objects
*/
createKey: (...parts: (string | number | boolean)[]): string => {
return parts.map(p => String(p)).join(':');
},
/**
* Hash object to create cache key
*/
hashObject: (obj: Record<string, unknown>): string => {
const str = JSON.stringify(obj, Object.keys(obj).sort());
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash;
}
return hash.toString(36);
},
/**
* Calculate memory usage in human-readable format
*/
formatBytes: (bytes: number): string => {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
},
/**
* Calculate hit rate percentage
*/
formatHitRate: (hits: number, total: number): string => {
if (total === 0) return '0.0%';
return ((hits / total) * 100).toFixed(1) + '%';
}
};
/**
* Performance monitoring for cache operations
*/
export class CachePerformanceMonitor {
private operationTimes = new Map<string, number[]>();
/**
* Start timing an operation
*/
startTiming(operation: string): () => void {
const start = Date.now();
return () => {
const duration = Date.now() - start;
if (!this.operationTimes.has(operation)) {
this.operationTimes.set(operation, []);
}
const times = this.operationTimes.get(operation)!;
times.push(duration);
// Keep only recent times (last 100 operations)
if (times.length > 100) {
times.splice(0, times.length - 100);
}
};
}
/**
* Get performance statistics for an operation
*/
getStats(operation: string): {
count: number;
average: number;
min: number;
max: number;
p95: number;
} | null {
const times = this.operationTimes.get(operation);
if (!times || times.length === 0) return null;
const sorted = [...times].sort((a, b) => a - b);
const p95Index = Math.floor(sorted.length * 0.95);
return {
count: times.length,
average: times.reduce((sum, time) => sum + time, 0) / times.length,
min: sorted[0] || 0,
max: sorted[sorted.length - 1] || 0,
p95: sorted[p95Index] || 0
};
}
/**
* Get all performance statistics
*/
getAllStats(): Record<string, ReturnType<typeof this.getStats>> {
const stats: Record<string, ReturnType<typeof this.getStats>> = {};
for (const operation of this.operationTimes.keys()) {
stats[operation] = this.getStats(operation);
}
return stats;
}
/**
* Clear all statistics
*/
clear(): void {
this.operationTimes.clear();
}
}
/**
* Global performance monitor instance
*/
export const globalCachePerformanceMonitor = new CachePerformanceMonitor();