/** * fork from https://github.com/node-cache-manager/node-cache-manager/tree/master * reason: Support node.js v18 version below and add some features * for example: add methodWrap */ import { coalesceAsync } from './prmoiseCoalesce'; import { Cache, CachingConfig, Config, FactoryConfig, FactoryStore, MemoryCache, MemoryConfig, MemoryStore, Milliseconds, MultiCache, Store, Stores, WrapTTL, } from './types'; import { memoryStore } from './store'; export async function caching( name: 'memory', args?: MemoryConfig ): Promise; export async function caching(store: S): Promise>; export async function caching( factory: FactoryStore, args?: FactoryConfig ): Promise>; /** * Generic caching interface that wraps any caching library with a compatible interface. */ export async function caching( factory: Stores, args?: CachingConfig ): Promise | Cache | MemoryCache> { if (factory === 'memory') { const store = memoryStore(args as MemoryConfig); return createCache(store, args as MemoryConfig); } if (typeof factory === 'function') { const store = await factory(args as FactoryConfig); return createCache(store, args); } return createCache(factory, args); } export function createCache( store: MemoryStore, args?: MemoryConfig ): MemoryCache; export function createCache(store: Store, args?: Config): Cache; /** * Create cache instance by store (non-async). */ export function createCache( store: S, args?: C ): Cache { return { /** * Wraps a function in cache. I.e., the first time the function is run, * its results are stored in cache so subsequent calls retrieve from cache * instead of calling the function. * @example * const result = await cache.wrap('key', () => Promise.resolve(1)); * */ wrap: async (key: string, fn: () => Promise, ttl?: WrapTTL) => { return coalesceAsync(key, async () => { const value = await store.get(key); if (value === undefined) { const result = await fn(); const cacheTTL = typeof ttl === 'function' ? ttl(result) : ttl; await store.set(key, result, cacheTTL); return result; } else if (args?.refreshThreshold) { const cacheTTL = typeof ttl === 'function' ? ttl(value) : ttl; const remainingTtl = await store.ttl(key); if (remainingTtl !== -1 && remainingTtl < args.refreshThreshold) { coalesceAsync(`+++${key}`, fn).then(result => store.set(key, result, cacheTTL) ); } } return value; }); }, store: store as S, del: (key: string) => store.del(key), get: (key: string) => store.get(key), set: (key: string, value: unknown, ttl?: Milliseconds) => store.set(key, value, ttl), reset: () => store.reset(), /** * cache function with args */ methodWrap: async ( key: string, fn: (...args) => Promise, fnArgs: any[], ttl?: WrapTTL ): Promise => { return coalesceAsync(key, async () => { const value = await store.get(key); if (value === undefined) { const result = await fn(...fnArgs); const cacheTTL = typeof ttl === 'function' ? ttl(result) : ttl; await store.set(key, result, cacheTTL); return result; } else if (args?.refreshThreshold) { const cacheTTL = typeof ttl === 'function' ? ttl(value) : ttl; const remainingTtl = await store.ttl(key); if (remainingTtl !== -1 && remainingTtl < args.refreshThreshold) { // fn(...fnArgs).then(result => store.set(key, result, cacheTTL)); coalesceAsync(`+++${key}`, () => fn(...fnArgs)).then(result => store.set(key, result, cacheTTL) ); } } return value; }); }, }; } /** * Module that lets you specify a hierarchy of caches. */ export function multiCaching( caches: Caches ): MultiCache { const get = async (key: string) => { for (const cache of caches) { try { const val = await cache.get(key); if (val !== undefined) return val; } catch (e) { // ignore } } }; const set = async ( key: string, data: T, ttl?: Milliseconds | undefined ) => { await Promise.all(caches.map(cache => cache.set(key, data, ttl))); }; return { get, set, del: async key => { await Promise.all(caches.map(cache => cache.del(key))); }, async wrap( key: string, fn: () => Promise, ttl?: WrapTTL ): Promise { let value: T | undefined; let i = 0; for (; i < caches.length; i++) { try { value = await caches[i].get(key); if (value !== undefined) break; } catch (e) { // ignore } } if (value === undefined) { const result = await fn(); const cacheTTL = typeof ttl === 'function' ? ttl(result) : ttl; await set(key, result, cacheTTL); return result; } else { const cacheTTL = typeof ttl === 'function' ? ttl(value) : ttl; Promise.all( caches.slice(0, i).map(cache => cache.set(key, value, cacheTTL)) ).then(); caches[i].wrap(key, fn, ttl).then(); // call wrap for store for internal refreshThreshold logic, see: src/caching.ts caching.wrap } return value; }, reset: async () => { await Promise.all(caches.map(x => x.reset())); }, mget: async (...keys: string[]) => { const values = new Array(keys.length).fill(undefined); for (const cache of caches) { if (values.every(x => x !== undefined)) break; try { const val = await cache.store.mget(...keys); val.forEach((v, i) => { if (values[i] === undefined && v !== undefined) values[i] = v; }); } catch (e) { // ignore } } return values; }, mset: async (args: [string, unknown][], ttl?: Milliseconds) => { await Promise.all(caches.map(cache => cache.store.mset(args, ttl))); }, mdel: async (...keys: string[]) => { await Promise.all(caches.map(cache => cache.store.mdel(...keys))); }, methodWrap: async ( key: string, fn: (...args) => Promise, fnArgs: any[], ttl?: WrapTTL ): Promise => { let value: T | undefined; let i = 0; for (; i < caches.length; i++) { try { value = await caches[i].get(key); if (value !== undefined) break; } catch (e) { // ignore } } if (value === undefined) { const result = await fn(...fnArgs); const cacheTTL = typeof ttl === 'function' ? ttl(result) : ttl; await set(key, result, cacheTTL); return result; } else { const cacheTTL = typeof ttl === 'function' ? ttl(value) : ttl; Promise.all( caches.slice(0, i).map(cache => cache.set(key, value, cacheTTL)) ).then(); caches[i].methodWrap(key, fn, fnArgs, ttl).then(); // call wrap for store for internal refreshThreshold logic, see: src/caching.ts caching.wrap } return value; }, }; }