Creates and caches values under keys. 🏭
npm install cached-factoryCreates and caches values under keys. 🏭
cached-factory exports a CachedFactory class that takes in "factory" function in its constructor.
Each time a factory's .get(key) is called with any key for the first time, that factory is used to create a value under the key.
``tsCached: ${key}!
const cache = new CachedFactory((key) => );
// "Cached: apple!"
cache.get("apple");
`
Values are cached so that subsequent .get(key) calls with the same key instantly return the same value.
`ts
const cache = new CachedFactory((key) => ({ key }));
// { key: "banana" }
cache.get("banana");
// true
cache.get("banana") === cached.get("banana");
`
CachedFactory does not itself handle Promise logic, but it doesn't have to!async
Provided factory functions can themselves be / return Promise values.
`ts/some/resource?key=${key}
const cache = new CachedFactory(
async (key) => await fetch(),
);
// Type: Promise
cache.get("cherry");
// Type: Response
await cache.get("cherry");
`
#### clear
Clears the cache.
`ts`
cache.clear();
CachedFactory is written in TypeScript and ships with strong typing. 💪
> 👉 Tip: if you're working with noImplicitAny enabled _(which is generally a good idea)_, an inline function provided as an argument to CachedFactory may need an explicit type annotation for its key.`
>
> tsCached: ${key}!
> new CachedFactory((key: string) => );``
>
Josh Goldberg ✨ 💻 🖋 📖 🤔 🚇 🚧 📆 🔧 |
> 💙 This package is based on @JoshuaKGoldberg's template-typescript-node-package.