@fjell/cache 4.6.22 → 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 (62) hide show
  1. package/CACHE_EVENTS.md +306 -0
  2. package/CACHE_IMPLEMENTATIONS.md +315 -0
  3. package/CONFIGURATION_GUIDE.md +167 -0
  4. package/CRITICAL_FIXES.md +68 -0
  5. package/MEMORY_LEAK_FIXES.md +270 -0
  6. package/README.md +513 -2
  7. package/dist/Aggregator.d.ts +27 -16
  8. package/dist/Cache.d.ts +59 -1
  9. package/dist/CacheContext.d.ts +35 -0
  10. package/dist/CacheMap.d.ts +132 -14
  11. package/dist/CacheStats.d.ts +51 -0
  12. package/dist/Instance.d.ts +4 -2
  13. package/dist/InstanceFactory.d.ts +3 -2
  14. package/dist/Operations.d.ts +21 -17
  15. package/dist/Options.d.ts +98 -0
  16. package/dist/browser/AsyncIndexDBCacheMap.d.ts +38 -0
  17. package/dist/browser/IndexDBCacheMap.d.ts +69 -0
  18. package/dist/browser/LocalStorageCacheMap.d.ts +59 -0
  19. package/dist/browser/SessionStorageCacheMap.d.ts +51 -0
  20. package/dist/events/CacheEventEmitter.d.ts +82 -0
  21. package/dist/events/CacheEventFactory.d.ts +121 -0
  22. package/dist/events/CacheEventTypes.d.ts +122 -0
  23. package/dist/events/index.d.ts +3 -0
  24. package/dist/eviction/EvictionManager.d.ts +57 -0
  25. package/dist/eviction/EvictionStrategy.d.ts +142 -0
  26. package/dist/eviction/EvictionStrategyConfig.d.ts +97 -0
  27. package/dist/eviction/EvictionStrategyFactory.d.ts +12 -0
  28. package/dist/eviction/EvictionStrategyValidation.d.ts +36 -0
  29. package/dist/eviction/index.d.ts +10 -0
  30. package/dist/eviction/strategies/ARCEvictionStrategy.d.ts +73 -0
  31. package/dist/eviction/strategies/FIFOEvictionStrategy.d.ts +12 -0
  32. package/dist/eviction/strategies/LFUEvictionStrategy.d.ts +38 -0
  33. package/dist/eviction/strategies/LRUEvictionStrategy.d.ts +12 -0
  34. package/dist/eviction/strategies/MRUEvictionStrategy.d.ts +12 -0
  35. package/dist/eviction/strategies/RandomEvictionStrategy.d.ts +12 -0
  36. package/dist/eviction/strategies/TwoQueueEvictionStrategy.d.ts +54 -0
  37. package/dist/index.d.ts +29 -6
  38. package/dist/index.js +5764 -435
  39. package/dist/index.js.map +4 -4
  40. package/dist/memory/EnhancedMemoryCacheMap.d.ts +81 -0
  41. package/dist/memory/MemoryCacheMap.d.ts +48 -0
  42. package/dist/normalization.d.ts +19 -0
  43. package/dist/ops/action.d.ts +2 -3
  44. package/dist/ops/all.d.ts +2 -3
  45. package/dist/ops/allAction.d.ts +2 -3
  46. package/dist/ops/allFacet.d.ts +2 -3
  47. package/dist/ops/create.d.ts +2 -3
  48. package/dist/ops/facet.d.ts +2 -3
  49. package/dist/ops/find.d.ts +2 -3
  50. package/dist/ops/findOne.d.ts +2 -3
  51. package/dist/ops/get.d.ts +3 -3
  52. package/dist/ops/one.d.ts +2 -3
  53. package/dist/ops/remove.d.ts +2 -3
  54. package/dist/ops/reset.d.ts +2 -1
  55. package/dist/ops/retrieve.d.ts +2 -3
  56. package/dist/ops/set.d.ts +2 -2
  57. package/dist/ops/update.d.ts +2 -3
  58. package/dist/ttl/TTLManager.d.ts +100 -0
  59. package/dist/ttl/index.d.ts +2 -0
  60. package/dist/utils/CacheSize.d.ts +30 -0
  61. package/fix-async-tests.js +116 -0
  62. package/package.json +16 -13
