Registry / storage / cajache

cajache

JSON →
library2.0.2jsnpmunverified

cajache is a minimalistic in-memory caching library for JavaScript/TypeScript (v2.0.2, released 2023). It provides a simple API to cache any asynchronous function result, with optional TTL support and configurable expiration check intervals. Unlike larger caching libraries like node-cache or lru-cache, cajache has zero dependencies and is very lightweight (~1KB minzipped). Its key differentiator is the `.use()` method that automatically handles cache miss/miss logic, reducing boilerplate. The library supports persistent (no TTL) and time-based caching, multiple instances, and path-based caching for nested results. It ships TypeScript definitions.

npm install cajache
INSTALL
IMPORT
SIG · CAJACHE
C
cajache
storagejavascriptv2.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 cajache from 'cajache'
✗ const cajache = require('cajache').default
ESM default import works directly. No .default needed in ESM.
Cajache
✓ import { Cajache } from 'cajache'
✗ const Cajache = require('cajache').Cajache
Named export for the class when using TypeScript types.
default (require)
✓ const cajache = require('cajache')
CommonJS require works as shown in README. No .default needed.

Shows basic usage of cajache: permanent caching, TTL caching, and creating a new instance.

import cajache from 'cajache'; async function main() { // Permanent cache const response1 = await cajache.use( 'myKey', () => fetch('https://api.example.com/data').then(r => r.json()) ); console.log(response1); // Fetches first time const response2 = await cajache.use( 'myKey', () => fetch('https://api.example.com/data').then(r => r.json()) ); console.log(response2); // Returns cached value // TTL cache (1 second) const response3 = await cajache.use( 'myKey2', () => fetch('https://api.example.com/other').then(r => r.json()), { ttl: 1000 } ); console.log(response3); // Creating isolated instance const myCache = cajache.new(); const response4 = await myCache.use( 'key', () => Promise.resolve('cached value') ); console.log(response4); // 'cached value' } main();
Debug
Known issues
gotchaThe `.use()` method always expects a function as second argument; passing a promise directly may cause unhandled rejections or unexpected behavior.
fix
Wrap any pre-existing promise in an arrow function: `() => myPromise`.
affects: >=1.0.0
gotchaTTL expiration check interval defaults to 60 seconds. This means expired entries may not be cleaned up immediately; they are only purged on get or during the periodic check.
fix
Set `checkInterval` in constructor options to a smaller value if more immediate cleanup is needed.
affects: >=1.0.0
gotchaThe `path` option uses dot notation but does not support array indices or bracket notation for nested properties with special characters.
fix
Ensure the path only references simple object keys without dots or special characters.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: cajache.use is not a function
Using default import with CommonJS require incorrectly.
fix
Use `const cajache = require('cajache')` for CommonJS, or `import cajache from 'cajache'` for ESM.
TypeError: fnc is not a function
Passing a non-function as second argument to `.use()`.
fix
Ensure the second argument is a function: `() => yourCode`.
Upgrade
Version history
2.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
29 hits · last 30 days
node
28
Resources
cajache — npm install cajache · libregistry