Skip to content

Commit 7c3be0d

Browse files
committed
feat(take): Added take() method for single-use values
Closes #159
1 parent 6117b2b commit 7c3be0d

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

_src/lib/node_cache.coffee

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,26 @@ module.exports = class NodeCache extends EventEmitter
287287

288288
return delCount
289289

290+
# ## take
291+
#
292+
# get the cached value and remove the key from the cache.
293+
# Equivalent to calling `get(key)` + `del(key)`.
294+
# Useful for implementing `single use` mechanism such as OTP, where once a value is read it will become obsolete.
295+
#
296+
# **Parameters:**
297+
#
298+
# * `key` ( String | Number ): cache key
299+
#
300+
# **Example:**
301+
#
302+
# myCache.take "myKey", ( err, val )
303+
#
304+
take: ( key )=>
305+
_ret = @get(key)
306+
if (_ret)
307+
@del(key)
308+
return _ret
309+
290310
# ## ttl
291311
#
292312
# reset or redefine the ttl of a key. `ttl` = 0 means infinite lifetime.

_src/test/mocha_test.coffee

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ describe "`#{pkg.name}@#{pkg.version}` on `node@#{process.version}`", () ->
7070
b:
7171
x: 2
7272
y: 3
73+
otp: randomString 10
7374
return
7475

7576
it "set key", () ->
@@ -103,6 +104,50 @@ describe "`#{pkg.name}@#{pkg.version}` on `node@#{process.version}`", () ->
103104
0.should.eql count
104105
return
105106

107+
it "take key", () ->
108+
# make sure we are starting fresh
109+
res = localCache.has("otp")
110+
res.should.eql false
111+
112+
# taking a non-exitent value should be fine
113+
res = localCache.take("otp")
114+
should.not.exist(res)
115+
116+
# check if otp insertion suceeded
117+
res = localCache.set "otp", state.otp, 0
118+
true.should.eql res
119+
120+
# are we able to check the presence of the key?
121+
res = localCache.has("otp")
122+
res.should.eql true
123+
124+
# not once, but twice?
125+
# This proves that keys can be accessed as many times as required, but
126+
# not the value. The `take()` method makes the values as single-read, not the keys.
127+
res = localCache.has("otp")
128+
res.should.eql true
129+
130+
# take the value
131+
otp = localCache.take("otp")
132+
otp.should.eql state.otp
133+
134+
# key should not be present anymore once the value is read
135+
res = localCache.has("otp")
136+
res.should.eql false
137+
138+
# and, re-insertions are not probhitied
139+
res = localCache.set "otp", "some other value"
140+
true.should.eql res
141+
142+
# should be able take the value again
143+
otp = localCache.take("otp")
144+
otp.should.eql "some other value"
145+
146+
# key should not be present anymore, again
147+
res = localCache.has("otp")
148+
res.should.eql false
149+
return
150+
106151
it "update key (and get it to check if the update worked)", () ->
107152
res = localCache.set state.key, state.value2, 0
108153
true.should.eql res

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"node": ">= 0.4.6"
5252
},
5353
"scripts": {
54-
"test": "COFFEECOV_INIT_ALL=false mocha --require coffee-script/register --require coffee-coverage/register-istanbul _src/test/mocha_test.coffee -R spec && tsc",
54+
"test": "mocha --require coffee-script/register --require coffee-coverage/register-istanbul _src/test/mocha_test.coffee -R spec && tsc",
5555
"build": "grunt build"
5656
},
5757
"dependencies": {

0 commit comments

Comments
 (0)