@@ -0,0 +1,12 @@
1
+ import { EvictionPolicy } from '../Options';
2
+ import { EvictionStrategy } from './EvictionStrategy';
3
+ import { EvictionStrategyConfigs } from './EvictionStrategyConfig';
4
+ /**
5
+ * Factory function to create eviction strategy instances with configuration
6
+ */
7
+ export declare function createEvictionStrategy(policy: EvictionPolicy, maxCacheSize?: number, config?: EvictionStrategyConfigs): EvictionStrategy;
8
+ /**
9
+ * Factory function for backwards compatibility
10
+ * @deprecated Use createEvictionStrategy with config parameter instead
11
+ */
12
+ export declare function createEvictionStrategyLegacy(policy: EvictionPolicy, maxCacheSize?: number): EvictionStrategy;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Validation functions for eviction strategy configurations
3
+ */
4
+ import { ARCConfig, EvictionStrategyConfigs, LFUConfig, TwoQueueConfig } from './EvictionStrategyConfig';
5
+ /**
6
+ * Sanitizes LFU configuration parameters, correcting invalid values
7
+ */
8
+ export declare function sanitizeLFUConfig(config: Partial<LFUConfig>): Partial<LFUConfig>;
9
+ /**
10
+ * Validates LFU configuration parameters (after sanitization)
11
+ */
12
+ export declare function validateLFUConfig(config: Partial<LFUConfig>): void;
13
+ /**
14
+ * Sanitizes ARC configuration parameters, correcting invalid values
15
+ */
16
+ export declare function sanitizeARCConfig(config: Partial<ARCConfig>): Partial<ARCConfig>;
17
+ /**
18
+ * Validates ARC configuration parameters (after sanitization)
19
+ */
20
+ export declare function validateARCConfig(config: Partial<ARCConfig>): void;
21
+ /**
22
+ * Sanitizes TwoQueue configuration parameters, correcting invalid values
23
+ */
24
+ export declare function sanitizeTwoQueueConfig(config: Partial<TwoQueueConfig>): Partial<TwoQueueConfig>;
25
+ /**
26
+ * Validates TwoQueue configuration parameters (after sanitization)
27
+ */
28
+ export declare function validateTwoQueueConfig(config: Partial<TwoQueueConfig>): void;
29
+ /**
30
+ * Validates any eviction strategy configuration
31
+ */
32
+ export declare function validateEvictionStrategyConfig(config: Partial<EvictionStrategyConfigs>): void;
33
+ /**
34
+ * Creates a validated configuration with defaults applied and invalid values sanitized
35
+ */
36
+ export declare function createValidatedConfig<T extends EvictionStrategyConfigs>(baseConfig: T, userConfig: Partial<T>): T;
@@ -0,0 +1,10 @@
1
+ export { EvictionStrategy, CacheItemMetadata, CacheMapMetadataProvider, EvictionContext } from './EvictionStrategy';
2
+ export { createEvictionStrategy } from './EvictionStrategyFactory';
3
+ export { EvictionManager } from './EvictionManager';
4
+ export { LRUEvictionStrategy } from './strategies/LRUEvictionStrategy';
5
+ export { LFUEvictionStrategy } from './strategies/LFUEvictionStrategy';
6
+ export { FIFOEvictionStrategy } from './strategies/FIFOEvictionStrategy';
7
+ export { MRUEvictionStrategy } from './strategies/MRUEvictionStrategy';
8
+ export { RandomEvictionStrategy } from './strategies/RandomEvictionStrategy';
9
+ export { ARCEvictionStrategy } from './strategies/ARCEvictionStrategy';
10
+ export { TwoQueueEvictionStrategy } from './strategies/TwoQueueEvictionStrategy';
@@ -0,0 +1,73 @@
1
+ import { CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from '../EvictionStrategy';
2
+ import { ARCConfig } from '../EvictionStrategyConfig';
3
+ /**
4
+ * ARC (Adaptive Replacement Cache) eviction strategy with enhanced frequency tracking
5
+ * Balances between recency (LRU) and frequency (LFU) dynamically with sophisticated frequency analysis
6
+ */
7
+ export declare class ARCEvictionStrategy extends EvictionStrategy {
8
+ getStrategyName(): string;
9
+ private recentGhosts;
10
+ private frequentGhosts;
11
+ private targetRecentSize;
12
+ private readonly config;
13
+ private readonly maxGhostSize;
14
+ private lastDecayTime;
15
+ constructor(maxCacheSize?: number, config?: Partial<ARCConfig>);
16
+ selectForEviction(metadataProvider: CacheMapMetadataProvider, context: EvictionContext): string[];
17
+ private selectLRUFromItems;
18
+ onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): void;
19
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): void;
20
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): void;
21
+ /**
22
+ * Add key to recent ghost list with proper size management
23
+ */
24
+ private addToRecentGhosts;
25
+ /**
26
+ * Add key to frequent ghost list with proper size management
27
+ */
28
+ private addToFrequentGhosts;
29
+ /**
30
+ * Cleanup ghost lists to prevent memory leaks
31
+ */
32
+ private cleanupGhostLists;
33
+ /**
34
+ * Enforce size limit on a ghost list by removing oldest entries
35
+ */
36
+ private enforceGhostListSizeLimit;
37
+ /**
38
+ * Determine if an item should be classified as frequent vs recent
39
+ */
40
+ private isFrequentItem;
41
+ /**
42
+ * Get effective frequency for an item, applying decay if enabled
43
+ */
44
+ private getEffectiveFrequency;
45
+ /**
46
+ * Calculate frequency score with decay applied
47
+ */
48
+ private calculateFrequencyScore;
49
+ /**
50
+ * Select eviction candidate using frequency-weighted approach
51
+ */
52
+ private selectFrequencyWeightedFromItems;
53
+ /**
54
+ * Apply periodic decay to frequency scores
55
+ */
56
+ private applyPeriodicDecay;
57
+ /**
58
+ * Get configuration for this strategy
59
+ */
60
+ getConfig(): ARCConfig;
61
+ /**
62
+ * Reset internal state (useful for testing)
63
+ */
64
+ reset(): void;
65
+ /**
66
+ * Get current adaptive state for monitoring/debugging
67
+ */
68
+ getAdaptiveState(): {
69
+ targetRecentSize: number;
70
+ recentGhostSize: number;
71
+ frequentGhostSize: number;
72
+ };
73
+ }
@@ -0,0 +1,12 @@
1
+ import { CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from '../EvictionStrategy';
2
+ /**
3
+ * FIFO (First-In, First-Out) eviction strategy
4
+ * Removes the oldest added item regardless of usage
5
+ */
6
+ export declare class FIFOEvictionStrategy extends EvictionStrategy {
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;
12
+ }
@@ -0,0 +1,38 @@
1
+ import { CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from '../EvictionStrategy';
2
+ import { LFUConfig } from '../EvictionStrategyConfig';
3
+ /**
4
+ * LFU (Least Frequently Used) eviction strategy with frequency sketching and decay
5
+ * Uses probabilistic counting and time-based frequency decay for more accurate frequency estimation
6
+ * When configured with default settings, behaves like traditional LFU for backwards compatibility
7
+ */
8
+ export declare class LFUEvictionStrategy extends EvictionStrategy {
9
+ getStrategyName(): string;
10
+ private readonly config;
11
+ private readonly sketch;
12
+ private lastDecayTime;
13
+ constructor(config?: Partial<LFUConfig>);
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;
18
+ /**
19
+ * Get the effective frequency for an item, applying real-time decay if needed
20
+ */
21
+ private getEffectiveFrequency;
22
+ /**
23
+ * Calculate frequency score with decay applied
24
+ */
25
+ private calculateFrequencyScore;
26
+ /**
27
+ * Apply periodic decay to the frequency sketch and metadata
28
+ */
29
+ private applyPeriodicDecay;
30
+ /**
31
+ * Get configuration for this strategy
32
+ */
33
+ getConfig(): LFUConfig;
34
+ /**
35
+ * Reset frequency tracking (useful for testing or cache clearing)
36
+ */
37
+ reset(): void;
38
+ }
@@ -0,0 +1,12 @@
1
+ import { CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from '../EvictionStrategy';
2
+ /**
3
+ * LRU (Least Recently Used) eviction strategy
4
+ * Removes the item that was accessed longest ago
5
+ */
6
+ export declare class LRUEvictionStrategy extends EvictionStrategy {
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;
12
+ }
@@ -0,0 +1,12 @@
1
+ import { CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from '../EvictionStrategy';
2
+ /**
3
+ * MRU (Most Recently Used) eviction strategy
4
+ * Removes the most recently accessed item
5
+ */
6
+ export declare class MRUEvictionStrategy extends EvictionStrategy {
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;
12
+ }
@@ -0,0 +1,12 @@
1
+ import { CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from '../EvictionStrategy';
2
+ /**
3
+ * Random eviction strategy
4
+ * Removes a random item from the cache
5
+ */
6
+ export declare class RandomEvictionStrategy extends EvictionStrategy {
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;
12
+ }
@@ -0,0 +1,54 @@
1
+ import { CacheMapMetadataProvider, EvictionContext, EvictionStrategy } from '../EvictionStrategy';
2
+ import { TwoQueueConfig } from '../EvictionStrategyConfig';
3
+ /**
4
+ * 2Q (Two Queues) eviction strategy with enhanced frequency tracking
5
+ * Maintains separate queues for recent and frequently accessed items
6
+ * Uses frequency analysis for promotion decisions and weighted LRU in hot queue
7
+ */
8
+ export declare class TwoQueueEvictionStrategy extends EvictionStrategy {
9
+ getStrategyName(): string;
10
+ private recentQueue;
11
+ private hotQueue;
12
+ private ghostQueue;
13
+ private readonly config;
14
+ private readonly maxRecentSize;
15
+ private readonly maxGhostSize;
16
+ private lastDecayTime;
17
+ constructor(maxCacheSize?: number, config?: Partial<TwoQueueConfig>);
18
+ selectForEviction(metadataProvider: CacheMapMetadataProvider, context: EvictionContext): string[];
19
+ /**
20
+ * Select eviction candidate from hot queue using traditional LRU
21
+ */
22
+ private selectFromHotQueueLRU;
23
+ /**
24
+ * Select eviction candidate from hot queue using frequency-weighted LRU
25
+ */
26
+ private selectFromHotQueueFrequencyWeighted;
27
+ onItemAccessed(key: string, metadataProvider: CacheMapMetadataProvider): void;
28
+ onItemAdded(key: string, estimatedSize: number, metadataProvider: CacheMapMetadataProvider): void;
29
+ onItemRemoved(key: string, metadataProvider: CacheMapMetadataProvider): void;
30
+ /**
31
+ * Determine if an item should be promoted from recent to hot queue
32
+ */
33
+ private shouldPromoteToHotQueue;
34
+ /**
35
+ * Get effective frequency for an item, applying decay if enabled
36
+ */
37
+ private getEffectiveFrequency;
38
+ /**
39
+ * Calculate frequency score with decay applied
40
+ */
41
+ private calculateFrequencyScore;
42
+ /**
43
+ * Apply periodic decay to hot queue items
44
+ */
45
+ private applyPeriodicDecay;
46
+ /**
47
+ * Get configuration for this strategy
48
+ */
49
+ getConfig(): TwoQueueConfig;
50
+ /**
51
+ * Reset internal state (useful for testing)
52
+ */
53
+ reset(): void;
54
+ }
package/dist/index.d.ts CHANGED
@@ -1,7 +1,30 @@
1
- export * from './Aggregator';
2
- export * from './Cache';
3
- export * from './CacheMap';
4
- export * from './Instance';
5
- export * from './InstanceFactory';
6
- export * from './Operations';
1
+ export { createCache, isCache } from './Cache';
2
+ export type { Cache, CacheInfo } from './Cache';
3
+ export { CacheMap } from './CacheMap';
4
+ export { CacheStatsManager } from './CacheStats';
5
+ export type { CacheStats } from './CacheStats';
6
+ export { MemoryCacheMap } from './memory/MemoryCacheMap';
7
+ export { EnhancedMemoryCacheMap } from './memory/EnhancedMemoryCacheMap';
8
+ export { LocalStorageCacheMap } from './browser/LocalStorageCacheMap';
9
+ export { SessionStorageCacheMap } from './browser/SessionStorageCacheMap';
10
+ export { IndexDBCacheMap } from './browser/IndexDBCacheMap';
11
+ export { AsyncIndexDBCacheMap } from './browser/AsyncIndexDBCacheMap';
12
+ export { createOptions, createCacheMap, validateOptions } from './Options';
13
+ export type { Options, CacheType, EvictionPolicy, CacheSizeConfig, IndexedDBConfig, WebStorageConfig, MemoryConfig, CacheMapFactory } from './Options';
14
+ export { createInstanceFactory } from './InstanceFactory';
15
+ export type { InstanceFactory } from './InstanceFactory';
16
+ export { createInstance, isInstance } from './Instance';
17
+ export type { Instance } from './Instance';
18
+ export { normalizeKeyValue, createNormalizedHashFunction, isLocKeyArrayEqual, normalizeLocKeyItem } from './normalization';
19
+ export { parseSizeString, formatBytes, estimateValueSize, validateSizeConfig } from './utils/CacheSize';
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';
24
+ export { validateEvictionStrategyConfig, validateLFUConfig, validateARCConfig, validateTwoQueueConfig, createValidatedConfig } from './eviction/EvictionStrategyValidation';
25
+ export { createOperations } from './Operations';
26
+ export type { Operations } from './Operations';
27
+ export * from './events';
28
+ export { createAggregator, toCacheConfig } from './Aggregator';
29
+ export type { Aggregator, CacheConfig, AggregateConfig } from './Aggregator';
7
30
  export * from './Registry';