Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

method for get + del #159

Closed
KrishnaPG opened this issue Nov 26, 2019 · 6 comments
Closed

method for get + del #159

KrishnaPG opened this issue Nov 26, 2019 · 6 comments

Comments

@KrishnaPG
Copy link
Contributor

For storing one-time reference items, such as OTP, it would be useful to have a method, say take, that would return the object and also delete it from the cache (so that any further get will result in undefined).

Something like below:

function take(key) {
  const val = this.get(key);
  if(val) this.delete(key);
  return val;
}

While this can be easily done at the app-level, for stronger semantics it would be nice to have in the API itself, if possible.

@sujeshthekkepatt
Copy link
Contributor

I think you can achieve the same with ttl. Store for a particular time and then automatically invalidate it. Otp's are also after a particular time frame not be re-used, I think. What do you think ?

@KrishnaPG
Copy link
Contributor Author

Single-use tokens (such as OTPs) cannot be used under two circumstances:

  1. time expired,
  2. already used

TTL solves the first problem. This take method aims to solve the second problem. The token could be active (TTL not expired), but if it is already "touched" (get() called for verification), then just delete it, so that no replay attack is possible.

@sujeshthekkepatt
Copy link
Contributor

It's a very specific use case I think. You can however make use of the current methods and implement your use case. Like get in combination with del.

@erdii
Copy link
Member

erdii commented Nov 26, 2019

Hey @KrishnaPG this sounds like a decent use-case.

PR welcome 😊

@KrishnaPG
Copy link
Contributor Author

Thank you. PR #160

@erdii
Copy link
Member

erdii commented Dec 8, 2019

Thank you! 👍 pr was merged and published in v5.0.2!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants