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

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 (156) 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/index.d.ts +8 -0
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +8 -0
  29. package/dist/index.js.map +1 -1
  30. package/dist/learning-bridge.d.ts +137 -0
  31. package/dist/learning-bridge.d.ts.map +1 -0
  32. package/dist/learning-bridge.js +335 -0
  33. package/dist/learning-bridge.js.map +1 -0
  34. package/dist/learning-bridge.test.d.ts +8 -0
  35. package/dist/learning-bridge.test.d.ts.map +1 -0
  36. package/dist/learning-bridge.test.js +578 -0
  37. package/dist/learning-bridge.test.js.map +1 -0
  38. package/dist/memory-graph.d.ts +100 -0
  39. package/dist/memory-graph.d.ts.map +1 -0
  40. package/dist/memory-graph.js +333 -0
  41. package/dist/memory-graph.js.map +1 -0
  42. package/dist/memory-graph.test.d.ts +8 -0
  43. package/dist/memory-graph.test.d.ts.map +1 -0
  44. package/dist/memory-graph.test.js +609 -0
  45. package/dist/memory-graph.test.js.map +1 -0
  46. package/dist/types.d.ts +3 -0
  47. package/dist/types.d.ts.map +1 -1
  48. package/package.json +19 -4
  49. package/.agentic-flow/intelligence.json +0 -16
  50. package/__tests__/coverage/base.css +0 -224
  51. package/__tests__/coverage/block-navigation.js +0 -87
  52. package/__tests__/coverage/coverage-final.json +0 -19
  53. package/__tests__/coverage/favicon.png +0 -0
  54. package/__tests__/coverage/index.html +0 -206
  55. package/__tests__/coverage/lcov-report/base.css +0 -224
  56. package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
  57. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  58. package/__tests__/coverage/lcov-report/index.html +0 -206
  59. package/__tests__/coverage/lcov-report/prettify.css +0 -1
  60. package/__tests__/coverage/lcov-report/prettify.js +0 -2
  61. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  62. package/__tests__/coverage/lcov-report/sorter.js +0 -210
  63. package/__tests__/coverage/lcov-report/src/agentdb-adapter.ts.html +0 -2737
  64. package/__tests__/coverage/lcov-report/src/agentdb-backend.ts.html +0 -3130
  65. package/__tests__/coverage/lcov-report/src/application/commands/delete-memory.command.ts.html +0 -601
  66. package/__tests__/coverage/lcov-report/src/application/commands/index.html +0 -131
  67. package/__tests__/coverage/lcov-report/src/application/commands/store-memory.command.ts.html +0 -394
  68. package/__tests__/coverage/lcov-report/src/application/queries/index.html +0 -116
  69. package/__tests__/coverage/lcov-report/src/application/queries/search-memory.query.ts.html +0 -796
  70. package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
  71. package/__tests__/coverage/lcov-report/src/application/services/memory-application-service.ts.html +0 -793
  72. package/__tests__/coverage/lcov-report/src/cache-manager.ts.html +0 -1633
  73. package/__tests__/coverage/lcov-report/src/database-provider.ts.html +0 -1618
  74. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
  75. package/__tests__/coverage/lcov-report/src/domain/entities/memory-entry.ts.html +0 -952
  76. package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
  77. package/__tests__/coverage/lcov-report/src/domain/services/memory-domain-service.ts.html +0 -1294
  78. package/__tests__/coverage/lcov-report/src/hnsw-index.ts.html +0 -3124
  79. package/__tests__/coverage/lcov-report/src/hybrid-backend.ts.html +0 -2167
  80. package/__tests__/coverage/lcov-report/src/index.html +0 -266
  81. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/hybrid-memory-repository.ts.html +0 -1633
  82. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/index.html +0 -116
  83. package/__tests__/coverage/lcov-report/src/migration.ts.html +0 -2092
  84. package/__tests__/coverage/lcov-report/src/query-builder.ts.html +0 -1711
  85. package/__tests__/coverage/lcov-report/src/sqlite-backend.ts.html +0 -2281
  86. package/__tests__/coverage/lcov-report/src/sqljs-backend.ts.html +0 -2374
  87. package/__tests__/coverage/lcov-report/src/types.ts.html +0 -2266
  88. package/__tests__/coverage/lcov.info +0 -10238
  89. package/__tests__/coverage/prettify.css +0 -1
  90. package/__tests__/coverage/prettify.js +0 -2
  91. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  92. package/__tests__/coverage/sorter.js +0 -210
  93. package/__tests__/coverage/src/agentdb-adapter.ts.html +0 -2737
  94. package/__tests__/coverage/src/agentdb-backend.ts.html +0 -3130
  95. package/__tests__/coverage/src/application/commands/delete-memory.command.ts.html +0 -601
  96. package/__tests__/coverage/src/application/commands/index.html +0 -131
  97. package/__tests__/coverage/src/application/commands/store-memory.command.ts.html +0 -394
  98. package/__tests__/coverage/src/application/queries/index.html +0 -116
  99. package/__tests__/coverage/src/application/queries/search-memory.query.ts.html +0 -796
  100. package/__tests__/coverage/src/application/services/index.html +0 -116
  101. package/__tests__/coverage/src/application/services/memory-application-service.ts.html +0 -793
  102. package/__tests__/coverage/src/cache-manager.ts.html +0 -1633
  103. package/__tests__/coverage/src/database-provider.ts.html +0 -1618
  104. package/__tests__/coverage/src/domain/entities/index.html +0 -116
  105. package/__tests__/coverage/src/domain/entities/memory-entry.ts.html +0 -952
  106. package/__tests__/coverage/src/domain/services/index.html +0 -116
  107. package/__tests__/coverage/src/domain/services/memory-domain-service.ts.html +0 -1294
  108. package/__tests__/coverage/src/hnsw-index.ts.html +0 -3124
  109. package/__tests__/coverage/src/hybrid-backend.ts.html +0 -2167
  110. package/__tests__/coverage/src/index.html +0 -266
  111. package/__tests__/coverage/src/infrastructure/repositories/hybrid-memory-repository.ts.html +0 -1633
  112. package/__tests__/coverage/src/infrastructure/repositories/index.html +0 -116
  113. package/__tests__/coverage/src/migration.ts.html +0 -2092
  114. package/__tests__/coverage/src/query-builder.ts.html +0 -1711
  115. package/__tests__/coverage/src/sqlite-backend.ts.html +0 -2281
  116. package/__tests__/coverage/src/sqljs-backend.ts.html +0 -2374
  117. package/__tests__/coverage/src/types.ts.html +0 -2266
  118. package/benchmarks/cache-hit-rate.bench.ts +0 -535
  119. package/benchmarks/hnsw-indexing.bench.ts +0 -552
  120. package/benchmarks/memory-write.bench.ts +0 -469
  121. package/benchmarks/vector-search.bench.ts +0 -449
  122. package/docs/AGENTDB-INTEGRATION.md +0 -388
  123. package/docs/CROSS_PLATFORM.md +0 -505
  124. package/docs/WINDOWS_SUPPORT.md +0 -422
  125. package/examples/agentdb-example.ts +0 -345
  126. package/examples/cross-platform-usage.ts +0 -326
  127. package/framework/benchmark.ts +0 -112
  128. package/src/agentdb-adapter.ts +0 -884
  129. package/src/agentdb-backend.test.ts +0 -339
  130. package/src/agentdb-backend.ts +0 -1016
  131. package/src/application/commands/delete-memory.command.ts +0 -172
  132. package/src/application/commands/store-memory.command.ts +0 -103
  133. package/src/application/index.ts +0 -36
  134. package/src/application/queries/search-memory.query.ts +0 -237
  135. package/src/application/services/memory-application-service.ts +0 -236
  136. package/src/cache-manager.ts +0 -516
  137. package/src/database-provider.test.ts +0 -364
  138. package/src/database-provider.ts +0 -511
  139. package/src/domain/entities/memory-entry.ts +0 -289
  140. package/src/domain/index.ts +0 -35
  141. package/src/domain/repositories/memory-repository.interface.ts +0 -120
  142. package/src/domain/services/memory-domain-service.ts +0 -403
  143. package/src/hnsw-index.ts +0 -1013
  144. package/src/hybrid-backend.test.ts +0 -399
  145. package/src/hybrid-backend.ts +0 -694
  146. package/src/index.ts +0 -515
  147. package/src/infrastructure/index.ts +0 -23
  148. package/src/infrastructure/repositories/hybrid-memory-repository.ts +0 -516
  149. package/src/migration.ts +0 -669
  150. package/src/query-builder.ts +0 -542
  151. package/src/sqlite-backend.ts +0 -732
  152. package/src/sqljs-backend.ts +0 -763
  153. package/src/types.ts +0 -727
  154. package/tsconfig.json +0 -9
  155. package/tsconfig.tsbuildinfo +0 -1
  156. package/verify-cross-platform.ts +0 -170
