Skip to content

Latest commit

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

cache-func

A tiny utility to cache function outputs easily.

Installation

npm i cache-func

Usage

import{cacheFunc}from'cache-func';functionexpensiveCalculation(){// it could be anything here, like a long loop, a network request, etc.console.log('Calculating...');return42;}constcachedCalculation=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

About

A tiny utility to cache function outputs easily

Resources

Stars

19 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages