@fjell/cache 4.7.0 → 4.7.2

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 (43) hide show
  1. package/CACHE_EVENTS.md +306 -0
  2. package/CACHE_IMPLEMENTATIONS.md +117 -0
  3. package/MEMORY_LEAK_FIXES.md +270 -0
  4. package/README.md +7 -0
  5. package/dist/Aggregator.d.ts +27 -16
  6. package/dist/Cache.d.ts +55 -0
  7. package/dist/CacheContext.d.ts +13 -5
  8. package/dist/CacheMap.d.ts +59 -16
  9. package/dist/CacheStats.d.ts +51 -0
  10. package/dist/Operations.d.ts +20 -17
  11. package/dist/Options.d.ts +1 -3
  12. package/dist/browser/AsyncIndexDBCacheMap.d.ts +1 -1
  13. package/dist/browser/IndexDBCacheMap.d.ts +26 -11
  14. package/dist/browser/LocalStorageCacheMap.d.ts +30 -14
  15. package/dist/browser/SessionStorageCacheMap.d.ts +27 -11
  16. package/dist/events/CacheEventEmitter.d.ts +82 -0
  17. package/dist/events/CacheEventFactory.d.ts +121 -0
  18. package/dist/events/CacheEventTypes.d.ts +122 -0
  19. package/dist/events/index.d.ts +3 -0
  20. package/dist/eviction/EvictionManager.d.ts +57 -0
  21. package/dist/eviction/EvictionStrategy.d.ts +102 -10
  22. package/dist/eviction/index.d.ts +2 -1
  23. package/dist/eviction/strategies/ARCEvictionStrategy.d.ts +10 -5
  24. package/dist/eviction/strategies/FIFOEvictionStrategy.d.ts +6 -5
  25. package/dist/eviction/strategies/LFUEvictionStrategy.d.ts +6 -5
  26. package/dist/eviction/strategies/LRUEvictionStrategy.d.ts +6 -5
  27. package/dist/eviction/strategies/MRUEvictionStrategy.d.ts +6 -5
  28. package/dist/eviction/strategies/RandomEvictionStrategy.d.ts +6 -5
  29. package/dist/eviction/strategies/TwoQueueEvictionStrategy.d.ts +6 -5
  30. package/dist/index.d.ts +9 -3
  31. package/dist/index.js +4739 -2843
  32. package/dist/index.js.map +4 -4
  33. package/dist/memory/EnhancedMemoryCacheMap.d.ts +28 -22
  34. package/dist/memory/MemoryCacheMap.d.ts +26 -11
  35. package/dist/normalization.d.ts +1 -2
  36. package/dist/ops/get.d.ts +1 -0
  37. package/dist/ttl/TTLManager.d.ts +100 -0
  38. package/dist/ttl/index.d.ts +2 -0
  39. package/dist/utils/CacheSize.d.ts +0 -7
  40. package/fix-async-tests.js +116 -0
  41. package/package.json +16 -13
  42. package/debug_test2.js +0 -0
  43. package/debug_test3.js +0 -0
@@ -1,5 +1,6 @@
1
- export { EvictionStrategy, CacheItemMetadata } from './EvictionStrategy';
1
+ export { EvictionStrategy, CacheItemMetadata, CacheMapMetadataProvider, EvictionContext } from './EvictionStrategy';
2
2
  export { createEvictionStrategy } from './EvictionStrategyFactory';
3
+ export { EvictionManager } from './EvictionManager';
3
4
  export { LRUEvictionStrategy } from './strategies/LRUEvictionStrategy';
4
5
  export { LFUEvictionStrategy } from './strategies/LFUEvictionStrategy';
5
6
  export { FIFOEvictionStrategy } from './strategies/FIFOEvictionStrategy';
@@ -1,10 +1,11 @@
1
- import { CacheItemMetadata, EvictionStrategy } from '../EvictionStrategy';
1
+ import { CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from '../EvictionStrategy';
2
2
  import { ARCConfig } from '../EvictionStrategyConfig';
3
3
  /**
4
4
  * ARC (Adaptive Replacement Cache) eviction strategy with enhanced frequency tracking
5
5
  * Balances between recency (LRU) and frequency (LFU) dynamically with sophisticated frequency analysis
6
6
  */
7
7
  export declare class ARCEvictionStrategy extends EvictionStrategy {
8
+ getStrategyName(): string;
8
9
  private recentGhosts;
9
10
  private frequentGhosts;
10
11
  private targetRecentSize;
@@ -12,11 +13,11 @@ export declare class ARCEvictionStrategy extends EvictionStrategy {
12
13
  private readonly maxGhostSize;
13
14
  private lastDecayTime;
14
15
  constructor(maxCacheSize?: number, config?: Partial<ARCConfig>);
15
- selectForEviction(items: Map<string, CacheItemMetadata>): string | null;
16
+ selectForEviction(metadataProvider: CacheMapMetadataProvider, context: EvictionContext): string[];
16
17
  private selectLRUFromItems;
17
- onItemAccessed(key: string, metadata: CacheItemMetadata): void;
18
- onItemAdded(key: string, metadata: CacheItemMetadata): void;
19
- onItemRemoved(key: string): void;
18
+ onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): void;
19
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): void;
20
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): void;
20
21
  /**
21
22
  * Add key to recent ghost list with proper size management
22
23
  */
