@fjell/cache 4.6.22 → 4.7.0

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 (52) hide show
  1. package/CACHE_IMPLEMENTATIONS.md +198 -0
  2. package/CONFIGURATION_GUIDE.md +167 -0
  3. package/CRITICAL_FIXES.md +68 -0
  4. package/README.md +506 -2
  5. package/debug_test2.js +0 -0
  6. package/debug_test3.js +0 -0
  7. package/dist/Cache.d.ts +4 -1
  8. package/dist/CacheContext.d.ts +27 -0
  9. package/dist/CacheMap.d.ts +89 -14
  10. package/dist/Instance.d.ts +4 -2
  11. package/dist/InstanceFactory.d.ts +3 -2
  12. package/dist/Operations.d.ts +2 -1
  13. package/dist/Options.d.ts +100 -0
  14. package/dist/browser/AsyncIndexDBCacheMap.d.ts +38 -0
  15. package/dist/browser/IndexDBCacheMap.d.ts +54 -0
  16. package/dist/browser/LocalStorageCacheMap.d.ts +43 -0
  17. package/dist/browser/SessionStorageCacheMap.d.ts +35 -0
  18. package/dist/eviction/EvictionStrategy.d.ts +50 -0
  19. package/dist/eviction/EvictionStrategyConfig.d.ts +97 -0
  20. package/dist/eviction/EvictionStrategyFactory.d.ts +12 -0
  21. package/dist/eviction/EvictionStrategyValidation.d.ts +36 -0
  22. package/dist/eviction/index.d.ts +9 -0
  23. package/dist/eviction/strategies/ARCEvictionStrategy.d.ts +68 -0
  24. package/dist/eviction/strategies/FIFOEvictionStrategy.d.ts +11 -0
  25. package/dist/eviction/strategies/LFUEvictionStrategy.d.ts +37 -0
  26. package/dist/eviction/strategies/LRUEvictionStrategy.d.ts +11 -0
  27. package/dist/eviction/strategies/MRUEvictionStrategy.d.ts +11 -0
  28. package/dist/eviction/strategies/RandomEvictionStrategy.d.ts +11 -0
  29. package/dist/eviction/strategies/TwoQueueEvictionStrategy.d.ts +53 -0
  30. package/dist/index.d.ts +24 -7
  31. package/dist/index.js +3879 -446
  32. package/dist/index.js.map +4 -4
  33. package/dist/memory/EnhancedMemoryCacheMap.d.ts +75 -0
  34. package/dist/memory/MemoryCacheMap.d.ts +33 -0
  35. package/dist/normalization.d.ts +20 -0
  36. package/dist/ops/action.d.ts +2 -3
  37. package/dist/ops/all.d.ts +2 -3
  38. package/dist/ops/allAction.d.ts +2 -3
  39. package/dist/ops/allFacet.d.ts +2 -3
  40. package/dist/ops/create.d.ts +2 -3
  41. package/dist/ops/facet.d.ts +2 -3
  42. package/dist/ops/find.d.ts +2 -3
  43. package/dist/ops/findOne.d.ts +2 -3
  44. package/dist/ops/get.d.ts +2 -3
  45. package/dist/ops/one.d.ts +2 -3
  46. package/dist/ops/remove.d.ts +2 -3
  47. package/dist/ops/reset.d.ts +2 -1
  48. package/dist/ops/retrieve.d.ts +2 -3
  49. package/dist/ops/set.d.ts +2 -2
  50. package/dist/ops/update.d.ts +2 -3
  51. package/dist/utils/CacheSize.d.ts +37 -0
  52. package/package.json +1 -1
