James Brumond 878592fbc6
All checks were successful
Build and test / build-and-test (18.x) (push) Successful in 12s
Build and test / build-and-test (20.x) (push) Successful in 17s
readme
2023-08-26 18:37:46 -07:00
2023-08-26 16:12:23 -07:00
2023-08-26 16:09:57 -07:00
2023-08-26 20:56:06 +00:00
2023-08-26 18:28:44 -07:00
2023-08-26 16:11:50 -07:00
2023-08-26 16:11:50 -07:00
2023-08-26 18:37:46 -07:00
2023-08-26 20:56:06 +00:00

In-memory caching/memoization utilities


Install

# Update project npm config to refer to correct registry for the @js scope
echo '@js:registry=https://gitea.jbrumond.me/api/packages/js/npm/' >> ./.npmrc

npm install --save @js/memo

# optional - additional supporting typescript definitions
npm install --save-dev @js/types

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,
});
Description
In-memory caching/memoization utilities
Readme 55 KiB
Languages
TypeScript 100%