Skip to content

Commit

Permalink
force-write
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Mar 11, 2024
1 parent 0c623eb commit 5acdfcd
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type AbstractCacheParams<F extends Func> = {
f: F;
read: (key: string) => ReturnType<F>;
write: (key: string, value: Awaited<ReturnType<F>>) => Promise<void>;
forceWrite?: boolean;
};

type Cache<Output> = Record<string, Output>;
Expand All @@ -55,7 +56,9 @@ const makeLocalReadWrite = <Output>(name: string) => {
return {
read: (key: string) =>
getCache().then((cache: Cache<Output>) =>
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();
Expand All @@ -77,23 +80,27 @@ const abstractCache = <F extends Func>({
f,
read,
write,
forceWrite,
}: AbstractCacheParams<F>): F =>
((...x: Parameters<F>) => {
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 =
Expand All @@ -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 extends Func>(f: F) => {
({ ttl, customKeyFn, forceWrite }: MemParams) => <F extends Func>(f: F) => {
const keyToValue: Record<string, Awaited<ReturnType<F>>> = {};
const keyToTimestamp: Record<string, number> = {};
return abstractCache({
Expand All @@ -128,13 +139,16 @@ export const memCache =
keyToTimestamp[key] = Date.now();
return Promise.resolve();
},
forceWrite,
});
};

const localCache =
({ cacheId, customKeyFn }: LocalCacheParams) => <F extends Func>(f: F) =>
({ cacheId, customKeyFn, forceWrite }: LocalCacheParams) =>
<F extends Func>(f: F) =>
// @ts-expect-error Promise+Awaited = nothing
abstractCache({
forceWrite,
key: inputToCacheKey<Parameters<F>>("", customKeyFn),
f,
...makeLocalReadWrite<Awaited<ReturnType<F>>>(cacheId),
Expand Down Expand Up @@ -194,6 +208,7 @@ export type CacheParams = LocalCacheParams | CloudCacheParams;

type LocalCacheParams = {
cacheId: string;
forceWrite?: boolean;
customKeyFn?: CustomKeyFn;
};

Expand All @@ -204,13 +219,15 @@ type CloudCacheParams = {
ttl?: number;
encryptionKey?: string;
customKeyFn?: CustomKeyFn;
forceWrite?: boolean;
};

export const cache = (params: CacheParams) =>
"token" in params ? cloudCache(params) : localCache(params);

const cloudCache = (params: CloudCacheParams) => <F extends Func>(f: F) =>
abstractCache({
forceWrite: params.forceWrite,
key: inputToCacheKey<Parameters<F>>(
params.encryptionKey || "",
params.customKeyFn,
Expand Down

0 comments on commit 5acdfcd

Please sign in to comment.