A simple, performant in-memory cache with configurable Time To Live (TTL) for Node.js. Current stable version is 1.0.52. It provides global and per-key TTL, automatic expiry cleanup via configurable interval, optional TTL randomization, extend-on-hit behavior, and an async onInterval callback to refresh cache entries. The package has zero dependencies and uses CommonJS. It is lightweight and minimal compared to more feature-rich caches like node-cache or lru-cache.
npm install memory-cache-ttlNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Initialize cache with 3-second TTL, set and get a value, then verify expiry after 5 seconds.
Use a positive integer for TTL. If you want no expiry, set TTL to Infinity or very large number.
Always provide a global TTL or per-key TTL in set() to enable automatic expiry.
Ensure onInterval returns a Promise that resolves to the new value or undefined to keep old value.
Set interval to a value less than or equal to the smallest TTL for timely expiry.
Use const cache = require('memory-cache-ttl'); (CommonJS) or use default import if ESM wrapper exists (check package).Ensure cache is required as default: const cache = require('memory-cache-ttl'); then call cache.init() before set/get.Set interval to a reasonable value (e.g., 1 second). Ensure cache.init({ interval: 1 }) is called.No dependency data recorded yet.