@@ -1,535 +0,0 @@
1
- /**
2
- * Cache Hit Rate Benchmark
3
- *
4
- * Target: <0.1ms for cache hits
5
- *
6
- * Measures cache performance including hit rate,
7
- * lookup time, and eviction strategies.
8
- */
9
-
10
- import { benchmark, BenchmarkRunner, formatTime, meetsTarget } from '../framework/benchmark.js';
11
-
12
- // ============================================================================
13
- // Cache Implementations
14
- // ============================================================================
15
-
16
- /**
17
- * Simple LRU Cache implementation
18
- */
19
- class LRUCache<K, V> {
20
- private cache = new Map<K, V>();
21
- private maxSize: number;
22
- private hits = 0;
23
- private misses = 0;
24
-
25
- constructor(maxSize: number) {
26
- this.maxSize = maxSize;
27
- }
28
-
29
- get(key: K): V | undefined {
30
- const value = this.cache.get(key);
31
- if (value !== undefined) {
32
- // Move to end (most recently used)
33
- this.cache.delete(key);
34
- this.cache.set(key, value);
35
- this.hits++;
36
- return value;
37
- }
38
- this.misses++;
39
- return undefined;
40
- }
41
-
42
- set(key: K, value: V): void {
43
- if (this.cache.has(key)) {
44
- this.cache.delete(key);
45
- } else if (this.cache.size >= this.maxSize) {
46
- // Evict oldest (first) entry
47
- const firstKey = this.cache.keys().next().value;
48
- this.cache.delete(firstKey);
49
- }
50
- this.cache.set(key, value);
51
- }
52
-
53
- get hitRate(): number {
54
- const total = this.hits + this.misses;
55
- return total > 0 ? this.hits / total : 0;
56
- }
57
-
58
- get size(): number {
59
- return this.cache.size;
60
- }
61
-
62
- clear(): void {
63
- this.cache.clear();
64
- this.hits = 0;
65
- this.misses = 0;
66
- }
67
- }
68
-
69
- /**
70
- * TTL Cache with expiration
71
- */
72
- class TTLCache<K, V> {
73
- private cache = new Map<K, { value: V; expiry: number }>();
74
- private defaultTTL: number;
75
- private hits = 0;
76
- private misses = 0;
77
-
78
- constructor(defaultTTL: number = 60000) {
79
- this.defaultTTL = defaultTTL;
80
- }
81
-
82
- get(key: K): V | undefined {
83
- const entry = this.cache.get(key);
84
- if (entry !== undefined) {
85
- if (Date.now() < entry.expiry) {
86
- this.hits++;
87
- return entry.value;
88
- }
89
- // Expired
90
- this.cache.delete(key);
91
- }
92
- this.misses++;
93
- return undefined;
94
- }
95
-
96
- set(key: K, value: V, ttl?: number): void {
97
- const expiry = Date.now() + (ttl || this.defaultTTL);
98
- this.cache.set(key, { value, expiry });
99
- }
100
-
101
- get hitRate(): number {
102
- const total = this.hits + this.misses;
103
- return total > 0 ? this.hits / total : 0;
104
- }
105
-
106
- get size(): number {
107
- return this.cache.size;
108
- }
109
-
110
- cleanup(): number {
111
- const now = Date.now();
112
- let cleaned = 0;
113
- for (const [key, entry] of this.cache) {
114
- if (now >= entry.expiry) {
115
- this.cache.delete(key);
116
- cleaned++;
117
- }
118
- }
119
- return cleaned;
120
- }
121
-
122
- clear(): void {
123
- this.cache.clear();
124
- this.hits = 0;
125
- this.misses = 0;
126
- }
127
- }
128
-
129
- /**
130
- * Two-level cache (L1 fast, L2 larger)
131
- */
132
- class TwoLevelCache<K, V> {
133
- private l1: LRUCache<K, V>;
134
- private l2: LRUCache<K, V>;
135
- private l1Hits = 0;
136
- private l2Hits = 0;
137
- private misses = 0;
138
-
139
- constructor(l1Size: number = 100, l2Size: number = 1000) {
140
- this.l1 = new LRUCache<K, V>(l1Size);
141
- this.l2 = new LRUCache<K, V>(l2Size);
142
- }
143
-
144
- get(key: K): V | undefined {
145
- // Check L1 first
146
- let value = this.l1.get(key);
147
- if (value !== undefined) {
148
- this.l1Hits++;
149
- return value;
150
- }
151
-
152
- // Check L2
153
- value = this.l2.get(key);
154
- if (value !== undefined) {
155
- this.l2Hits++;
156
- // Promote to L1
157
- this.l1.set(key, value);
158
- return value;
159
- }
160
-
161
- this.misses++;
162
- return undefined;
163
- }
164
-
165
- set(key: K, value: V): void {
166
- this.l1.set(key, value);
167
- this.l2.set(key, value);
168
- }
169
-
170
- get stats(): { l1HitRate: number; l2HitRate: number; missRate: number } {
171
- const total = this.l1Hits + this.l2Hits + this.misses;
172
- return {
173
- l1HitRate: total > 0 ? this.l1Hits / total : 0,
174
- l2HitRate: total > 0 ? this.l2Hits / total : 0,
175
- missRate: total > 0 ? this.misses / total : 0,
176
- };
177
- }
178
-
179
- clear(): void {
180
- this.l1.clear();
181
- this.l2.clear();
182
- this.l1Hits = 0;
183
- this.l2Hits = 0;
184
- this.misses = 0;
185
- }
186
- }
187
-
188
- /**
189
- * Memoization cache for function results
190
- */
191
- class MemoCache<T extends (...args: unknown[]) => unknown> {
192
- private cache = new Map<string, ReturnType<T>>();
193
- private fn: T;
194
-
195
- constructor(fn: T) {
196
- this.fn = fn;
197
- }
198
-
199
- call(...args: Parameters<T>): ReturnType<T> {
200
- const key = JSON.stringify(args);
201
- if (this.cache.has(key)) {
202
- return this.cache.get(key)!;
203
- }
204
- const result = this.fn(...args) as ReturnType<T>;
205
- this.cache.set(key, result);
206
- return result;
207
- }
208
-
209
- get size(): number {
210
- return this.cache.size;
211
- }
212
-
213
- clear(): void {
214
- this.cache.clear();
215
- }
216
- }
217
-
218
- // ============================================================================
219
- // Benchmark Suite
220
- // ============================================================================
221
-
222
- export async function runCacheHitRateBenchmarks(): Promise<void> {
223
- const runner = new BenchmarkRunner('Cache Hit Rate');
224
-
225
- console.log('\n--- Cache Hit Rate Benchmarks ---\n');
226
-
227
- // Prepare test data
228
- const testKeys = Array.from({ length: 10000 }, (_, i) => `key-${i}`);
229
- const testValues = testKeys.map((k) => ({ key: k, data: Math.random() }));
230
-
231
- // Benchmark 1: LRU Cache - Cache Hit
232
- const lruCache = new LRUCache<string, object>(1000);
233
-
234
- // Pre-populate cache
235
- for (let i = 0; i < 1000; i++) {
236
- lruCache.set(testKeys[i]!, testValues[i]!);
237
- }
238
-
239
- const lruHitResult = await runner.run(
240
- 'lru-cache-hit',
241
- async () => {
242
- lruCache.get(testKeys[500]!);
243
- },
244
- { iterations: 100000 }
245
- );
246
-
247
- console.log(`LRU Cache Hit: ${formatTime(lruHitResult.mean)}`);
248
- const hitTarget = meetsTarget('cache-hit', lruHitResult.mean);
249
- console.log(` Target (<0.1ms): ${hitTarget.met ? 'PASS' : 'FAIL'}`);
250
-
251
- // Benchmark 2: LRU Cache - Cache Miss
252
- const lruMissResult = await runner.run(
253
- 'lru-cache-miss',
254
- async () => {
255
- lruCache.get('non-existent-key');
256
- },
257
- { iterations: 100000 }
258
- );
259
-
260
- console.log(`LRU Cache Miss: ${formatTime(lruMissResult.mean)}`);
261
-
262
- // Benchmark 3: LRU Cache - Set Operation
263
- const lruSetResult = await runner.run(
264
- 'lru-cache-set',
265
- async () => {
266
- lruCache.set(`new-key-${Date.now()}`, { data: 'test' });
267
- },
268
- { iterations: 10000 }
269
- );
270
-
271
- console.log(`LRU Cache Set: ${formatTime(lruSetResult.mean)}`);
272
-
273
- // Benchmark 4: TTL Cache - Hit
274
- const ttlCache = new TTLCache<string, object>(60000);
275
-
276
- for (let i = 0; i < 1000; i++) {
277
- ttlCache.set(testKeys[i]!, testValues[i]!);
278
- }
279
-
280
- const ttlHitResult = await runner.run(
281
- 'ttl-cache-hit',
282
- async () => {
283
- ttlCache.get(testKeys[500]!);
284
- },
285
- { iterations: 100000 }
286
- );
287
-
288
- console.log(`TTL Cache Hit: ${formatTime(ttlHitResult.mean)}`);
289
-
290
- // Benchmark 5: TTL Cache - Cleanup
291
- // Add some expired entries
292
- for (let i = 0; i < 100; i++) {
293
- ttlCache.set(`expired-${i}`, { data: i }, -1); // Already expired
294
- }
295
-
296
- const ttlCleanupResult = await runner.run(
297
- 'ttl-cache-cleanup',
298
- async () => {
299
- ttlCache.cleanup();
300
- },
301
- { iterations: 1000 }
302
- );
303
-
304
- console.log(`TTL Cache Cleanup: ${formatTime(ttlCleanupResult.mean)}`);
305
-
306
- // Benchmark 6: Two-Level Cache - L1 Hit
307
- const twoLevelCache = new TwoLevelCache<string, object>(100, 1000);
308
-
309
- for (let i = 0; i < 1000; i++) {
310
- twoLevelCache.set(testKeys[i]!, testValues[i]!);
311
- }
312
-
313
- // Warm up L1
314
- for (let i = 0; i < 50; i++) {
315
- twoLevelCache.get(testKeys[i]!);
316
- }
317
-
318
- const l1HitResult = await runner.run(
319
- 'two-level-cache-l1-hit',
320
- async () => {
321
- twoLevelCache.get(testKeys[25]!);
322
- },
323
- { iterations: 100000 }
324
- );
325
-
326
- console.log(`Two-Level Cache L1 Hit: ${formatTime(l1HitResult.mean)}`);
327
-
328
- // Benchmark 7: Two-Level Cache - L2 Hit (promotes to L1)
329
- const l2HitResult = await runner.run(
330
- 'two-level-cache-l2-hit',
331
- async () => {
332
- twoLevelCache.get(testKeys[500]!);
333
- },
334
- { iterations: 50000 }
335
- );
336
-
337
- console.log(`Two-Level Cache L2 Hit: ${formatTime(l2HitResult.mean)}`);
338
-
339
- // Benchmark 8: Memoization Cache
340
- const expensiveFn = (n: number): number => {
341
- let result = 0;
342
- for (let i = 0; i < n; i++) {
343
- result += Math.sqrt(i);
344
- }
345
- return result;
346
- };
347
-
348
- const memoCache = new MemoCache(expensiveFn);
349
-
350
- // Prime the cache
351
- memoCache.call(1000);
352
-
353
- const memoHitResult = await runner.run(
354
- 'memo-cache-hit',
355
- async () => {
356
- memoCache.call(1000);
357
- },
358
- { iterations: 100000 }
359
- );
360
-
361
- console.log(`Memo Cache Hit: ${formatTime(memoHitResult.mean)}`);
362
-
363
- const memoMissResult = await runner.run(
364
- 'memo-cache-miss',
365
- async () => {
366
- memoCache.call(Math.floor(Math.random() * 10000000));
367
- },
368
- { iterations: 100 }
369
- );
370
-
371
- console.log(`Memo Cache Miss (compute): ${formatTime(memoMissResult.mean)}`);
372
-
373
- // Benchmark 9: Cache with Different Hit Rates
374
- const cache90 = new LRUCache<string, object>(1000);
375
- for (let i = 0; i < 1000; i++) {
376
- cache90.set(testKeys[i]!, testValues[i]!);
377
- }
378
-
379
- const hitRate90Result = await runner.run(
380
- 'workload-90-percent-hits',
381
- async () => {
382
- const isHit = Math.random() < 0.9;
383
- if (isHit) {
384
- cache90.get(testKeys[Math.floor(Math.random() * 1000)]!);
385
- } else {
386
- cache90.get(`miss-${Date.now()}`);
387
- }
388
- },
389
- { iterations: 10000 }
390
- );
391
-
392
- console.log(`90% Hit Rate Workload: ${formatTime(hitRate90Result.mean)}`);
393
-
394
- // Benchmark 10: Cache Eviction Under Pressure
395
- const smallCache = new LRUCache<string, object>(100);
396
-
397
- const evictionResult = await runner.run(
398
- 'cache-with-eviction',
399
- async () => {
400
- // Write 200 items to a cache of size 100
401
- for (let i = 0; i < 200; i++) {
402
- smallCache.set(`evict-key-${i}`, { data: i });
403
- }
404
- },
405
- { iterations: 100 }
406
- );
407
-
408
- console.log(`Cache with Eviction (200 to 100): ${formatTime(evictionResult.mean)}`);
409
-
410
- // Summary
411
- console.log('\n--- Summary ---');
412
- console.log(`LRU hit: ${formatTime(lruHitResult.mean)} (${lruHitResult.opsPerSecond.toFixed(0)} ops/sec)`);
413
- console.log(`LRU miss: ${formatTime(lruMissResult.mean)}`);
414
- console.log(`TTL hit: ${formatTime(ttlHitResult.mean)}`);
415
- console.log(`L1 hit: ${formatTime(l1HitResult.mean)}`);
416
- console.log(`L2 hit: ${formatTime(l2HitResult.mean)}`);
417
- console.log(`Memo hit: ${formatTime(memoHitResult.mean)}`);
418
- console.log(`\nTwo-Level Cache Stats:`, twoLevelCache.stats);
419
-
420
- // Print full results
421
- runner.printResults();
422
- }
423
-
424
- // ============================================================================
425
- // Cache Optimization Strategies
426
- // ============================================================================
427
-
428
- export const cacheOptimizations = {
429
- /**
430
- * Optimal cache sizing
431
- */
432
- optimalSizing: {
433
- description: 'Size cache based on working set and memory constraints',
434
- expectedImprovement: '20-50% hit rate',
435
- implementation: `
436
- function calculateOptimalCacheSize(workingSetSize: number, memoryLimit: number): number {
437
- const avgItemSize = estimateAverageItemSize();
438
- const maxItems = Math.floor(memoryLimit / avgItemSize);
439
- return Math.min(workingSetSize * 0.8, maxItems);
440
- }
441
- `,
442
- },
443
-
444
- /**
445
- * Adaptive TTL
446
- */
447
- adaptiveTTL: {
448
- description: 'Adjust TTL based on access patterns',
449
- expectedImprovement: '10-30% hit rate',
450
- implementation: `
451
- class AdaptiveTTLCache {
452
- private accessCounts = new Map<string, number>();
453
-
454
- getTTL(key: string): number {
455
- const count = this.accessCounts.get(key) || 0;
456
- // Hot keys get longer TTL
457
- return this.baseTTL * Math.min(10, 1 + Math.log2(count + 1));
458
- }
459
- }
460
- `,
461
- },
462
-
463
- /**
464
- * Probabilistic early expiration
465
- */
466
- probabilisticExpiry: {
467
- description: 'Refresh items before expiry to avoid thundering herd',
468
- expectedImprovement: 'Reduces load spikes by 80%',
469
- implementation: `
470
- function shouldRefresh(ttl: number, remaining: number): boolean {
471
- const beta = 1.0;
472
- const delta = ttl - remaining;
473
- const xFetch = Date.now() + delta * beta * Math.log(Math.random());
474
- return xFetch > Date.now() + remaining;
475
- }
476
- `,
477
- },
478
-
479
- /**
480
- * Segmented cache
481
- */
482
- segmentedCache: {
483
- description: 'Reduce lock contention with segmented caches',
484
- expectedImprovement: '2-4x concurrent performance',
485
- implementation: `
486
- class SegmentedCache<K, V> {
487
- private segments: LRUCache<K, V>[];
488
-
489
- constructor(numSegments: number, sizePerSegment: number) {
490
- this.segments = Array.from(
491
- { length: numSegments },
492
- () => new LRUCache<K, V>(sizePerSegment)
493
- );
494
- }
495
-
496
- private getSegment(key: K): LRUCache<K, V> {
497
- const hash = this.hash(key);
498
- return this.segments[hash % this.segments.length]!;
499
- }
500
- }
501
- `,
502
- },
503
-
504
- /**
505
- * Read-through caching
506
- */
507
- readThrough: {
508
- description: 'Automatically fetch missing items',
509
- expectedImprovement: 'Simplifies code, consistent performance',
510
- implementation: `
511
- class ReadThroughCache<K, V> {
512
- constructor(
513
- private cache: LRUCache<K, V>,
514
- private loader: (key: K) => Promise<V>
515
- ) {}
516
-
517
- async get(key: K): Promise<V> {
518
- let value = this.cache.get(key);
519
- if (value === undefined) {
520
- value = await this.loader(key);
521
- this.cache.set(key, value);
522
- }
523
- return value;
524
- }
525
- }
526
- `,
527
- },
528
- };
529
-
530
- // Run if executed directly
531
- if (import.meta.url === `file://${process.argv[1]}`) {
532
- runCacheHitRateBenchmarks().catch(console.error);
533
- }
534
-
535
- export default runCacheHitRateBenchmarks;