Registry / storage / memory-cacher

memory-cacher

JSON →
library1.1.1jsnpmunverified

A lightweight in-memory cache with async value retrieval, deduplication of concurrent requests, and TTL support. Version 1.1.1 is the latest stable release. Published by Logz.io, it provides a simple API to cache asynchronous results, ensuring that multiple calls for the same key during ongoing retrieval will wait for the same promise, avoiding redundant executions. Unlike more feature-rich caches (e.g., node-cache or lru-cache), it focuses on simplicity and async-first design.

npm install memory-cacher
INSTALL
IMPORT
SIG · MEMORY-CACHER
M
memory-cacher
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.

default
import MemoryCacher from 'memory-cacher';
const { MemoryCacher } = require('memory-cacher');
Default import in ESM; CommonJS uses require('memory-cacher').
getCached
import MemoryCacher from 'memory-cacher'; const result = await MemoryCacher.getCached(key, fn, ttl);
import { getCached } from 'memory-cacher';
getCached is a static method on the default export, not a named export.
setCached
import MemoryCacher from 'memory-cacher'; await MemoryCacher.setCached(key, value, ttl);
Also a static method on the default export.

Demonstrates basic usage: caching async result with TTL and retrieving from cache synchronously.

import MemoryCacher from 'memory-cacher'; async function getValue() { // Simulate async operation return new Promise(resolve => setTimeout(() => resolve('cached value'), 100)); } async function main() { const key = 'test'; // First call: executes and caches const result1 = await MemoryCacher.getCached(key, getValue, 5000); console.log(result1); // 'cached value' // Second call (immediate): returns cached value const result2 = await MemoryCacher.getCached(key); console.log(result2); // 'cached value' } main().catch(console.error);
Debug
Known issues
gotchaCalling getCached without a function argument does not throw, but returns null if key not cached.
fix
Check for null or use optional chaining.
affects: >=1.0.0
gotchaTTL parameter is in milliseconds, not seconds.
fix
Ensure you pass TTL in milliseconds.
affects: >=1.0.0
deprecatedThe package has not been updated since 2018 and uses CommonJS only. No TypeScript types.
fix
Consider alternative modern caches like lru-cache or async-cache-dedupe for TypeScript projects.
affects: >=1.0.0
Errors
Common errors & fixes
MemoryCacher is not defined
CommonJS require without variable assignment or ESM import error.
fix
Use const MemoryCacher = require('memory-cacher'); for CommonJS or import MemoryCacher from 'memory-cacher'; for ESM.
getCached(...) is undefined
Calling getCached with no key or non-existent key.
fix
Call getCached with a key that was previously cached or provide a function to compute the value.
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
12
Resources
memory-cacher — npm install memory-cacher · libregistry