@@ -29,6 +30,10 @@ export declare class ARCEvictionStrategy extends EvictionStrategy {
29
30
  * Cleanup ghost lists to prevent memory leaks
30
31
  */
31
32
  private cleanupGhostLists;
33
+ /**
34
+ * Enforce size limit on a ghost list by removing oldest entries
35
+ */
36
+ private enforceGhostListSizeLimit;
32
37
  /**
33
38
  * Determine if an item should be classified as frequent vs recent
34
39
  */
@@ -1,11 +1,12 @@
1
- import { CacheItemMetadata, EvictionStrategy } from '../EvictionStrategy';
1
+ import { CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from '../EvictionStrategy';
2
2
  /**
3
3
  * FIFO (First-In, First-Out) eviction strategy
4
4
  * Removes the oldest added item regardless of usage
5
5
  */
6
6
  export declare class FIFOEvictionStrategy extends EvictionStrategy {
7
- selectForEviction(items: Map<string, CacheItemMetadata>): string | null;
8
- onItemAccessed(_key: string, metadata: CacheItemMetadata): void;
9
- onItemAdded(_key: string, metadata: CacheItemMetadata): void;
10
- onItemRemoved(): void;
7
+ selectForEviction(metadataProvider: CacheMapMetadataProvider, context: EvictionContext): string[];
8
+ onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): void;
9
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): void;
10
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): void;
11
+ getStrategyName(): string;
11
12
  }
@@ -1,4 +1,4 @@
1
- import { CacheItemMetadata, EvictionStrategy } from '../EvictionStrategy';
1
+ import { CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from '../EvictionStrategy';
2
2
  import { LFUConfig } from '../EvictionStrategyConfig';
3
3
  /**
4
4
  * LFU (Least Frequently Used) eviction strategy with frequency sketching and decay
@@ -6,14 +6,15 @@ import { LFUConfig } from '../EvictionStrategyConfig';
6
6
  * When configured with default settings, behaves like traditional LFU for backwards compatibility
7
7
  */
8
8
  export declare class LFUEvictionStrategy extends EvictionStrategy {
9
+ getStrategyName(): string;
9
10
  private readonly config;
10
11
  private readonly sketch;
11
12
  private lastDecayTime;
12
13
  constructor(config?: Partial<LFUConfig>);
13
- selectForEviction(items: Map<string, CacheItemMetadata>): string | null;
14
- onItemAccessed(key: string, metadata: CacheItemMetadata): void;
15
- onItemAdded(key: string, metadata: CacheItemMetadata): void;
16
- onItemRemoved(): void;
14
+ selectForEviction(metadataProvider: CacheMapMetadataProvider, context: EvictionContext): string[];
15
+ onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): void;
16
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): void;
17
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): void;
17
18
  /**
18
19
  * Get the effective frequency for an item, applying real-time decay if needed
19
20
  */
@@ -1,11 +1,12 @@
1
- import { CacheItemMetadata, EvictionStrategy } from '../EvictionStrategy';
1
+ import { CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from '../EvictionStrategy';
2
2
  /**
3
3
  * LRU (Least Recently Used) eviction strategy
4
4
  * Removes the item that was accessed longest ago
5
5
  */
6
6
  export declare class LRUEvictionStrategy extends EvictionStrategy {
7
- selectForEviction(items: Map<string, CacheItemMetadata>): string | null;
8
- onItemAccessed(_key: string, metadata: CacheItemMetadata): void;
9
- onItemAdded(_key: string, metadata: CacheItemMetadata): void;
10
- onItemRemoved(): void;
7
+ selectForEviction(metadataProvider: CacheMapMetadataProvider, context: EvictionContext): string[];
8
+ onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): void;
9
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): void;
10
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): void;
11
+ getStrategyName(): string;
11
12
  }
@@ -1,11 +1,12 @@
1
- import { CacheItemMetadata, EvictionStrategy } from '../EvictionStrategy';
1
+ import { CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from '../EvictionStrategy';
2
2
  /**
3
3
  * MRU (Most Recently Used) eviction strategy
4
4
  * Removes the most recently accessed item
5
5
  */
6
6
  export declare class MRUEvictionStrategy extends EvictionStrategy {
7
- selectForEviction(items: Map<string, CacheItemMetadata>): string | null;
8
- onItemAccessed(_key: string, metadata: CacheItemMetadata): void;
9
- onItemAdded(_key: string, metadata: CacheItemMetadata): void;
10
- onItemRemoved(): void;
7
+ getStrategyName(): string;
8
+ selectForEviction(metadataProvider: CacheMapMetadataProvider, context: EvictionContext): string[];
9
+ onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): void;
10
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): void;
11
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): void;
11
12
  }
