Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | /**
* Intelligence Domain Cache
*
* LRU cache with TTL support for intelligence operations.
* Reduces redundant API calls and improves responsiveness.
*/
import type { CacheEntry, CacheStats } from './types';
/**
* Simple LRU cache with TTL support
*/
export class IntelligenceCache {
private cache = new Map<string, CacheEntry<unknown>>();
private accessOrder: string[] = [];
private maxSize: number;
private defaultTTL: number;
// Stats tracking
private hits = 0;
private misses = 0;
constructor(options?: { maxSize?: number; defaultTTL?: number }) {
this.maxSize = options?.maxSize ?? 100;
this.defaultTTL = options?.defaultTTL ?? 5 * 60 * 1000; // 5 minutes default
}
/**
* Get a value from cache
*/
get<T>(key: string): T | null {
const entry = this.cache.get(key) as CacheEntry<T> | undefined;
if (!entry) {
this.misses++;
return null;
}
// Check TTL
if (Date.now() > entry.timestamp + entry.ttl) {
this.cache.delete(key);
this.accessOrder = this.accessOrder.filter(k => k !== key);
this.misses++;
return null;
}
// Update access order (move to end = most recently used)
this.accessOrder = this.accessOrder.filter(k => k !== key);
this.accessOrder.push(key);
this.hits++;
return entry.data;
}
/**
* Set a value in cache
*/
set<T>(key: string, data: T, ttl?: number): void {
// Remove if exists (to update access order)
if (this.cache.has(key)) {
this.accessOrder = this.accessOrder.filter(k => k !== key);
}
// Evict oldest if at capacity
while (this.cache.size >= this.maxSize && this.accessOrder.length > 0) {
const oldestKey = this.accessOrder.shift();
if (oldestKey) {
this.cache.delete(oldestKey);
}
}
this.cache.set(key, {
data,
timestamp: Date.now(),
ttl: ttl ?? this.defaultTTL,
});
this.accessOrder.push(key);
}
/**
* Check if key exists and is not expired
*/
has(key: string): boolean {
const entry = this.cache.get(key);
if (!entry) return false;
if (Date.now() > entry.timestamp + entry.ttl) {
this.cache.delete(key);
this.accessOrder = this.accessOrder.filter(k => k !== key);
return false;
}
return true;
}
/**
* Delete a specific key
*/
delete(key: string): boolean {
if (this.cache.has(key)) {
this.cache.delete(key);
this.accessOrder = this.accessOrder.filter(k => k !== key);
return true;
}
return false;
}
/**
* Clear all cache entries
*/
clear(): void {
this.cache.clear();
this.accessOrder = [];
// Don't reset hits/misses to maintain historical stats
}
/**
* Get cache statistics
*/
getStats(): CacheStats {
const total = this.hits + this.misses;
return {
entries: this.cache.size,
hits: this.hits,
misses: this.misses,
hitRate: total > 0 ? this.hits / total : 0,
};
}
/**
* Reset statistics
*/
resetStats(): void {
this.hits = 0;
this.misses = 0;
}
/**
* Get all keys (for debugging)
*/
keys(): string[] {
return Array.from(this.cache.keys());
}
/**
* Get cache size
*/
get size(): number {
return this.cache.size;
}
}
// === Cache Key Generators ===
/**
* Generate cache key for memory search
*/
export function memorySearchKey(options: Record<string, unknown>): string {
const sorted = Object.entries(options)
.filter(([, v]) => v !== undefined)
.sort(([a], [b]) => a.localeCompare(b))
.map(([k, v]) => `${k}=${JSON.stringify(v)}`)
.join('&');
return `memory:search:${sorted}`;
}
/**
* Generate cache key for identity
*/
export function identityKey(agentId: string): string {
return `identity:${agentId}`;
}
/**
* Generate cache key for heartbeat config
*/
export function heartbeatConfigKey(agentId: string): string {
return `heartbeat:config:${agentId}`;
}
/**
* Generate cache key for reflection state
*/
export function reflectionStateKey(): string {
return 'reflection:state';
}
// === Singleton Instance ===
let cacheInstance: IntelligenceCache | null = null;
/**
* Get the global cache instance
*/
export function getIntelligenceCache(): IntelligenceCache {
if (!cacheInstance) {
cacheInstance = new IntelligenceCache({
maxSize: 200,
defaultTTL: 5 * 60 * 1000, // 5 minutes
});
}
return cacheInstance;
}
/**
* Clear the global cache instance
*/
export function clearIntelligenceCache(): void {
if (cacheInstance) {
cacheInstance.clear();
}
}
|