@@ -0,0 +1,68 @@
1
+ import { CacheItemMetadata, 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
+ private recentGhosts;
9
+ private frequentGhosts;
10
+ private targetRecentSize;
11
+ private readonly config;
12
+ private readonly maxGhostSize;
13
+ private lastDecayTime;
14
+ constructor(maxCacheSize?: number, config?: Partial<ARCConfig>);
15
+ selectForEviction(items: Map<string, CacheItemMetadata>): string | null;
16
+ private selectLRUFromItems;
17
+ onItemAccessed(key: string, metadata: CacheItemMetadata): void;
18
+ onItemAdded(key: string, metadata: CacheItemMetadata): void;
19
+ onItemRemoved(key: string): void;
20
+ /**
21
+ * Add key to recent ghost list with proper size management
22
+ */
23
+ private addToRecentGhosts;
24
+ /**
25
+ * Add key to frequent ghost list with proper size management
26
+ */
27
+ private addToFrequentGhosts;
28
+ /**
29
+ * Cleanup ghost lists to prevent memory leaks
30
+ */
31
+ private cleanupGhostLists;
32
+ /**
33
+ * Determine if an item should be classified as frequent vs recent
34
+ */
35
+ private isFrequentItem;
36
+ /**
37
+ * Get effective frequency for an item, applying decay if enabled
38
+ */
39
+ private getEffectiveFrequency;
40
+ /**
41
+ * Calculate frequency score with decay applied
42
+ */
43
+ private calculateFrequencyScore;
44
+ /**
45
+ * Select eviction candidate using frequency-weighted approach
46
+ */
47
+ private selectFrequencyWeightedFromItems;
48
+ /**
49
+ * Apply periodic decay to frequency scores
50
+ */
51
+ private applyPeriodicDecay;
52
+ /**
53
+ * Get configuration for this strategy
54
+ */
55
+ getConfig(): ARCConfig;
56
+ /**
57
+ * Reset internal state (useful for testing)
58
+ */
59
+ reset(): void;
60
+ /**
61
+ * Get current adaptive state for monitoring/debugging
62
+ */
63
+ getAdaptiveState(): {
64
+ targetRecentSize: number;
65
+ recentGhostSize: number;
66
+ frequentGhostSize: number;
67
+ };
68
+ }
@@ -0,0 +1,11 @@
1
+ import { CacheItemMetadata, 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(items: Map<string, CacheItemMetadata>): string | null;
8
+ onItemAccessed(_key: string, metadata: CacheItemMetadata): void;
9
+ onItemAdded(_key: string, metadata: CacheItemMetadata): void;
10
+ onItemRemoved(): void;
11
+ }
@@ -0,0 +1,37 @@
1
+ import { CacheItemMetadata, 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
+ private readonly config;
10
+ private readonly sketch;
11
+ private lastDecayTime;
12
+ 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;
17
+ /**
18
+ * Get the effective frequency for an item, applying real-time decay if needed
19
+ */
20
+ private getEffectiveFrequency;
21
+ /**
22
+ * Calculate frequency score with decay applied
23
+ */
24
+ private calculateFrequencyScore;
25
+ /**
26
+ * Apply periodic decay to the frequency sketch and metadata
27
+ */
28
+ private applyPeriodicDecay;
29
+ /**
30
+ * Get configuration for this strategy
31
+ */
32
+ getConfig(): LFUConfig;
33
+ /**
34
+ * Reset frequency tracking (useful for testing or cache clearing)
35
+ */
36
+ reset(): void;
37
+ }
@@ -0,0 +1,11 @@
1
+ import { CacheItemMetadata, 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(items: Map<string, CacheItemMetadata>): string | null;
8
+ onItemAccessed(_key: string, metadata: CacheItemMetadata): void;
9
+ onItemAdded(_key: string, metadata: CacheItemMetadata): void;
10
+ onItemRemoved(): void;
11
+ }
@@ -0,0 +1,11 @@
1
+ import { CacheItemMetadata, 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
+ selectForEviction(items: Map<string, CacheItemMetadata>): string | null;
8
+ onItemAccessed(_key: string, metadata: CacheItemMetadata): void;
9
+ onItemAdded(_key: string, metadata: CacheItemMetadata): void;
10
+ onItemRemoved(): void;
11
+ }
@@ -0,0 +1,11 @@
1
+ import { CacheItemMetadata, 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(items: Map<string, CacheItemMetadata>): string | null;
8
+ onItemAccessed(_key: string, metadata: CacheItemMetadata): void;
9
+ onItemAdded(_key: string, metadata: CacheItemMetadata): void;
10
+ onItemRemoved(): void;
11
+ }
@@ -0,0 +1,53 @@
1
+ import { CacheItemMetadata, 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
+ private recentQueue;
10
+ private hotQueue;
11
+ private ghostQueue;
12
+ private readonly config;
13
+ private readonly maxRecentSize;
14
+ private readonly maxGhostSize;
15
+ private lastDecayTime;
16
+ constructor(maxCacheSize?: number, config?: Partial<TwoQueueConfig>);
17
+ selectForEviction(items: Map<string, CacheItemMetadata>): string | null;
18
+ /**
19
+ * Select eviction candidate from hot queue using traditional LRU
20
+ */
21
+ private selectFromHotQueueLRU;
22
+ /**
23
+ * Select eviction candidate from hot queue using frequency-weighted LRU
24
+ */
25
+ private selectFromHotQueueFrequencyWeighted;
26
+ onItemAccessed(key: string, metadata: CacheItemMetadata): void;
27
+ onItemAdded(key: string, metadata: CacheItemMetadata): void;
28
+ onItemRemoved(key: string): void;
29
+ /**
30
+ * Determine if an item should be promoted from recent to hot queue
31
+ */
32
+ private shouldPromoteToHotQueue;
33
+ /**
34
+ * Get effective frequency for an item, applying decay if enabled
35
+ */
36
+ private getEffectiveFrequency;
37
+ /**
38
+ * Calculate frequency score with decay applied
39
+ */
40
+ private calculateFrequencyScore;
41
+ /**
42
+ * Apply periodic decay to hot queue items
43
+ */
44
+ private applyPeriodicDecay;
45
+ /**
46
+ * Get configuration for this strategy
47
+ */
48
+ getConfig(): TwoQueueConfig;
49
+ /**
50
+ * Reset internal state (useful for testing)
51
+ */
52
+ reset(): void;
53
+ }
package/dist/index.d.ts CHANGED
@@ -1,7 +1,24 @@
1
- export * from './Aggregator';
2
- export * from './Cache';
3
- export * from './CacheMap';
4
- export * from './Instance';
5
- export * from './InstanceFactory';
6
- export * from './Operations';
7
- export * from './Registry';
1
+ export { createCache, isCache } from './Cache';
2
+ export type { Cache } from './Cache';
3
+ export { CacheMap } from './CacheMap';
4
+ export { MemoryCacheMap } from './memory/MemoryCacheMap';
5
+ export { EnhancedMemoryCacheMap } from './memory/EnhancedMemoryCacheMap';
6
+ export { LocalStorageCacheMap } from './browser/LocalStorageCacheMap';
7
+ export { SessionStorageCacheMap } from './browser/SessionStorageCacheMap';
8
+ export { IndexDBCacheMap } from './browser/IndexDBCacheMap';
9
+ export { AsyncIndexDBCacheMap } from './browser/AsyncIndexDBCacheMap';
10
+ export { createOptions, createCacheMap, validateOptions } from './Options';
11
+ export type { Options, CacheType, EvictionPolicy, CacheSizeConfig, IndexedDBConfig, WebStorageConfig, MemoryConfig, CacheMapFactory } from './Options';
12
+ export { createInstanceFactory } from './InstanceFactory';
13
+ export type { InstanceFactory } from './InstanceFactory';
14
+ export { createInstance, isInstance } from './Instance';
15
+ export type { Instance } from './Instance';
16
+ export { normalizeKeyValue, createNormalizedHashFunction, isLocKeyArrayEqual, normalizeLocKeyItem } from './normalization';
17
+ export { parseSizeString, formatBytes, estimateValueSize, validateSizeConfig } from './utils/CacheSize';
18
+ export { createEvictionStrategy } from './eviction';
19
+ export type { CacheItemMetadata, EvictionStrategy } from './eviction';
20
+ export { validateEvictionStrategyConfig, validateLFUConfig, validateARCConfig, validateTwoQueueConfig, createValidatedConfig } from './eviction/EvictionStrategyValidation';
21
+ export { createOperations } from './Operations';
22
+ export type { Operations } from './Operations';
23
+ export { createAggregator, toCacheConfig } from './Aggregator';
24
+ export type { Aggregator, CacheConfig, AggregateConfig } from './Aggregator';