@fjell/cache 4.7.0 → 4.7.4

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 +67 -24
  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 +19 -2
  13. package/dist/browser/IndexDBCacheMap.d.ts +49 -36
  14. package/dist/browser/LocalStorageCacheMap.d.ts +38 -22
  15. package/dist/browser/SessionStorageCacheMap.d.ts +35 -19
  16. package/dist/events/CacheEventEmitter.d.ts +83 -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 +4959 -2920
  32. package/dist/index.js.map +4 -4
  33. package/dist/memory/EnhancedMemoryCacheMap.d.ts +36 -30
  34. package/dist/memory/MemoryCacheMap.d.ts +34 -19
  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 +19 -16
  42. package/debug_test2.js +0 -0
  43. package/debug_test3.js +0 -0
@@ -18,33 +18,125 @@ export interface CacheItemMetadata {
18
18
  lastFrequencyUpdate?: number;
19
19
  /** Raw frequency count before decay */
20
20
  rawFrequency?: number;
21
+ /** Additional strategy-specific metadata */
22
+ strategyData?: {
23
+ [key: string]: any;
24
+ };
25
+ }
26
+ /**
27
+ * Interface for CacheMap implementations to support metadata storage
28
+ * This allows eviction strategies to store and retrieve metadata independently of the storage mechanism
29
+ */
30
+ export interface CacheMapMetadataProvider {
31
+ /**
32
+ * Get metadata for a specific item
33
+ * @param key - Item key
34
+ * @returns Metadata if exists, null otherwise
35
+ */
36
+ getMetadata(key: string): Promise<CacheItemMetadata | null>;
37
+ /**
38
+ * Set metadata for a specific item
39
+ * @param key - Item key
40
+ * @param metadata - Metadata to store
41
+ */
42
+ setMetadata(key: string, metadata: CacheItemMetadata): Promise<void>;
43
+ /**
44
+ * Delete metadata for a specific item
45
+ * @param key - Item key
46
+ */
47
+ deleteMetadata(key: string): Promise<void>;
48
+ /**
49
+ * Get all metadata entries
50
+ * @returns Map of all metadata entries
51
+ */
52
+ getAllMetadata(): Promise<Map<string, CacheItemMetadata>>;
53
+ /**
54
+ * Clear all metadata
55
+ */
56
+ clearMetadata(): Promise<void>;
57
+ /**
58
+ * Get current cache size information
59
+ * @returns Object with current size metrics
60
+ */
61
+ getCurrentSize(): Promise<{
62
+ itemCount: number;
63
+ sizeBytes: number;
64
+ }>;
65
+ /**
66
+ * Get cache size limits
67
+ * @returns Object with size limits (null means unlimited)
68
+ */
69
+ getSizeLimits(): Promise<{
70
+ maxItems: number | null;
71
+ maxSizeBytes: number | null;
72
+ }>;
73
+ }
74
+ /**
75
+ * Context provided to eviction strategies for decision making
76
+ */
77
+ export interface EvictionContext {
78
+ /** Current cache size information */
79
+ currentSize: {
80
+ itemCount: number;
81
+ sizeBytes: number;
82
+ };
83
+ /** Cache size limits */
84
+ limits: {
85
+ maxItems: number | null;
86
+ maxSizeBytes: number | null;
87
+ };
88
+ /** Size of the item being added (for proactive eviction) */
89
+ newItemSize?: number;
21
90
  }
22
91
  /**
23
92
  * Abstract base class for cache eviction strategies.
24
93
  * Defines the core contract that all eviction policies must implement.
94
+ *
95
+ * Eviction strategies are now completely independent of CacheMap implementations
96
+ * and interact through the CacheMapMetadataProvider interface.
25
97
  */
26
98
  export declare abstract class EvictionStrategy {
27
99
  /**
28
- * Select which item should be evicted based on the strategy
29
- * @param items - Map of items with their metadata
30
- * @returns Key of the item to evict, or null if no eviction needed
100
+ * Select which items should be evicted based on the strategy and context
101
+ * @param metadataProvider - Provider for accessing cache metadata
102
+ * @param context - Current cache state and limits
103
+ * @returns Array of keys to evict (empty array if no eviction needed)
31
104
  */
32
- abstract selectForEviction(items: Map<string, CacheItemMetadata>): string | null;
105
+ abstract selectForEviction(metadataProvider: CacheMapMetadataProvider, context: EvictionContext): Promise<string[]>;
33
106
  /**
34
107
  * Update metadata when an item is accessed
35
108
  * @param key - Item key
36
- * @param metadata - Current metadata
109
+ * @param metadataProvider - Provider for accessing cache metadata
37
110
  */
38
- abstract onItemAccessed(key: string, metadata: CacheItemMetadata): void;
111
+ abstract onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): Promise<void>;
39
112
  /**
40
113
  * Update metadata when an item is added
41
114
  * @param key - Item key
42
- * @param metadata - Initial metadata
115
+ * @param estimatedSize - Estimated size of the item in bytes
116
+ * @param metadataProvider - Provider for accessing cache metadata
43
117
  */
44
- abstract onItemAdded(key: string, metadata: CacheItemMetadata): void;
118
+ abstract onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): Promise<void>;
45
119
  /**
46
120
  * Clean up when an item is removed
47
- * @param key - Item key (optional for some strategies)
121
+ * @param key - Item key
122
+ * @param metadataProvider - Provider for accessing cache metadata
123
+ */
124
+ abstract onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): Promise<void>;
125
+ /**
126
+ * Get the name/identifier of this eviction strategy
127
+ * @returns String identifier for the strategy
128
+ */
129
+ abstract getStrategyName(): string;
130
+ /**
131
+ * Determine if eviction is needed based on current context
132
+ * @param context - Current cache state and limits
133
+ * @returns True if eviction should occur
134
+ */
135
+ protected isEvictionNeeded(context: EvictionContext): boolean;
136
+ /**
137
+ * Calculate how many items need to be evicted
138
+ * @param context - Current cache state and limits
139
+ * @returns Number of items that should be evicted
48
140
  */
49
- abstract onItemRemoved(key?: string): void;
141
+ protected calculateEvictionCount(context: EvictionContext): number;
50
142
  }
@@ -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): Promise<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): Promise<void>;
19
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): Promise<void>;
20
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): Promise<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): Promise<string[]>;
8
+ onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): Promise<void>;
9
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): Promise<void>;
10
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): Promise<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): Promise<string[]>;
15
+ onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): Promise<void>;
16
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): Promise<void>;
17
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): Promise<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): Promise<string[]>;
8
+ onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): Promise<void>;
9
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): Promise<void>;
10
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): Promise<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): Promise<string[]>;
9
+ onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): Promise<void>;
10
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): Promise<void>;
11
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): Promise<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): Promise<string[]>;
8
+ onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): Promise<void>;
9
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): Promise<void>;
10
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): Promise<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): Promise<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): Promise<void>;
28
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): Promise<void>;
29
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): Promise<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';