diff --git a/client/src/index.ts b/client/src/index.ts index 44281fd..a646fe6 100644 --- a/client/src/index.ts +++ b/client/src/index.ts @@ -33,6 +33,7 @@ type AbstractCacheParams = { f: F; read: (key: string) => ReturnType; write: (key: string, value: Awaited>) => Promise; + forceWrite?: boolean; }; type Cache = Record; @@ -55,7 +56,9 @@ const makeLocalReadWrite = (name: string) => { return { read: (key: string) => getCache().then((cache: Cache) => - key in cache ? cache[key] : Promise.reject() + key in cache + ? cache[key] + : Promise.reject(new Error("key not in cache")) ), write: async (key: string, value: Output) => { const cache = await getCache(); @@ -77,23 +80,27 @@ const abstractCache = ({ f, read, write, + forceWrite, }: AbstractCacheParams): F => ((...x: Parameters) => { const keyResult = key(...x); - return read(keyResult).catch(() => - f(...x).then((y) => { - enrollPromise( - write(keyResult, y).catch((e) => { - console.error("failed writing to rmmbr cache", e); - }), - ); - return y; - }) - ); + return (forceWrite + ? Promise.reject(new Error("forced write is on")) + : read(keyResult)).catch(() => + f(...x).then((y) => { + enrollPromise( + write(keyResult, y).catch((e) => { + console.error("failed writing to rmmbr cache", e); + }), + ); + return y; + }) + ); }) as F; export const waitAllWrites = () => Promise.all(writePromises); +// deno-lint-ignore no-explicit-any type CustomKeyFn = (..._: any[]) => any; export const inputToCacheKey = @@ -102,10 +109,14 @@ export const inputToCacheKey = (...x: Args): string => hash(jsonStableStringify(customKeyFn ? customKeyFn(...x) : x) + secret); -type MemParams = { ttl?: number; customKeyFn?: (..._: any[]) => string }; +type MemParams = { + ttl?: number; + customKeyFn?: (..._: any[]) => string; + forceWrite?: boolean; +}; export const memCache = - ({ ttl, customKeyFn }: MemParams) => (f: F) => { + ({ ttl, customKeyFn, forceWrite }: MemParams) => (f: F) => { const keyToValue: Record>> = {}; const keyToTimestamp: Record = {}; return abstractCache({ @@ -128,13 +139,16 @@ export const memCache = keyToTimestamp[key] = Date.now(); return Promise.resolve(); }, + forceWrite, }); }; const localCache = - ({ cacheId, customKeyFn }: LocalCacheParams) => (f: F) => + ({ cacheId, customKeyFn, forceWrite }: LocalCacheParams) => + (f: F) => // @ts-expect-error Promise+Awaited = nothing abstractCache({ + forceWrite, key: inputToCacheKey>("", customKeyFn), f, ...makeLocalReadWrite>>(cacheId), @@ -194,6 +208,7 @@ export type CacheParams = LocalCacheParams | CloudCacheParams; type LocalCacheParams = { cacheId: string; + forceWrite?: boolean; customKeyFn?: CustomKeyFn; }; @@ -204,6 +219,7 @@ type CloudCacheParams = { ttl?: number; encryptionKey?: string; customKeyFn?: CustomKeyFn; + forceWrite?: boolean; }; export const cache = (params: CacheParams) => @@ -211,6 +227,7 @@ export const cache = (params: CacheParams) => const cloudCache = (params: CloudCacheParams) => (f: F) => abstractCache({ + forceWrite: params.forceWrite, key: inputToCacheKey>( params.encryptionKey || "", params.customKeyFn,