A simple in memory cache
import { withLittleCache, littleCache, TTL_5_minutes } from '@numbereight/cache';
function myExpensiveFunction(a: string, b: string): string {
fictional_sleepSync(30);
return a.slice(1) + b.slice(-1);
}
// The included "littleCache", that uses a Map<> to cache data in memory
withLittleCache(
'myCacheName', // The name of your cache; different caches use this in different ways
myExpensiveFunction, // The function to be cached
(a, b) => a+b, // How to generate the cache key
TTL_5_minutes // Cache TTL
};
// Use an external cache that supports the Cache<T> interface like this:
withCache(
littleCache('myCacheName'), // get your cache
myExpensiveFunction,
(a, b) => a+b,
TTL_5_minutes
);