Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions _src/lib/node_cache.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,26 @@ module.exports = class NodeCache extends EventEmitter

return delCount

# ## take
#
# get the cached value and remove the key from the cache.
# Equivalent to calling `get(key)` + `del(key)`.
# Useful for implementing `single use` mechanism such as OTP, where once a value is read it will become obsolete.
#
# **Parameters:**
#
# * `key` ( String | Number ): cache key
#
# **Example:**
#
# myCache.take "myKey", ( err, val )
#
take: ( key )=>
_ret = @get(key)
if (_ret?)
@del(key)
return _ret

# ## ttl
#
# reset or redefine the ttl of a key. `ttl` = 0 means infinite lifetime.
Expand Down
61 changes: 61 additions & 0 deletions _src/test/mocha_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe "`#{pkg.name}@#{pkg.version}` on `node@#{process.version}`", () ->
b:
x: 2
y: 3
otp: randomString 10
return

it "set key", () ->
Expand Down Expand Up @@ -103,6 +104,66 @@ describe "`#{pkg.name}@#{pkg.version}` on `node@#{process.version}`", () ->
0.should.eql count
return

it "take key", () ->
# make sure we are starting fresh
res = localCache.has("otp")
res.should.eql false

# taking a non-exitent value should be fine
res = localCache.take("otp")
should.not.exist(res)

# check if otp insertion suceeded
res = localCache.set "otp", state.otp, 0
true.should.eql res

# are we able to check the presence of the key?
res = localCache.has("otp")
res.should.eql true

# not once, but twice?
# This proves that keys can be accessed as many times as required, but
# not the value. The `take()` method makes the values as single-read, not the keys.
res = localCache.has("otp")
res.should.eql true

# take the value
otp = localCache.take("otp")
otp.should.eql state.otp

# key should not be present anymore once the value is read
res = localCache.has("otp")
res.should.eql false

# and, re-insertions are not probhitied
res = localCache.set "otp", "some other value"
true.should.eql res

# should be able take the value again
otp = localCache.take("otp")
otp.should.eql "some other value"

# key should not be present anymore, again
res = localCache.has("otp")
res.should.eql false
return

it "take key with falsy values", () ->
# make sure we are starting fresh
res = localCache.has("otp")
res.should.eql false

# insert a falsy value and take it
res = localCache.set "otp", 0
true.should.eql res
otp = localCache.take("otp")
otp.should.eql 0

# key should not exist anymore
res = localCache.has("otp")
res.should.eql false
return

it "update key (and get it to check if the update worked)", () ->
res = localCache.set state.key, state.value2, 0
true.should.eql res
Expand Down