Registry / devops / cache-func

cache-func

JSON →
library1.1.1jsnpmunverified

A minimal utility for caching synchronous function return values with optional TTL. Current stable version is 1.1.1. It has no dependencies and ships TypeScript declarations. Differentiates by extreme simplicity (single function, no complex keying or async support) vs. heavier memoization libraries like lodash.memoize or memoizee. Suitable for one-time or stale-while-revalidate caching patterns.

npm install cache-func
INSTALL
IMPORT
SIG · CACHE-FUNC
C
cache-func
devopsjavascriptv1.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.

cacheFunc
import { cacheFunc } from 'cache-func'
import cacheFunc from 'cache-func'
cacheFunc is a named export, not default.
cacheFunc
const { cacheFunc } = require('cache-func')
CommonJS requires destructuring; the package supports both ESM and CJS.
cacheFunc
import { cacheFunc } from 'cache-func'
import * as cacheFunc from 'cache-func'
Namespace import won't work because cacheFunc is a named export.

Creates a cached version of a synchronous function with a 1-minute TTL.

import { cacheFunc } from 'cache-func'; function expensiveCalculation() { console.log('Calculating...'); return 42; } const cachedFn = cacheFunc(expensiveCalculation, { maxAge: 60 * 1000 // Cache for 1 minute }); console.log(cachedFn()); // Logs 'Calculating...' then 42 console.log(cachedFn()); // Logs 42 (from cache) // After 1 minute, cache expires and recalculates
Debug
Known issues
gotchacacheFunc only caches the result of the first call; subsequent calls always return the same cached value if within maxAge. It does not support multiple arguments or different inputs.
fix
Use a more feature-rich library like memoizee if you need per-argument caching.
affects: >=1.0.0
gotchamaxAge is in milliseconds; passing a number without units is a common mistake.
fix
Always use milliseconds: e.g., 5000 for 5 seconds, 60000 for 1 minute.
affects: >=1.0.0
gotchacacheFunc does not cache asynchronous functions; passing an async function will return a Promise but only the first call's result is cached.
fix
Wrap the async function manually or use a library that supports async caching.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: cacheFunc is not a function
Using default import instead of named import in ESM.
fix
Use import { cacheFunc } from 'cache-func' instead of import cacheFunc from 'cache-func'.
Error: Cannot find module 'cache-func'
Package not installed or import path incorrect.
fix
Run npm install cache-func and ensure the import statement is correct.
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
cache-func — npm install cache-func · libregistry