Installation
@js:registry=
npm install @js/memo@0.2.0
"@js/memo": "0.2.0"
About this package
In-memory caching/memoization utilities
Install
npm install --save @js/memo
Usage
import { memo } from '@js/memo';
function get_data_from_slow_source(name: string) : string {
// ....
}
const get_data_cached = memo({
func: get_data_from_slow_source,
ttl: 30_000,
});
Custom cache key generation
import { memo } from '@js/memo';
interface SomeObject {
id: string;
foo: string;
bar: number;
baz: {
qux: string;
};
}
function get_data_from_slow_source(obj: SomeObject) : string {
// ....
}
const get_data_cached = memo({
func: get_data_from_slow_source,
ttl: 30_000,
// Pull just the `id` field from the first parameter to use
// as the cache key
key([ obj ]) {
return obj.id;
},
});
Simple functions with no params
import { memo_simple } from '@js/memo';
function get_data_from_slow_source() : string {
// ....
}
const get_data_cached = memo_simple(30_000, get_data_from_slow_source);
Full async example
import { memo_async } from '@js/memo';
async function get_data_from_slow_source(name: string) : Promise<string> {
// ....
}
const get_data_cached = memo_async({
func: get_data_from_slow_source,
ttl: 30_000,
stale_ttl: 5_000,
promise_ttl: 5_000,
});
Dependencies
Development Dependencies
ID | Version |
---|---|
@js/types | ^0.2.1 |
typescript | ^5.1.3 |