Registry / storage / with-simple-cache

with-simple-cache

JSON →
library0.16.0jsnpmunverified

A wrapper pattern for adding caching to synchronous and asynchronous functions in Node.js and browser environments. Version 0.16.0 ships TypeScript types and supports both sync and async caches via a clean functional API. Key differentiators: automatic cache key derivation from function arguments, pluggable cache stores (in-memory, on-disk, localStorage), and a promise-deduplication layer for async operations. Compared to alternatives like p-memoize or moize, this library enforces a simpler wrapper pattern and requires explicit cache configuration. Release cadence is irregular, with maintenance mode likely.

npm install with-simple-cache
INSTALL
IMPORT
SIG · WITH-SIMPLE-CACHE
W
with-simple-cache
storagejavascriptv0.16.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.

withSimpleCache
import { withSimpleCache } from 'with-simple-cache'
import withSimpleCache from 'with-simple-cache'
Named export only; default import is not available
withSimpleCacheAsync
import { withSimpleCacheAsync } from 'with-simple-cache'
import { withSimpleCacheAsync } from 'with-simple-cache/async'
Exported from main module, not a subpath
type Cache
import type { Cache } from 'with-simple-cache'
import { Cache } from 'with-simple-cache'
Cache is exported as a type for TypeScript users; runtime import will be undefined

Demonstrates synchronous caching: same input returns cached object reference

import { createCache } from 'simple-in-memory-cache'; import { withSimpleCache } from 'with-simple-cache'; const solveSuperToughMathProblem = withSimpleCache( ({ a, b }: { a: number; b: number }) => ({ solution: a + b }), { cache: createCache() }, ); const result1 = solveSuperToughMathProblem({ a: 1, b: 1 }); const result2 = solveSuperToughMathProblem({ a: 1, b: 1 }); console.log(result1 === result2); // true
Debug
Known issues
breakingCache argument object shape changed in v0.10.0: was { store, ... } now { cache, ... }
fix
Use { cache: createCache() } instead of { store: createCache() }
affects: >=0.10.0 <0.16.0
gotchaAsync cache usage requires explicit withSimpleCacheAsync wrapper for correct deduplication; using withSimpleCache with async cache may cause stale results
fix
Import withSimpleCacheAsync for async logic with async cache stores
affects: >=0.10.0
deprecatedString literals as cache keys are deprecated in favor of object-based keys (v0.8+)
fix
Pass object arguments to cached function; avoid raw string keys
affects: >=0.8.0
gotchaCache key derivation relies on object identity, not deep equality; mutating input objects after first call will not invalidate cache
fix
Use immutable patterns or ensure same object references for cache hits
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: cache.set is not a function
Passed an object without the expected cache interface (set, get, has, delete methods) as the cache option
fix
Use a cache implementation that provides the required methods, e.g., simple-in-memory-cache
Error: withSimpleCache expects a function as first argument
First argument to withSimpleCache is not a callable function
fix
Ensure the first argument is a function expression or arrow function, not a variable reference
TS2339: Property 'withSimpleCacheAsync' does not exist on type 'typeof import("with-simple-cache")'
Trying to use an old import path (with-simple-cache/async) that does not exist
fix
Import from the main module: import { withSimpleCacheAsync } from 'with-simple-cache'
Upgrade
Version history
0.16.0latest on npm
Audit
Dependencies
simple-in-memory-cacheoptionalRequired for default in-memory cache in examples, but not a package dependency; user provides cache implementation
simple-on-disk-cacheoptionalExample usage for async cache, optional user choice
Agent activity
31 hits · last 30 days
node
28
OpenAI (training)
1
Resources
with-simple-cache — npm install with-simple-cache · libregistry