Skip to content

Commit

Permalink
fix(memoize): 主动清理过期数据
Browse files Browse the repository at this point in the history
  • Loading branch information
fupengl committed Mar 17, 2022
1 parent b5398a6 commit 67438fe
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/promise/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ function memoize<FnType extends AnyPromiseFN>(

const returnFn = (async (...args: Parameters<FnType>): Promise<ThenReturn<FnType>> => {
const key = harsher(...args);
if (memos.has(key)) {
if (!timeoutMs || Date.now() < memos.get(key)!.expiration) {
return memos.get(key)!.value;
}

if (timeoutMs) {
memos.forEach((item, k) => {
if (item.expiration > Date.now()) {
memos.delete(k);
}
});
}

if (!timeoutMs || memos.has(key)) {
return memos.get(key)!.value;
}

if (queues.has(key)) {
Expand Down

0 comments on commit 67438fe

Please sign in to comment.