Make .get always return a value, even if it is expired#56
Closed
TehShrike wants to merge 1 commit intoavoidwork:masterfrom
Closed
Conversation
This fixes an issue where you could
if (cache.has(key)) {
const value = cache.get(key)
// assume that value is a legit value
}
and have issues because cache.has will return true if the item is in the map, but then the call to .get would delete the item if it was expired and return undefined.
Owner
|
No, you should never call that method. it's not for you. It's purposefully absent from the README.md. |
Author
|
Huh. I'm currently using tiny-lru as a drop-in replacement for native |
Owner
|
You should stop, it's not meant to do that. 'has()' is private because it's a duplicating statement within get() and set(); this lib has always had lazy expiration which works on get() ... so because something is in the items doesn't mean it's valid to use. Every place i use this lib i generally assign the returned value and then do a conditional statement; if it's expired or not there i get the same return result. |
4 tasks
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.
This could be an issue instead of a pull request, you can consider this a discussion starter if you want.
I ran into an issue recently when doing this:
I ran into issues because
cache.haswill return true if the item is in the map, but then the call tocache.getwould delete the item if it was expired and return undefined, and I would get runtime errors because I assumed thatcache.hasmeant that I could safely callcache.getin the same tick.