@claude-flow/memory 3.0.0-alpha.1

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 (214) hide show
  1. package/.agentic-flow/intelligence.json +16 -0
  2. package/README.md +249 -0
  3. package/__tests__/coverage/base.css +224 -0
  4. package/__tests__/coverage/block-navigation.js +87 -0
  5. package/__tests__/coverage/coverage-final.json +19 -0
  6. package/__tests__/coverage/favicon.png +0 -0
  7. package/__tests__/coverage/index.html +206 -0
  8. package/__tests__/coverage/lcov-report/base.css +224 -0
  9. package/__tests__/coverage/lcov-report/block-navigation.js +87 -0
  10. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  11. package/__tests__/coverage/lcov-report/index.html +206 -0
  12. package/__tests__/coverage/lcov-report/prettify.css +1 -0
  13. package/__tests__/coverage/lcov-report/prettify.js +2 -0
  14. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  15. package/__tests__/coverage/lcov-report/sorter.js +210 -0
  16. package/__tests__/coverage/lcov-report/src/agentdb-adapter.ts.html +2737 -0
  17. package/__tests__/coverage/lcov-report/src/agentdb-backend.ts.html +3130 -0
  18. package/__tests__/coverage/lcov-report/src/application/commands/delete-memory.command.ts.html +601 -0
  19. package/__tests__/coverage/lcov-report/src/application/commands/index.html +131 -0
  20. package/__tests__/coverage/lcov-report/src/application/commands/store-memory.command.ts.html +394 -0
  21. package/__tests__/coverage/lcov-report/src/application/queries/index.html +116 -0
  22. package/__tests__/coverage/lcov-report/src/application/queries/search-memory.query.ts.html +796 -0
  23. package/__tests__/coverage/lcov-report/src/application/services/index.html +116 -0
  24. package/__tests__/coverage/lcov-report/src/application/services/memory-application-service.ts.html +793 -0
  25. package/__tests__/coverage/lcov-report/src/cache-manager.ts.html +1633 -0
  26. package/__tests__/coverage/lcov-report/src/database-provider.ts.html +1618 -0
  27. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +116 -0
  28. package/__tests__/coverage/lcov-report/src/domain/entities/memory-entry.ts.html +952 -0
  29. package/__tests__/coverage/lcov-report/src/domain/services/index.html +116 -0
  30. package/__tests__/coverage/lcov-report/src/domain/services/memory-domain-service.ts.html +1294 -0
  31. package/__tests__/coverage/lcov-report/src/hnsw-index.ts.html +3124 -0
  32. package/__tests__/coverage/lcov-report/src/hybrid-backend.ts.html +2167 -0
  33. package/__tests__/coverage/lcov-report/src/index.html +266 -0
  34. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/hybrid-memory-repository.ts.html +1633 -0
  35. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/index.html +116 -0
  36. package/__tests__/coverage/lcov-report/src/migration.ts.html +2092 -0
  37. package/__tests__/coverage/lcov-report/src/query-builder.ts.html +1711 -0
  38. package/__tests__/coverage/lcov-report/src/sqlite-backend.ts.html +2281 -0
  39. package/__tests__/coverage/lcov-report/src/sqljs-backend.ts.html +2374 -0
  40. package/__tests__/coverage/lcov-report/src/types.ts.html +2266 -0
  41. package/__tests__/coverage/lcov.info +10238 -0
  42. package/__tests__/coverage/prettify.css +1 -0
  43. package/__tests__/coverage/prettify.js +2 -0
  44. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  45. package/__tests__/coverage/sorter.js +210 -0
  46. package/__tests__/coverage/src/agentdb-adapter.ts.html +2737 -0
  47. package/__tests__/coverage/src/agentdb-backend.ts.html +3130 -0
  48. package/__tests__/coverage/src/application/commands/delete-memory.command.ts.html +601 -0
  49. package/__tests__/coverage/src/application/commands/index.html +131 -0
  50. package/__tests__/coverage/src/application/commands/store-memory.command.ts.html +394 -0
  51. package/__tests__/coverage/src/application/queries/index.html +116 -0
  52. package/__tests__/coverage/src/application/queries/search-memory.query.ts.html +796 -0
  53. package/__tests__/coverage/src/application/services/index.html +116 -0
  54. package/__tests__/coverage/src/application/services/memory-application-service.ts.html +793 -0
  55. package/__tests__/coverage/src/cache-manager.ts.html +1633 -0
  56. package/__tests__/coverage/src/database-provider.ts.html +1618 -0
  57. package/__tests__/coverage/src/domain/entities/index.html +116 -0
  58. package/__tests__/coverage/src/domain/entities/memory-entry.ts.html +952 -0
  59. package/__tests__/coverage/src/domain/services/index.html +116 -0
  60. package/__tests__/coverage/src/domain/services/memory-domain-service.ts.html +1294 -0
  61. package/__tests__/coverage/src/hnsw-index.ts.html +3124 -0
  62. package/__tests__/coverage/src/hybrid-backend.ts.html +2167 -0
  63. package/__tests__/coverage/src/index.html +266 -0
  64. package/__tests__/coverage/src/infrastructure/repositories/hybrid-memory-repository.ts.html +1633 -0
  65. package/__tests__/coverage/src/infrastructure/repositories/index.html +116 -0
  66. package/__tests__/coverage/src/migration.ts.html +2092 -0
  67. package/__tests__/coverage/src/query-builder.ts.html +1711 -0
  68. package/__tests__/coverage/src/sqlite-backend.ts.html +2281 -0
  69. package/__tests__/coverage/src/sqljs-backend.ts.html +2374 -0
  70. package/__tests__/coverage/src/types.ts.html +2266 -0
  71. package/benchmarks/cache-hit-rate.bench.ts +535 -0
  72. package/benchmarks/hnsw-indexing.bench.ts +552 -0
  73. package/benchmarks/memory-write.bench.ts +469 -0
  74. package/benchmarks/vector-search.bench.ts +449 -0
  75. package/dist/agentdb-adapter.d.ts +146 -0
  76. package/dist/agentdb-adapter.d.ts.map +1 -0
  77. package/dist/agentdb-adapter.js +679 -0
  78. package/dist/agentdb-adapter.js.map +1 -0
  79. package/dist/agentdb-backend.d.ts +214 -0
  80. package/dist/agentdb-backend.d.ts.map +1 -0
  81. package/dist/agentdb-backend.js +827 -0
  82. package/dist/agentdb-backend.js.map +1 -0
  83. package/dist/agentdb-backend.test.d.ts +7 -0
  84. package/dist/agentdb-backend.test.d.ts.map +1 -0
  85. package/dist/agentdb-backend.test.js +258 -0
  86. package/dist/agentdb-backend.test.js.map +1 -0
  87. package/dist/application/commands/delete-memory.command.d.ts +65 -0
  88. package/dist/application/commands/delete-memory.command.d.ts.map +1 -0
  89. package/dist/application/commands/delete-memory.command.js +129 -0
  90. package/dist/application/commands/delete-memory.command.js.map +1 -0
  91. package/dist/application/commands/store-memory.command.d.ts +48 -0
  92. package/dist/application/commands/store-memory.command.d.ts.map +1 -0
  93. package/dist/application/commands/store-memory.command.js +72 -0
  94. package/dist/application/commands/store-memory.command.js.map +1 -0
  95. package/dist/application/index.d.ts +12 -0
  96. package/dist/application/index.d.ts.map +1 -0
  97. package/dist/application/index.js +15 -0
  98. package/dist/application/index.js.map +1 -0
  99. package/dist/application/queries/search-memory.query.d.ts +72 -0
  100. package/dist/application/queries/search-memory.query.d.ts.map +1 -0
  101. package/dist/application/queries/search-memory.query.js +143 -0
  102. package/dist/application/queries/search-memory.query.js.map +1 -0
  103. package/dist/application/services/memory-application-service.d.ts +121 -0
  104. package/dist/application/services/memory-application-service.d.ts.map +1 -0
  105. package/dist/application/services/memory-application-service.js +190 -0
  106. package/dist/application/services/memory-application-service.js.map +1 -0
  107. package/dist/cache-manager.d.ts +134 -0
  108. package/dist/cache-manager.d.ts.map +1 -0
  109. package/dist/cache-manager.js +407 -0
  110. package/dist/cache-manager.js.map +1 -0
  111. package/dist/database-provider.d.ts +86 -0
  112. package/dist/database-provider.d.ts.map +1 -0
  113. package/dist/database-provider.js +385 -0
  114. package/dist/database-provider.js.map +1 -0
  115. package/dist/database-provider.test.d.ts +7 -0
  116. package/dist/database-provider.test.d.ts.map +1 -0
  117. package/dist/database-provider.test.js +285 -0
  118. package/dist/database-provider.test.js.map +1 -0
  119. package/dist/domain/entities/memory-entry.d.ts +143 -0
  120. package/dist/domain/entities/memory-entry.d.ts.map +1 -0
  121. package/dist/domain/entities/memory-entry.js +226 -0
  122. package/dist/domain/entities/memory-entry.js.map +1 -0
  123. package/dist/domain/index.d.ts +11 -0
  124. package/dist/domain/index.d.ts.map +1 -0
  125. package/dist/domain/index.js +12 -0
  126. package/dist/domain/index.js.map +1 -0
  127. package/dist/domain/repositories/memory-repository.interface.d.ts +102 -0
  128. package/dist/domain/repositories/memory-repository.interface.d.ts.map +1 -0
  129. package/dist/domain/repositories/memory-repository.interface.js +11 -0
  130. package/dist/domain/repositories/memory-repository.interface.js.map +1 -0
  131. package/dist/domain/services/memory-domain-service.d.ts +105 -0
  132. package/dist/domain/services/memory-domain-service.d.ts.map +1 -0
  133. package/dist/domain/services/memory-domain-service.js +297 -0
  134. package/dist/domain/services/memory-domain-service.js.map +1 -0
  135. package/dist/hnsw-index.d.ts +111 -0
  136. package/dist/hnsw-index.d.ts.map +1 -0
  137. package/dist/hnsw-index.js +781 -0
  138. package/dist/hnsw-index.js.map +1 -0
  139. package/dist/hybrid-backend.d.ts +217 -0
  140. package/dist/hybrid-backend.d.ts.map +1 -0
  141. package/dist/hybrid-backend.js +491 -0
  142. package/dist/hybrid-backend.js.map +1 -0
  143. package/dist/hybrid-backend.test.d.ts +8 -0
  144. package/dist/hybrid-backend.test.d.ts.map +1 -0
  145. package/dist/hybrid-backend.test.js +320 -0
  146. package/dist/hybrid-backend.test.js.map +1 -0
  147. package/dist/index.d.ts +188 -0
  148. package/dist/index.d.ts.map +1 -0
  149. package/dist/index.js +345 -0
  150. package/dist/index.js.map +1 -0
  151. package/dist/infrastructure/index.d.ts +17 -0
  152. package/dist/infrastructure/index.d.ts.map +1 -0
  153. package/dist/infrastructure/index.js +16 -0
  154. package/dist/infrastructure/index.js.map +1 -0
  155. package/dist/infrastructure/repositories/hybrid-memory-repository.d.ts +66 -0
  156. package/dist/infrastructure/repositories/hybrid-memory-repository.d.ts.map +1 -0
  157. package/dist/infrastructure/repositories/hybrid-memory-repository.js +409 -0
  158. package/dist/infrastructure/repositories/hybrid-memory-repository.js.map +1 -0
  159. package/dist/migration.d.ts +68 -0
  160. package/dist/migration.d.ts.map +1 -0
  161. package/dist/migration.js +513 -0
  162. package/dist/migration.js.map +1 -0
  163. package/dist/query-builder.d.ts +211 -0
  164. package/dist/query-builder.d.ts.map +1 -0
  165. package/dist/query-builder.js +438 -0
  166. package/dist/query-builder.js.map +1 -0
  167. package/dist/sqlite-backend.d.ts +121 -0
  168. package/dist/sqlite-backend.d.ts.map +1 -0
  169. package/dist/sqlite-backend.js +564 -0
  170. package/dist/sqlite-backend.js.map +1 -0
  171. package/dist/sqljs-backend.d.ts +128 -0
  172. package/dist/sqljs-backend.d.ts.map +1 -0
  173. package/dist/sqljs-backend.js +598 -0
  174. package/dist/sqljs-backend.js.map +1 -0
  175. package/dist/types.d.ts +481 -0
  176. package/dist/types.d.ts.map +1 -0
  177. package/dist/types.js +58 -0
  178. package/dist/types.js.map +1 -0
  179. package/docs/AGENTDB-INTEGRATION.md +388 -0
  180. package/docs/CROSS_PLATFORM.md +505 -0
  181. package/docs/WINDOWS_SUPPORT.md +422 -0
  182. package/examples/agentdb-example.ts +345 -0
  183. package/examples/cross-platform-usage.ts +326 -0
  184. package/framework/benchmark.ts +112 -0
  185. package/package.json +31 -0
  186. package/src/agentdb-adapter.ts +884 -0
  187. package/src/agentdb-backend.test.ts +339 -0
  188. package/src/agentdb-backend.ts +1016 -0
  189. package/src/application/commands/delete-memory.command.ts +172 -0
  190. package/src/application/commands/store-memory.command.ts +103 -0
  191. package/src/application/index.ts +36 -0
  192. package/src/application/queries/search-memory.query.ts +237 -0
  193. package/src/application/services/memory-application-service.ts +236 -0
  194. package/src/cache-manager.ts +516 -0
  195. package/src/database-provider.test.ts +364 -0
  196. package/src/database-provider.ts +511 -0
  197. package/src/domain/entities/memory-entry.ts +289 -0
  198. package/src/domain/index.ts +35 -0
  199. package/src/domain/repositories/memory-repository.interface.ts +120 -0
  200. package/src/domain/services/memory-domain-service.ts +403 -0
  201. package/src/hnsw-index.ts +1013 -0
  202. package/src/hybrid-backend.test.ts +399 -0
  203. package/src/hybrid-backend.ts +694 -0
  204. package/src/index.ts +515 -0
  205. package/src/infrastructure/index.ts +23 -0
  206. package/src/infrastructure/repositories/hybrid-memory-repository.ts +516 -0
  207. package/src/migration.ts +669 -0
  208. package/src/query-builder.ts +542 -0
  209. package/src/sqlite-backend.ts +732 -0
  210. package/src/sqljs-backend.ts +763 -0
  211. package/src/types.ts +727 -0
  212. package/tsconfig.json +9 -0
  213. package/tsconfig.tsbuildinfo +1 -0
  214. package/verify-cross-platform.ts +170 -0
@@ -0,0 +1,516 @@
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;