Registry / storage / ts-promise-cache

ts-promise-cache

JSON →
library0.9.0jsnpmunverified

A TypeScript promise-based loading cache that prevents thundering herds (cache stampede). Version 0.9.0 is the latest stable release. Ships with TypeScript definitions. Low release cadence. Differentiators: configurable TTL (access- or write-based), separate cache for single values, statistics, explicit set method, and customizable rejection handling with optional retry via ts-retry-promise.

npm install ts-promise-cache
INSTALL
IMPORT
SIG · TS-PROMISE-CACHE
T
ts-promise-cache
storagejavascriptv0.9.0
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.

PromiseCache
✓ import { PromiseCache } from 'ts-promise-cache'
✗ const PromiseCache = require('ts-promise-cache')
Package is ESM-first and ships TypeScript types. CJS require works but may need .default.
singlePromiseCache
✓ import { singlePromiseCache } from 'ts-promise-cache'
✗ const { singlePromiseCache } = require('ts-promise-cache')
Factory function for caching a single promise. Use named import.
CacheConfig
✓ import { CacheConfig } from 'ts-promise-cache'
✗ import CacheConfig from 'ts-promise-cache'
CacheConfig is a named export (class), not a default export.
Stats
✓ import { Stats } from 'ts-promise-cache'
Interface for stats object, useful for type annotations.

Creates a PromiseCache with a loader, retrieves a value, and logs cache statistics.

import { PromiseCache } from 'ts-promise-cache'; const cache = new PromiseCache<string>((key) => Promise.resolve(`value for ${key}`)); async function main() { const value = await cache.get('myKey'); console.log(value); // 'value for myKey' const stats = cache.stats(); console.log(stats.hits, stats.misses); } main().catch(console.error);
Debug
Known issues
gotchaDefault TTL is 'FOREVER', meaning entries never expire unless explicitly configured.
fix
Set ttl and checkInterval when creating cache to avoid unbounded memory growth.
affects: *
gotchaDefault onReject re-throws the error; rejected promises are not cached (removeRejected: true).
fix
Override onReject to handle retries or provide fallback values.
affects: *
gotchaThe loader is called synchronously with the key argument; async loaders must return a Promise.
fix
Ensure loader returns a Promise; for synchronous loads, wrap in Promise.resolve().
affects: *
breakingVersion 0.9.0 may have breaking changes from earlier 0.x versions; check changelog.
fix
Review the GitHub releases for migration notes.
affects: <0.9.0
Errors
Common errors & fixes
TypeError: PromiseCache is not a constructor
Importing the wrong export or using CJS incorrectly.
fix
import { PromiseCache } from 'ts-promise-cache'
Property 'stats' does not exist on type 'PromiseCache<string>'
Using an older version before stats was added (pre-0.9.0).
fix
Update to 0.9.0: npm install ts-promise-cache@latest
Expected 0 arguments, but got 1
Using singlePromiseCache incorrectly: it returns a function, not a cache object.
fix
const cachedFn = singlePromiseCache(loader); const value = await cachedFn();
Upgrade
Version history
0.9.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
34 hits · last 30 days
node
32
Resources
ts-promise-cache — npm install ts-promise-cache · libregistry