Registry / storage / ttl-cache-js

ttl-cache-js

JSON →
library1.1.1jsnpmunverified

A simple TTL-based in-memory cache library for Node.js (v1.1.1, 5+ years old). Provides get/set/del/has/size/clear with optional TTL in milliseconds. Minimal dependencies; no async, no events, no size limits. Slower than `node-cache` or `lru-cache`; primarily a teaching example. Not actively maintained (last commit 2018).

npm install ttl-cache-js
INSTALL
IMPORT
SIG · TTL-CACHE-JS
T
ttl-cache-js
storagejavascriptv1.1.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.

Cache
const Cache = require('ttl-cache-js')
import Cache from 'ttl-cache-js'
CJS-only; no ESM or default export. Must use require().
cache instance
const cache = Cache()
const cache = new Cache()
Both work but library returns a factory; new Cache() also returns a new instance via internal fallback.
type imports
no type exports
import { Cache } from 'ttl-cache-js'
No TypeScript definitions; types are not shipped.

Basic usage: create cache, set key with 60s TTL, get value, wait for expiry, delete key, and check existence.

const Cache = require('ttl-cache-js'); const cache = Cache(); cache.set('greeting', 'Hello', 60000); console.log(cache.get('greeting')); // 'Hello' setTimeout(() => console.log(cache.get('greeting', 'expired')), 60001); // 'expired' cache.del('greeting'); console.log(cache.has('greeting')); // false
Debug
Known issues
gotchaTTL is in milliseconds, not seconds.
fix
Convert seconds to ms: set(key, val, seconds * 1000)
affects: >=0.0.0
gotchaset() returns the value, not the cache instance (no chaining).
fix
Chain with separate calls: cache.set('a', 1); cache.set('b', 2);
affects: >=0.0.0
deprecatedNo ES module support; will break in Node ESM contexts.
fix
Use dynamic import() with .mjs wrapper or switch to lru-cache.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Cache is not a constructor
Attempting to use `new Cache()` in a context where Cache is not a constructor (factory call works but new fails if library doesn't handle it).
fix
Use `const cache = Cache()` without `new`.
Cannot find module 'ttl-cache-js'
Missing npm install or incorrect path (ESM import).
fix
Run `npm install ttl-cache-js` and use `require('ttl-cache-js')`.
cache.get is not a function
Instance not created; imported the raw module instead of calling it.
fix
Ensure you call `Cache()` or `new Cache()` to get an instance.
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
31 hits · last 30 days
node
28
Amazon
1
Resources
ttl-cache-js — npm install ttl-cache-js · libregistry