Registry / storage / ttl-cache

ttl-cache

JSON →
library1.0.2jsnpmunverified

Simple in-memory object cache with per-item TTL (time-to-live) expiry. Version 1.0.2 is the latest stable release, with infrequent updates. Key differentiators: lightweight, zero dependencies, simple API. Suitable for small-scale caching needs where a full-featured cache like node-cache or lru-cache is overkill.

npm install ttl-cache
INSTALL
IMPORT
SIG · TTL-CACHE
T
ttl-cache
storagejavascriptv1.0.2
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
import TTLCache from 'ttl-cache'
const ttlCache = require('ttl-cache')
Package exports a single constructor function. ESM import is the default export.
TTLCache
import TTLCache from 'ttl-cache'
import { TTLCache } from 'ttl-cache'
Named import is incorrect; the package does not export a named class.
instance methods
const cache = new TTLCache({ ttl: 60 }); cache.get('key'); cache.set('key', value);
TTLCache.get('key')
Methods are on the instance, not static, despite the package's all-caps naming convention.

Creates a TTL cache with 60-second expiry, sets and retrieves a value, then shows expiration after timeout.

import TTLCache from 'ttl-cache'; const cache = new TTLCache({ ttl: 60 }); // items expire after 60 seconds // Set a value cache.set('greeting', 'Hello, world!'); // Get a value console.log(cache.get('greeting')); // 'Hello, world!' // Wait for expiration setTimeout(() => { console.log(cache.get('greeting')); // undefined (expired) }, 61000);
Debug
Known issues
gotchaThe `ttl` option is in seconds, not milliseconds.
fix
Pass TTL in seconds (e.g., 300 for 5 minutes).
affects: >=1.0.0
gotchaNo built-in eviction for expired items; they are only removed on access.
fix
Manually call `cache.purge()` to remove expired items proactively.
affects: >=1.0.0
gotchaThe package does not support TypeScript type definitions.
fix
Install @types/ttl-cache or define your own types.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'ttl-cache'
Package not installed correctly (maybe missing from node_modules).
fix
Run `npm install ttl-cache` or `yarn add ttl-cache`.
TypeError: cache.get is not a function
Incorrect import (e.g., imported { TTLCache } instead of export default).
fix
Use import TTLCache from 'ttl-cache'.
undefined is returned for an item that was just set
Item expired immediately due to TTL=0.
fix
Set a positive ttl value in constructor options.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
23 hits · last 30 days
node
22
Resources
ttl-cache — npm install ttl-cache · libregistry