CacheCraft is a TypeScript-based caching library that provides a collection of classes to easily memorize and cache the results of function calls. It offers a variety of caching mechanisms including simple key-value mapping, list-based caching, and weak reference caching.
- Memorizer: Simple key-value caching.
- MemorizerList: Caching for list-like data structures.
- WeakMemorizer: WeakMap-based caching for objects.
- LastKnownMemorizer: Caches the last known value.
npm install cachecraft
or
yarn add cachecraft
import { Memorizer } from 'cachecraft';
const memo = new Memorizer<number, string>((value) => value.toString());
console.log(memo.get(1)); // "1"
import { MemorizerList } from 'cachecraft';
const memoList = new MemorizerList<number, string>((value) => value.toString());
console.log(memoList.get(1)); // "1"
import { WeakMemorizer } from 'cachecraft';
const weakMemo = new WeakMemorizer<object, string>((value) => JSON.stringify(value));
const obj = { key: 'value' };
console.log(weakMemo.get(obj)); // "{"key":"value"}"
import { LastKnownMemorizer } from 'cachecraft';
const lastKnownMemo = new LastKnownMemorizer<number, string>((value) => value.toString());
console.log(lastKnownMemo.get(1)); // "1"
You can also specify custom comparers for MemorizerList
and LastKnownMemorizer
:
import { MemorizerList, LastKnownMemorizer } from 'cachecraft';
const customComparer = (a: number, b: number) => a % 10 === b % 10;
const memoList = new MemorizerList<number, string>((value) => value.toString(), customComparer);
const lastKnownMemo = new LastKnownMemorizer<number, string>((value) => value.toString(), customComparer);
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests.
This project is licensed under the MIT License - see the LICENSE.md file for details.