Releases: ori88c/auto-fetch-fifo-ttl-cache
Releases · ori88c/auto-fetch-fifo-ttl-cache
First Release
Key Features ✨
- FIFO Cache 📦: Automatically evicts the oldest entry (based on insertion time) when the cache reaches its capacity and a new entry with a non-existing key is added, irrespective of the entry’s popularity. Compared to LRU cache variants, FIFO offers faster response times in scenarios such as: short-term key popularity, uniform key access patterns, or when the cache size closely matches the total number of possible keys. It is also well suited for security-sensitive use cases or environments where data freshness is critical.
- Fixed TTL 🔒: Ensures all cached entries share the same Time-to-Live (TTL) duration, allowing for automatic eviction of stale entries.
- Automated Value Fetching ⚙️: The
getOrFetchmethod streamlines the common get-or-fetch pattern, allowing developers to focus on core business logic. A value is fetched only if the key is missing or has expired; otherwise, the cached value is returned. - Concurrent Fetch Handling 🤹: To prevent redundant fetches for the same key during high-concurrency scenarios, the class uses a keyed lock mechanism. When multiple
getOrFetchcalls are made concurrently for the same missing key, only one fetch operation is executed. All callers await the same in-flight promise, reducing unnecessary network traffic and minimizing the risk of rate-limiting or throttling errors. - Graceful Teardown ⏳: Await the completion of all active fetch attempts using the
waitForActiveFetchesToCompletemethod. Example use cases include application shutdowns (e.g.,onModuleDestroyin NestJS applications) or maintaining a clear state between unit-tests. - Efficiency 💨: JavaScript's
Mapmaintains the insertion order of keys, offering a reliable and often overlooked guarantee for iteration. The underlying fifo-ttl-cache package leverages this guarantee to eliminate the need for manually managing insertion order during evictions. - Comprehensive Documentation 📚: The class is thoroughly documented, enabling IDEs to provide helpful tooltips that enhance the coding experience.
- Tests 🧪: Fully covered by comprehensive unit tests. Both dependencies are maintained by the same author and are thoroughly tested as well.
- ES2020 Compatibility: The project targets ES2020 for modern JavaScript support.
- Full TypeScript Support: Designed for seamless TypeScript integration.