ƒ Function Caching

Learn how to cache functions like never before.

Welcome to the place where you will learn how to easily cache functions in a single line of code to save resources.

Why cache a function?

Caching a function helps you precalculate the value it will return if the parameters (if any) are the same as in a previous execution, so you don't need to calculate it again.

Getting started

Now let's move on to the code.


Example: In the following code, we cache a function using cacheFunction and set a TTL of 5 seconds.

Caching a function
import { cacheFunction } from "toolkitify/cache";

function expensiveCalculation(x) {...};

const cachedCalc = cacheFunction(expensiveCalculation, { ttl: "5s" });
const result = cachedCalc(5);

Interfaces

ParameterTypeRequiredDescription
ttlString | NumberFunction cache lifetime
maxUses | max_usesStringMaximum number of uses
storageStorage[]Storage to use
logsLogs[]Logs to use