@claude-flow/memory 3.0.0-alpha.1 → 3.0.0-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/README.md +356 -18
  2. package/dist/agent-memory-scope.d.ts +131 -0
  3. package/dist/agent-memory-scope.d.ts.map +1 -0
  4. package/dist/agent-memory-scope.js +215 -0
  5. package/dist/agent-memory-scope.js.map +1 -0
  6. package/dist/agent-memory-scope.test.d.ts +8 -0
  7. package/dist/agent-memory-scope.test.d.ts.map +1 -0
  8. package/dist/agent-memory-scope.test.js +463 -0
  9. package/dist/agent-memory-scope.test.js.map +1 -0
  10. package/dist/agentdb-adapter.d.ts +22 -3
  11. package/dist/agentdb-adapter.d.ts.map +1 -1
  12. package/dist/agentdb-adapter.js +135 -8
  13. package/dist/agentdb-adapter.js.map +1 -1
  14. package/dist/auto-memory-bridge.d.ts +226 -0
  15. package/dist/auto-memory-bridge.d.ts.map +1 -0
  16. package/dist/auto-memory-bridge.js +709 -0
  17. package/dist/auto-memory-bridge.js.map +1 -0
  18. package/dist/auto-memory-bridge.test.d.ts +8 -0
  19. package/dist/auto-memory-bridge.test.d.ts.map +1 -0
  20. package/dist/auto-memory-bridge.test.js +754 -0
  21. package/dist/auto-memory-bridge.test.js.map +1 -0
  22. package/dist/benchmark.test.d.ts +2 -0
  23. package/dist/benchmark.test.d.ts.map +1 -0
  24. package/dist/benchmark.test.js +277 -0
  25. package/dist/benchmark.test.js.map +1 -0
  26. package/dist/hybrid-backend.d.ts.map +1 -1
  27. package/dist/hybrid-backend.js +29 -4
  28. package/dist/hybrid-backend.js.map +1 -1
  29. package/dist/index.d.ts +8 -0
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +8 -0
  32. package/dist/index.js.map +1 -1
  33. package/dist/learning-bridge.d.ts +137 -0
  34. package/dist/learning-bridge.d.ts.map +1 -0
  35. package/dist/learning-bridge.js +335 -0
  36. package/dist/learning-bridge.js.map +1 -0
  37. package/dist/learning-bridge.test.d.ts +8 -0
  38. package/dist/learning-bridge.test.d.ts.map +1 -0
  39. package/dist/learning-bridge.test.js +578 -0
  40. package/dist/learning-bridge.test.js.map +1 -0
  41. package/dist/memory-graph.d.ts +100 -0
  42. package/dist/memory-graph.d.ts.map +1 -0
  43. package/dist/memory-graph.js +333 -0
  44. package/dist/memory-graph.js.map +1 -0
  45. package/dist/memory-graph.test.d.ts +8 -0
  46. package/dist/memory-graph.test.d.ts.map +1 -0
  47. package/dist/memory-graph.test.js +609 -0
  48. package/dist/memory-graph.test.js.map +1 -0
  49. package/dist/sqlite-backend.js +3 -3
  50. package/dist/sqljs-backend.d.ts.map +1 -1
  51. package/dist/sqljs-backend.js +5 -2
  52. package/dist/sqljs-backend.js.map +1 -1
  53. package/dist/types.d.ts +3 -0
  54. package/dist/types.d.ts.map +1 -1
  55. package/package.json +20 -5
  56. package/.agentic-flow/intelligence.json +0 -16
  57. package/__tests__/coverage/base.css +0 -224
  58. package/__tests__/coverage/block-navigation.js +0 -87
  59. package/__tests__/coverage/coverage-final.json +0 -19
  60. package/__tests__/coverage/favicon.png +0 -0
  61. package/__tests__/coverage/index.html +0 -206
  62. package/__tests__/coverage/lcov-report/base.css +0 -224
  63. package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
  64. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  65. package/__tests__/coverage/lcov-report/index.html +0 -206
  66. package/__tests__/coverage/lcov-report/prettify.css +0 -1
  67. package/__tests__/coverage/lcov-report/prettify.js +0 -2
  68. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  69. package/__tests__/coverage/lcov-report/sorter.js +0 -210
  70. package/__tests__/coverage/lcov-report/src/agentdb-adapter.ts.html +0 -2737
  71. package/__tests__/coverage/lcov-report/src/agentdb-backend.ts.html +0 -3130
  72. package/__tests__/coverage/lcov-report/src/application/commands/delete-memory.command.ts.html +0 -601
  73. package/__tests__/coverage/lcov-report/src/application/commands/index.html +0 -131
  74. package/__tests__/coverage/lcov-report/src/application/commands/store-memory.command.ts.html +0 -394
  75. package/__tests__/coverage/lcov-report/src/application/queries/index.html +0 -116
  76. package/__tests__/coverage/lcov-report/src/application/queries/search-memory.query.ts.html +0 -796
  77. package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
  78. package/__tests__/coverage/lcov-report/src/application/services/memory-application-service.ts.html +0 -793
  79. package/__tests__/coverage/lcov-report/src/cache-manager.ts.html +0 -1633
  80. package/__tests__/coverage/lcov-report/src/database-provider.ts.html +0 -1618
  81. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
  82. package/__tests__/coverage/lcov-report/src/domain/entities/memory-entry.ts.html +0 -952
  83. package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
  84. package/__tests__/coverage/lcov-report/src/domain/services/memory-domain-service.ts.html +0 -1294
  85. package/__tests__/coverage/lcov-report/src/hnsw-index.ts.html +0 -3124
  86. package/__tests__/coverage/lcov-report/src/hybrid-backend.ts.html +0 -2167
  87. package/__tests__/coverage/lcov-report/src/index.html +0 -266
  88. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/hybrid-memory-repository.ts.html +0 -1633
  89. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/index.html +0 -116
  90. package/__tests__/coverage/lcov-report/src/migration.ts.html +0 -2092
  91. package/__tests__/coverage/lcov-report/src/query-builder.ts.html +0 -1711
  92. package/__tests__/coverage/lcov-report/src/sqlite-backend.ts.html +0 -2281
  93. package/__tests__/coverage/lcov-report/src/sqljs-backend.ts.html +0 -2374
  94. package/__tests__/coverage/lcov-report/src/types.ts.html +0 -2266
  95. package/__tests__/coverage/lcov.info +0 -10238
  96. package/__tests__/coverage/prettify.css +0 -1
  97. package/__tests__/coverage/prettify.js +0 -2
  98. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  99. package/__tests__/coverage/sorter.js +0 -210
  100. package/__tests__/coverage/src/agentdb-adapter.ts.html +0 -2737
  101. package/__tests__/coverage/src/agentdb-backend.ts.html +0 -3130
  102. package/__tests__/coverage/src/application/commands/delete-memory.command.ts.html +0 -601
  103. package/__tests__/coverage/src/application/commands/index.html +0 -131
  104. package/__tests__/coverage/src/application/commands/store-memory.command.ts.html +0 -394
  105. package/__tests__/coverage/src/application/queries/index.html +0 -116
  106. package/__tests__/coverage/src/application/queries/search-memory.query.ts.html +0 -796
  107. package/__tests__/coverage/src/application/services/index.html +0 -116
  108. package/__tests__/coverage/src/application/services/memory-application-service.ts.html +0 -793
  109. package/__tests__/coverage/src/cache-manager.ts.html +0 -1633
  110. package/__tests__/coverage/src/database-provider.ts.html +0 -1618
  111. package/__tests__/coverage/src/domain/entities/index.html +0 -116
  112. package/__tests__/coverage/src/domain/entities/memory-entry.ts.html +0 -952
  113. package/__tests__/coverage/src/domain/services/index.html +0 -116
  114. package/__tests__/coverage/src/domain/services/memory-domain-service.ts.html +0 -1294
  115. package/__tests__/coverage/src/hnsw-index.ts.html +0 -3124
  116. package/__tests__/coverage/src/hybrid-backend.ts.html +0 -2167
  117. package/__tests__/coverage/src/index.html +0 -266
  118. package/__tests__/coverage/src/infrastructure/repositories/hybrid-memory-repository.ts.html +0 -1633
  119. package/__tests__/coverage/src/infrastructure/repositories/index.html +0 -116
  120. package/__tests__/coverage/src/migration.ts.html +0 -2092
  121. package/__tests__/coverage/src/query-builder.ts.html +0 -1711
  122. package/__tests__/coverage/src/sqlite-backend.ts.html +0 -2281
  123. package/__tests__/coverage/src/sqljs-backend.ts.html +0 -2374
  124. package/__tests__/coverage/src/types.ts.html +0 -2266
  125. package/benchmarks/cache-hit-rate.bench.ts +0 -535
  126. package/benchmarks/hnsw-indexing.bench.ts +0 -552
  127. package/benchmarks/memory-write.bench.ts +0 -469
  128. package/benchmarks/vector-search.bench.ts +0 -449
  129. package/docs/AGENTDB-INTEGRATION.md +0 -388
  130. package/docs/CROSS_PLATFORM.md +0 -505
  131. package/docs/WINDOWS_SUPPORT.md +0 -422
  132. package/examples/agentdb-example.ts +0 -345
  133. package/examples/cross-platform-usage.ts +0 -326
  134. package/framework/benchmark.ts +0 -112
  135. package/src/agentdb-adapter.ts +0 -884
  136. package/src/agentdb-backend.test.ts +0 -339
  137. package/src/agentdb-backend.ts +0 -1016
  138. package/src/application/commands/delete-memory.command.ts +0 -172
  139. package/src/application/commands/store-memory.command.ts +0 -103
  140. package/src/application/index.ts +0 -36
  141. package/src/application/queries/search-memory.query.ts +0 -237
  142. package/src/application/services/memory-application-service.ts +0 -236
  143. package/src/cache-manager.ts +0 -516
  144. package/src/database-provider.test.ts +0 -364
  145. package/src/database-provider.ts +0 -511
  146. package/src/domain/entities/memory-entry.ts +0 -289
  147. package/src/domain/index.ts +0 -35
  148. package/src/domain/repositories/memory-repository.interface.ts +0 -120
  149. package/src/domain/services/memory-domain-service.ts +0 -403
  150. package/src/hnsw-index.ts +0 -1013
  151. package/src/hybrid-backend.test.ts +0 -399
  152. package/src/hybrid-backend.ts +0 -694
  153. package/src/index.ts +0 -515
  154. package/src/infrastructure/index.ts +0 -23
  155. package/src/infrastructure/repositories/hybrid-memory-repository.ts +0 -516
  156. package/src/migration.ts +0 -669
  157. package/src/query-builder.ts +0 -542
  158. package/src/sqlite-backend.ts +0 -732
  159. package/src/sqljs-backend.ts +0 -763
  160. package/src/types.ts +0 -727
  161. package/tsconfig.json +0 -9
  162. package/tsconfig.tsbuildinfo +0 -1
  163. package/verify-cross-platform.ts +0 -170
@@ -1,516 +0,0 @@
1
- /**
2
- * V3 Cache Manager
3
- *
4
- * High-performance LRU cache with TTL support, memory pressure handling,
5
- * and write-through caching for the unified memory system.
6
- *
7
- * @module v3/memory/cache-manager
8
- */
9
-
10
- import { EventEmitter } from 'node:events';
11
- import {
12
- CacheConfig,
13
- CacheStats,
14
- CachedEntry,
15
- MemoryEntry,
16
- MemoryEvent,
17
- } from './types.js';
18
-
19
- /**
20
- * Doubly-linked list node for LRU implementation
21
- */
22
- interface LRUNode<T> {
23
- key: string;
24
- value: CachedEntry<T>;
25
- prev: LRUNode<T> | null;
26
- next: LRUNode<T> | null;
27
- }
28
-
29
- /**
30
- * High-performance LRU Cache with TTL support
31
- *
32
- * Features:
33
- * - O(1) get, set, delete operations
34
- * - LRU eviction policy
35
- * - TTL-based expiration
36
- * - Memory pressure handling
37
- * - Write-through caching support
38
- * - Performance statistics
39
- */
40
- export class CacheManager<T = MemoryEntry> extends EventEmitter {
41
- private config: CacheConfig;
42
- private cache: Map<string, LRUNode<T>> = new Map();
43
- private head: LRUNode<T> | null = null;
44
- private tail: LRUNode<T> | null = null;
45
- private currentMemory: number = 0;
46
-
47
- // Statistics
48
- private stats: {
49
- hits: number;
50
- misses: number;
51
- evictions: number;
52
- expirations: number;
53
- writes: number;
54
- } = {
55
- hits: 0,
56
- misses: 0,
57
- evictions: 0,
58
- expirations: 0,
59
- writes: 0,
60
- };
61
-
62
- // Cleanup timer
63
- private cleanupInterval: NodeJS.Timeout | null = null;
64
-
65
- constructor(config: Partial<CacheConfig> = {}) {
66
- super();
67
- this.config = this.mergeConfig(config);
68
- this.startCleanupTimer();
69
- }
70
-
71
- /**
72
- * Get a value from the cache
73
- */
74
- get(key: string): T | null {
75
- const node = this.cache.get(key);
76
-
77
- if (!node) {
78
- this.stats.misses++;
79
- this.emit('cache:miss', { key });
80
- return null;
81
- }
82
-
83
- // Check if expired
84
- if (this.isExpired(node.value)) {
85
- this.delete(key);
86
- this.stats.misses++;
87
- this.stats.expirations++;
88
- this.emit('cache:expired', { key });
89
- return null;
90
- }
91
-
92
- // Update access time and count
93
- node.value.lastAccessedAt = Date.now();
94
- node.value.accessCount++;
95
-
96
- // Move to front (most recently used)
97
- this.moveToFront(node);
98
-
99
- this.stats.hits++;
100
- this.emit('cache:hit', { key });
101
-
102
- return node.value.data;
103
- }
104
-
105
- /**
106
- * Set a value in the cache
107
- */
108
- set(key: string, data: T, ttl?: number): void {
109
- const now = Date.now();
110
- const entryTtl = ttl || this.config.ttl;
111
-
112
- // Check if key already exists
113
- const existingNode = this.cache.get(key);
114
- if (existingNode) {
115
- // Update existing entry
116
- existingNode.value.data = data;
117
- existingNode.value.cachedAt = now;
118
- existingNode.value.expiresAt = now + entryTtl;
119
- existingNode.value.lastAccessedAt = now;
120
-
121
- this.moveToFront(existingNode);
122
- this.stats.writes++;
123
- return;
124
- }
125
-
126
- // Calculate memory for new entry
127
- const entryMemory = this.estimateSize(data);
128
-
129
- // Evict entries if needed for memory pressure
130
- if (this.config.maxMemory) {
131
- while (
132
- this.currentMemory + entryMemory > this.config.maxMemory &&
133
- this.cache.size > 0
134
- ) {
135
- this.evictLRU();
136
- }
137
- }
138
-
139
- // Evict entries if at capacity
140
- while (this.cache.size >= this.config.maxSize) {
141
- this.evictLRU();
142
- }
143
-
144
- // Create new node
145
- const cachedEntry: CachedEntry<T> = {
146
- data,
147
- cachedAt: now,
148
- expiresAt: now + entryTtl,
149
- lastAccessedAt: now,
150
- accessCount: 0,
151
- };
152
-
153
- const node: LRUNode<T> = {
154
- key,
155
- value: cachedEntry,
156
- prev: null,
157
- next: null,
158
- };
159
-
160
- // Add to cache
161
- this.cache.set(key, node);
162
- this.addToFront(node);
163
- this.currentMemory += entryMemory;
164
- this.stats.writes++;
165
-
166
- this.emit('cache:set', { key, ttl: entryTtl });
167
- }
168
-
169
- /**
170
- * Delete a value from the cache
171
- */
172
- delete(key: string): boolean {
173
- const node = this.cache.get(key);
174
- if (!node) {
175
- return false;
176
- }
177
-
178
- this.removeNode(node);
179
- this.cache.delete(key);
180
- this.currentMemory -= this.estimateSize(node.value.data);
181
-
182
- this.emit('cache:delete', { key });
183
- return true;
184
- }
185
-
186
- /**
187
- * Check if a key exists in the cache (without affecting LRU order)
188
- */
189
- has(key: string): boolean {
190
- const node = this.cache.get(key);
191
- if (!node) return false;
192
- if (this.isExpired(node.value)) {
193
- this.delete(key);
194
- return false;
195
- }
196
- return true;
197
- }
198
-
199
- /**
200
- * Clear all entries from the cache
201
- */
202
- clear(): void {
203
- this.cache.clear();
204
- this.head = null;
205
- this.tail = null;
206
- this.currentMemory = 0;
207
-
208
- this.emit('cache:cleared', { previousSize: this.cache.size });
209
- }
210
-
211
- /**
212
- * Get cache statistics
213
- */
214
- getStats(): CacheStats {
215
- const total = this.stats.hits + this.stats.misses;
216
- return {
217
- size: this.cache.size,
218
- hitRate: total > 0 ? this.stats.hits / total : 0,
219
- hits: this.stats.hits,
220
- misses: this.stats.misses,
221
- evictions: this.stats.evictions,
222
- memoryUsage: this.currentMemory,
223
- };
224
- }
225
-
226
- /**
227
- * Get all keys in the cache
228
- */
229
- keys(): string[] {
230
- return Array.from(this.cache.keys());
231
- }
232
-
233
- /**
234
- * Get the size of the cache
235
- */
236
- get size(): number {
237
- return this.cache.size;
238
- }
239
-
240
- /**
241
- * Prefetch multiple keys in a single batch
242
- */
243
- async prefetch(
244
- keys: string[],
245
- loader: (keys: string[]) => Promise<Map<string, T>>,
246
- ttl?: number
247
- ): Promise<void> {
248
- const missing = keys.filter((key) => !this.has(key));
249
-
250
- if (missing.length === 0) {
251
- return;
252
- }
253
-
254
- const data = await loader(missing);
255
-
256
- for (const [key, value] of data) {
257
- this.set(key, value, ttl);
258
- }
259
-
260
- this.emit('cache:prefetched', { keys: missing.length });
261
- }
262
-
263
- /**
264
- * Get or set pattern - get from cache or load and cache
265
- */
266
- async getOrSet(
267
- key: string,
268
- loader: () => Promise<T>,
269
- ttl?: number
270
- ): Promise<T> {
271
- const cached = this.get(key);
272
- if (cached !== null) {
273
- return cached;
274
- }
275
-
276
- const data = await loader();
277
- this.set(key, data, ttl);
278
- return data;
279
- }
280
-
281
- /**
282
- * Warm the cache with initial data
283
- */
284
- warmUp(entries: Array<{ key: string; data: T; ttl?: number }>): void {
285
- for (const entry of entries) {
286
- this.set(entry.key, entry.data, entry.ttl);
287
- }
288
- this.emit('cache:warmedUp', { count: entries.length });
289
- }
290
-
291
- /**
292
- * Invalidate entries matching a pattern
293
- */
294
- invalidatePattern(pattern: string | RegExp): number {
295
- const regex = typeof pattern === 'string' ? new RegExp(pattern) : pattern;
296
- let invalidated = 0;
297
-
298
- for (const key of this.cache.keys()) {
299
- if (regex.test(key)) {
300
- this.delete(key);
301
- invalidated++;
302
- }
303
- }
304
-
305
- this.emit('cache:invalidated', { pattern: pattern.toString(), count: invalidated });
306
- return invalidated;
307
- }
308
-
309
- /**
310
- * Shutdown the cache manager
311
- */
312
- shutdown(): void {
313
- if (this.cleanupInterval) {
314
- clearInterval(this.cleanupInterval);
315
- this.cleanupInterval = null;
316
- }
317
- this.clear();
318
- this.emit('cache:shutdown');
319
- }
320
-
321
- // ===== Private Methods =====
322
-
323
- private mergeConfig(config: Partial<CacheConfig>): CacheConfig {
324
- return {
325
- maxSize: config.maxSize || 10000,
326
- ttl: config.ttl || 300000, // 5 minutes default
327
- lruEnabled: config.lruEnabled !== false,
328
- maxMemory: config.maxMemory,
329
- writeThrough: config.writeThrough || false,
330
- };
331
- }
332
-
333
- private isExpired(entry: CachedEntry<T>): boolean {
334
- return Date.now() > entry.expiresAt;
335
- }
336
-
337
- private estimateSize(data: T): number {
338
- try {
339
- return JSON.stringify(data).length * 2; // Rough UTF-16 estimate
340
- } catch {
341
- return 1000; // Default for non-serializable objects
342
- }
343
- }
344
-
345
- private addToFront(node: LRUNode<T>): void {
346
- node.prev = null;
347
- node.next = this.head;
348
-
349
- if (this.head) {
350
- this.head.prev = node;
351
- }
352
-
353
- this.head = node;
354
-
355
- if (!this.tail) {
356
- this.tail = node;
357
- }
358
- }
359
-
360
- private removeNode(node: LRUNode<T>): void {
361
- if (node.prev) {
362
- node.prev.next = node.next;
363
- } else {
364
- this.head = node.next;
365
- }
366
-
367
- if (node.next) {
368
- node.next.prev = node.prev;
369
- } else {
370
- this.tail = node.prev;
371
- }
372
- }
373
-
374
- private moveToFront(node: LRUNode<T>): void {
375
- if (node === this.head) return;
376
-
377
- this.removeNode(node);
378
- this.addToFront(node);
379
- }
380
-
381
- private evictLRU(): void {
382
- if (!this.tail) return;
383
-
384
- const evictedKey = this.tail.key;
385
- const evictedSize = this.estimateSize(this.tail.value.data);
386
-
387
- this.removeNode(this.tail);
388
- this.cache.delete(evictedKey);
389
- this.currentMemory -= evictedSize;
390
- this.stats.evictions++;
391
-
392
- this.emit('cache:eviction', { key: evictedKey });
393
- }
394
-
395
- private startCleanupTimer(): void {
396
- // Clean up expired entries every minute
397
- this.cleanupInterval = setInterval(() => {
398
- this.cleanupExpired();
399
- }, 60000);
400
- }
401
-
402
- private cleanupExpired(): void {
403
- const now = Date.now();
404
- let cleaned = 0;
405
-
406
- for (const [key, node] of this.cache) {
407
- if (node.value.expiresAt < now) {
408
- this.delete(key);
409
- cleaned++;
410
- }
411
- }
412
-
413
- if (cleaned > 0) {
414
- this.emit('cache:cleanup', { expired: cleaned });
415
- }
416
- }
417
- }
418
-
419
- /**
420
- * Multi-layer cache with L1 (memory) and L2 (storage) tiers
421
- */
422
- export class TieredCacheManager<T = MemoryEntry> extends EventEmitter {
423
- private l1Cache: CacheManager<T>;
424
- private l2Loader: ((key: string) => Promise<T | null>) | null = null;
425
- private l2Writer: ((key: string, value: T) => Promise<void>) | null = null;
426
-
427
- constructor(
428
- l1Config: Partial<CacheConfig> = {},
429
- l2Options?: {
430
- loader: (key: string) => Promise<T | null>;
431
- writer?: (key: string, value: T) => Promise<void>;
432
- }
433
- ) {
434
- super();
435
- this.l1Cache = new CacheManager<T>(l1Config);
436
-
437
- if (l2Options) {
438
- this.l2Loader = l2Options.loader;
439
- this.l2Writer = l2Options.writer ?? null;
440
- }
441
-
442
- // Forward L1 events
443
- this.l1Cache.on('cache:hit', (data) => this.emit('l1:hit', data));
444
- this.l1Cache.on('cache:miss', (data) => this.emit('l1:miss', data));
445
- this.l1Cache.on('cache:eviction', (data) => this.emit('l1:eviction', data));
446
- }
447
-
448
- /**
449
- * Get from tiered cache
450
- */
451
- async get(key: string): Promise<T | null> {
452
- // Try L1 first
453
- const l1Result = this.l1Cache.get(key);
454
- if (l1Result !== null) {
455
- return l1Result;
456
- }
457
-
458
- // Try L2 if available
459
- if (this.l2Loader) {
460
- const l2Result = await this.l2Loader(key);
461
- if (l2Result !== null) {
462
- // Promote to L1
463
- this.l1Cache.set(key, l2Result);
464
- this.emit('l2:hit', { key });
465
- return l2Result;
466
- }
467
- this.emit('l2:miss', { key });
468
- }
469
-
470
- return null;
471
- }
472
-
473
- /**
474
- * Set in tiered cache
475
- */
476
- async set(key: string, value: T, ttl?: number): Promise<void> {
477
- // Write to L1
478
- this.l1Cache.set(key, value, ttl);
479
-
480
- // Write-through to L2 if configured
481
- if (this.l2Writer) {
482
- await this.l2Writer(key, value);
483
- this.emit('l2:write', { key });
484
- }
485
- }
486
-
487
- /**
488
- * Delete from tiered cache
489
- */
490
- delete(key: string): boolean {
491
- return this.l1Cache.delete(key);
492
- }
493
-
494
- /**
495
- * Get L1 cache statistics
496
- */
497
- getStats(): CacheStats {
498
- return this.l1Cache.getStats();
499
- }
500
-
501
- /**
502
- * Clear L1 cache
503
- */
504
- clear(): void {
505
- this.l1Cache.clear();
506
- }
507
-
508
- /**
509
- * Shutdown tiered cache
510
- */
511
- shutdown(): void {
512
- this.l1Cache.shutdown();
513
- }
514
- }
515
-
516
- export default CacheManager;