Registry / storage / lru-ttl-cache

lru-ttl-cache

JSON →
library2.4.8jsnpmunverified

Fast LRU (Least Recently Used) and TTL (Time-To-Live) cache implementation with upsert and promise support. Current stable version is 2.4.8, actively maintained. Key differentiators: supports both LRU eviction and TTL expiration simultaneously, built-in upsert (insert or update) operations, and promise-based async value resolution. Lightweight, zero dependencies, TypeScript type definitions included. Suitable for in-memory caching in Node.js and browser environments where both access pattern and expiration control are needed.

npm install lru-ttl-cache
INSTALL
IMPORT
SIG · LRU-TTL-CACHE
L
lru-ttl-cache
storagejavascriptv2.4.8
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.

LRUTTLCache
import { LRUTTLCache } from 'lru-ttl-cache'
import LRUTTLCache from 'lru-ttl-cache'
This package only exports a named export. Default import will result in undefined. Check docs for correct named export.
LRUTTLCache (CJS)
const { LRUTTLCache } = require('lru-ttl-cache')
const LRUTTLCache = require('lru-ttl-cache')
CommonJS users must destructure the named export. Direct require returns an object with the class.
LRUTTLCache (type import)
import type { LRUTTLCache } from 'lru-ttl-cache'
import { LRUTTLCache as LRU } from 'lru-ttl-cache'
TypeScript type-only import should use 'import type'. Renaming the type is fine but not required.

Shows basic cache operations: creation with maxSize and ttl, set, get, upsert with async function, has, delete, and clear.

import { LRUTTLCache } from 'lru-ttl-cache'; const cache = new LRUTTLCache<string, number>({ maxSize: 100, ttl: 60000 // 60 seconds }); // Set a value cache.set('key1', 42); // Get a value const val = cache.get('key1'); console.log(val); // 42 // Upsert: update or insert with async function cache.upsert('key2', async () => { // Simulate async fetch return 100; }).then(value => { console.log('Upserted value:', value); }); // Check if key exists console.log(cache.has('key1')); // true // Delete a key cache.delete('key1'); // Clear cache cache.clear();
Debug
Known issues
breakingIn v2, the constructor options object is required. Passing no arguments or invalid options throws an error.
fix
Always pass an options object with at least maxSize or ttl.
affects: >=2.0.0
breakingThe 'maxAge' option was renamed to 'ttl' in v2. Using 'maxAge' will be ignored silently.
fix
Use 'ttl' instead of 'maxAge'.
affects: >=2.0.0
gotchaThe cache does not provide a 'size' property. Use 'has' or iterate to determine number of entries.
fix
There is no direct method to get the count. Consider maintaining your own counter if needed.
affects: >=1.0.0
gotchaWhen using 'upsert', the async function may be called multiple times concurrently for the same key if requests arrive before the first promise resolves. The cache only stores the result of the first resolved promise.
fix
If you need deduplication, implement your own locking or use an external library.
affects: >=1.0.0
gotchaThe cache does not support 'set' with a custom TTL per entry in v2.x. All entries use the cache-level ttl.
fix
If you need per-key TTL, consider a different cache library or implement a wrapper.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: lru_ttl_cache_1.default is not a constructor
Using default import when package only exports named export.
fix
Use named import: import { LRUTTLCache } from 'lru-ttl-cache'
Cannot read properties of undefined (reading 'get')
Instantiating without 'new' keyword.
fix
Use 'new LRUTTLCache(options)' instead of calling it as a function.
LRUTTLCache is not defined
Forgetting to import or incorrect import path.
fix
Run 'npm install lru-ttl-cache' and ensure import statement is correct.
Upgrade
Version history
2.4.8latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources
lru-ttl-cache — npm install lru-ttl-cache · libregistry