Skip to content

Commit

Permalink
Fix Redis get timeout re-uses promise (#27)
Browse files Browse the repository at this point in the history
* Fix Redis get timeout re-uses promise

* remove extra async
  • Loading branch information
PabloSzx committed Jan 12, 2023
1 parent b46f04b commit c4904ea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fresh-coins-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@soundxyz/fine-grained-cache": patch
---

Fix Redis get timeout re-uses promise for calls within the same event cycle
15 changes: 14 additions & 1 deletion src/fineGrained.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,19 @@ export function FineGrainedCache({
}
}

let currentTimeout: Promise<undefined> | null = null;
function timeoutRedisPromise() {
if (currentTimeout) return currentTimeout;

currentTimeout = timersSetTimeout(GETRedisTimeout, undefined);

setTimeout(() => {
currentTimeout = null;
});

return currentTimeout;
}

async function getRedisCacheValue<T>(
key: string,
useSuperjson: boolean,
Expand Down Expand Up @@ -439,7 +452,7 @@ export function FineGrainedCache({
);

const redisValue = await (GETRedisTimeout != null
? Promise.race([redisGet, timersSetTimeout(GETRedisTimeout, undefined)])
? Promise.race([redisGet, timeoutRedisPromise()])
: redisGet);

if (redisValue === undefined) {
Expand Down

0 comments on commit c4904ea

Please sign in to comment.