Registry / storage / ttl
library1.3.1jsnpmunverified

Simple in-memory cache with TTL and capacity for JavaScript. Version 1.3.1 is the current stable release, with infrequent updates. It provides a lightweight cache with optional time-to-live per entry, capacity-limited eviction, and events for put, del, drop, hit, and miss. Differentiates from similar libraries (e.g., node-cache) by its simple API and event-driven architecture. Designed for Node.js environments, with no external dependencies.

npm install ttl
INSTALL
IMPORT
SIG · TTL
T
ttl
storagejavascriptv1.3.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const Cache = require('ttl');
const Cache = require('ttl').default;
CommonJS default export; no named exports.
Cache
import Cache from 'ttl';
import { Cache } from 'ttl';
ESM import; package does not export named symbols.
Cache
const { default: Cache } = await import('ttl');
const Cache = await import('ttl');
Dynamic import in Node.js ESM; default export is on the module namespace.

Creates a cache with TTL and capacity, listens to events, and demonstrates put/get with expiry.

const Cache = require('ttl'); const cache = new Cache({ ttl: 5000, capacity: 10 }); cache.on('put', (key, val) => console.log(`Put: ${key}=${val}`)); cache.on('del', (key, val) => console.log(`Del: ${key}`)); cache.on('drop', (key, val) => console.log(`Drop: ${key}`)); cache.on('hit', (key, val) => console.log(`Hit: ${key}`)); cache.on('miss', key => console.log(`Miss: ${key}`)); cache.put('greeting', 'hello'); cache.put('farewell', 'bye', 10000); console.log(cache.get('greeting')); // 'hello' console.log(cache.get('missing')); // undefined setTimeout(() => { console.log(cache.get('greeting')); // undefined after 5s }, 6000);
Debug
Known issues
gotchaCache keys are compared by reference for objects, not by value.
fix
Use strings or primitives as keys; if using objects, ensure same reference.
affects: >=0.0.0
gotchaCapacity eviction drops the oldest entries (FIFO), not LRU.
fix
Understand eviction policy: oldest inserted items are dropped when capacity is exceeded.
affects: >=0.0.0
gotchaTTL is in milliseconds, not seconds or a string format like '5s'.
fix
Pass numbers in milliseconds (e.g., 5000 for 5 seconds).
affects: >=0.0.0
deprecatedNo TypeScript type definitions are provided; user must create own or use @types/ttl.
fix
Install @types/ttl or declare module manually.
affects: <2.0.0
Errors
Common errors & fixes
TypeError: Cache is not a constructor
Trying to import incorrectly or using wrong export name.
fix
Use const Cache = require('ttl'); in CommonJS or import Cache from 'ttl'; in ESM.
ReferenceError: require is not defined
Running in ESM context where require is not available.
fix
Use import Cache from 'ttl'; instead of require.
Cannot find module 'ttl'
Package not installed.
fix
Run npm install ttl --save.
cache.put(...) is not a function
Cache object not instantiated correctly, e.g., missing 'new'.
fix
Use const cache = new Cache({ ... }); with the 'new' keyword.
Upgrade
Version history
1.3.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
31 hits · last 30 days
node
28
Resources
packagettl