1.1.0 • Published 5 months ago

cache-func v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

cache-func

A tiny utility to cache function outputs easily.

Installation

npm i cache-func

Usage

import { cacheFunc } from 'cache-func';

function expensiveCalculation() {
	// it could be here anything here, like a long loop, a network request, etc.
	console.log('Calculating...');
	return 42;
}

const cachedCalculation = cacheFunc(expensiveCalculation, {
	maxAge: 5 * 60 * 1000, // Cache for 5 minutes
});

console.log(cachedCalculation()); // Logs "Calculating..." and then "42"
console.log(cachedCalculation()); // Logs "42" without recalculating

API

cacheFunc(fn: Function, options?: { maxAge?: number }): Function

Wraps a function fn and returns a new function that caches the result of fn's first call and returns the cached result on subsequent calls.

License

MIT

1.1.0

5 months ago

1.0.5

5 months ago

1.0.2

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.1

6 months ago

1.0.0

6 months ago