Skip to content

Commit

Permalink
Merge pull request #12 from BJS-kr/fix/test
Browse files Browse the repository at this point in the history
fix test
  • Loading branch information
BJS-kr authored Apr 19, 2024
2 parents c623812 + edab9ef commit cc4b2c7
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 305 deletions.
19 changes: 17 additions & 2 deletions lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ import "reflect-metadata";

export const cacheEventEmitter = new EventEmitter();
export const intervalTimerMap = new Map<string, boolean>();
const timeouts: NodeJS.Timeout[] = [];
const intervals: NodeJS.Timeout[] = [];

export function clearAllTimeouts() {
for (const timeout of timeouts) {
clearTimeout(timeout);
}
for (const interval of intervals) {
clearInterval(interval);
}
}

type RootKey = `${string}${typeof ROOT_KEY_SUFFIX}`;
const ROOT_KEY_SUFFIX = "__ROOT_KEY__" as const;

Expand Down Expand Up @@ -115,7 +127,7 @@ export const Cache =
storage.set(key, result);

if (refreshIntervalSec && !intervalTimerMap.has(key)) {
setInterval(() => {
const interval = setInterval(() => {
const result = originalMethod.call(this);

result instanceof Promise
Expand All @@ -125,6 +137,7 @@ export const Cache =
: storage.set(key, result);
}, refreshIntervalSec * 1000);

intervals.push(interval);
intervalTimerMap.set(key, true);
}

Expand Down Expand Up @@ -161,13 +174,15 @@ export const Cache =

storage.set(cacheKey, result);

setTimeout(async () => {
const timeout = setTimeout(async () => {
storage.delete(cacheKey);
if (await storage.has(rootKey)) {
deleteChildCacheKey(storage, cacheKey, rootKey);
}
}, ttl * 1000);

timeouts.push(timeout);

return result;
};
}
Expand Down
Loading

0 comments on commit cc4b2c7

Please sign in to comment.