@@ -1,11 +1,12 @@
1
- import { CacheItemMetadata, EvictionStrategy } from '../EvictionStrategy';
1
+ import { CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from '../EvictionStrategy';
2
2
  /**
3
3
  * Random eviction strategy
4
4
  * Removes a random item from the cache
5
5
  */
6
6
  export declare class RandomEvictionStrategy extends EvictionStrategy {
7
- selectForEviction(items: Map<string, CacheItemMetadata>): string | null;
8
- onItemAccessed(_key: string, metadata: CacheItemMetadata): void;
9
- onItemAdded(_key: string, metadata: CacheItemMetadata): void;
10
- onItemRemoved(): void;
7
+ selectForEviction(metadataProvider: CacheMapMetadataProvider, context: EvictionContext): string[];
8
+ onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): void;
9
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): void;
10
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): void;
11
+ getStrategyName(): string;
11
12
  }
@@ -1,4 +1,4 @@
1
- import { CacheItemMetadata, EvictionStrategy } from '../EvictionStrategy';
1
+ import { CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from '../EvictionStrategy';
2
2
  import { TwoQueueConfig } from '../EvictionStrategyConfig';
3
3
  /**
4
4
  * 2Q (Two Queues) eviction strategy with enhanced frequency tracking
@@ -6,6 +6,7 @@ import { TwoQueueConfig } from '../EvictionStrategyConfig';
6
6
  * Uses frequency analysis for promotion decisions and weighted LRU in hot queue
7
7
  */
8
8
  export declare class TwoQueueEvictionStrategy extends EvictionStrategy {
9
+ getStrategyName(): string;
9
10
  private recentQueue;
10
11
  private hotQueue;
11
12
  private ghostQueue;
@@ -14,7 +15,7 @@ export declare class TwoQueueEvictionStrategy extends EvictionStrategy {
14
15
  private readonly maxGhostSize;
15
16
  private lastDecayTime;
16
17
  constructor(maxCacheSize?: number, config?: Partial<TwoQueueConfig>);
17
- selectForEviction(items: Map<string, CacheItemMetadata>): string | null;
18
+ selectForEviction(metadataProvider: CacheMapMetadataProvider, context: EvictionContext): string[];
18
19
  /**
19
20
  * Select eviction candidate from hot queue using traditional LRU
20
21
  */
@@ -23,9 +24,9 @@ export declare class TwoQueueEvictionStrategy extends EvictionStrategy {
23
24
  * Select eviction candidate from hot queue using frequency-weighted LRU
24
25
  */
25
26
  private selectFromHotQueueFrequencyWeighted;
26
- onItemAccessed(key: string, metadata: CacheItemMetadata): void;
27
- onItemAdded(key: string, metadata: CacheItemMetadata): void;
28
- onItemRemoved(key: string): void;
27
+ onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): void;
28
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): void;
29
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): void;
29
30
  /**
30
31
  * Determine if an item should be promoted from recent to hot queue
31
32
  */
package/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  export { createCache, isCache } from './Cache';
2
- export type { Cache } from './Cache';
2
+ export type { Cache, CacheInfo } from './Cache';
3
3
  export { CacheMap } from './CacheMap';
4
+ export { CacheStatsManager } from './CacheStats';
5
+ export type { CacheStats } from './CacheStats';
4
6
  export { MemoryCacheMap } from './memory/MemoryCacheMap';
5
7
  export { EnhancedMemoryCacheMap } from './memory/EnhancedMemoryCacheMap';
6
8
  export { LocalStorageCacheMap } from './browser/LocalStorageCacheMap';
@@ -15,10 +17,14 @@ export { createInstance, isInstance } from './Instance';
15
17
  export type { Instance } from './Instance';
16
18
  export { normalizeKeyValue, createNormalizedHashFunction, isLocKeyArrayEqual, normalizeLocKeyItem } from './normalization';
17
19
  export { parseSizeString, formatBytes, estimateValueSize, validateSizeConfig } from './utils/CacheSize';
18
- export { createEvictionStrategy } from './eviction';
19
- export type { CacheItemMetadata, EvictionStrategy } from './eviction';
20
+ export { createEvictionStrategy, EvictionManager } from './eviction';
21
+ export type { CacheItemMetadata, CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from './eviction';
22
+ export { TTLManager } from './ttl';
23
+ export type { TTLConfig, TTLItemMetadata } from './ttl';
20
24
  export { validateEvictionStrategyConfig, validateLFUConfig, validateARCConfig, validateTwoQueueConfig, createValidatedConfig } from './eviction/EvictionStrategyValidation';
21
25
  export { createOperations } from './Operations';
22
26
  export type { Operations } from './Operations';
27
+ export * from './events';
23
28
  export { createAggregator, toCacheConfig } from './Aggregator';
24
29
  export type { Aggregator, CacheConfig, AggregateConfig } from './Aggregator';
30
+ export * from './Registry';