Skip to content

Commit 6441b57

Browse files
vbarik317-droidatul-upadhyay-7
authored andcommitted
fix(cache): fix ttl cache update method (JhaSourav07#2196)
## Summary Fixes an issue where `TTLCache.update()` returned `false` for expired entries but did not remove them from the internal cache store. ## Changes Made * Added immediate cleanup of expired entries inside `update()`. * Deletes the expired key from `this.store` before returning `false`. * Keeps behavior consistent with `get()` and `has()`, which already remove expired entries when encountered. ## Expected Result Expired cache entries are no longer left in memory after calling `update()`, reducing stale data retention and ensuring consistent cache behavior. Fixes JhaSourav07#1879
1 parent 14653fc commit 6441b57

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

lib/cache.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,17 @@ export class TTLCache<T> {
146146
* @returns `true` if the entry existed and was updated, `false` if missing or expired.
147147
*/
148148
update(key: string, value: T): boolean {
149-
TTLCache.assertValidKey(key);
150-
151149
const hit = this.store.get(key);
152-
if (!hit || Date.now() > hit.expiresAt) return false;
150+
151+
if (!hit) {
152+
return false;
153+
}
154+
155+
if (Date.now() > hit.expiresAt) {
156+
this.store.delete(key);
157+
return false;
158+
}
159+
153160
hit.value = value;
154161
return true;
155162
}

0 commit comments

Comments
 (0)