node-cache - fix: has was not removing expired keys#1618
Merged
Conversation
has() previously returned true for expired keys, breaking the has()/get() pattern used with the original node-cache. It now checks the TTL, emits "expired", and removes the key (when deleteOnExpire is enabled), matching node-cache's behavior.
Owner
Author
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the has() method to correctly handle expired keys by returning false, emitting an 'expired' event, and optionally deleting the entry. This change aligns the behavior with the get() method and the original node-cache library. A review comment highlights a potential issue where repeated calls to has() on an expired key could cause event flooding if deleteOnExpire is set to false.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1618 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 26 26
Lines 2485 2492 +7
Branches 556 556
=========================================
+ Hits 2485 2492 +7 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
When deleteOnExpire is false, repeatedly accessing an expired key via get()/has() (or hitting it via the checkData() interval sweep) would emit the "expired" event on every call, flooding listeners. Centralize the expiration path in a handleExpired() helper and track an "expiredEmitted" flag on the entry so the event fires once. The flag is reset when the entry is re-set or its ttl is re-scheduled. mget()/take() go through get() and inherit this behavior.
Drop the expiredEmitted de-duplication. The "expired" event now fires on every access to an expired entry (get/has/checkData), regardless of the deleteOnExpire option. Callers that want one-shot semantics can track that themselves.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
has() previously returned true for expired keys, breaking the
has()/get() pattern used with the original node-cache. It now checks
the TTL, emits "expired", and removes the key (when deleteOnExpire is
enabled), matching node-cache's behavior.