Skip to content

Commit 130776c

Browse files
adds test, documentation and renames method parameter
1 parent 55b4f81 commit 130776c

2 files changed

Lines changed: 92 additions & 4 deletions

File tree

_src/lib/node_cache.coffee

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,27 @@ module.exports = class NodeCache extends EventEmitter
206206
# return true
207207
return true
208208

209+
210+
# ## mset
211+
#
212+
# set multiple key,value and ttl
213+
#
214+
# **Parameters:**
215+
#
216+
# * `keyValueSet` ( Object[] ): an array of object which includes key,value and ttl
217+
#
218+
# **Example:**
219+
#
220+
# myCache.mset([
221+
# {
222+
# key:"myKey",
223+
# val:"myValue"
224+
# ttl:[optionsl]
225+
# }
226+
# ])
227+
#
228+
#
229+
209230
mset: ( keyValueSet ) =>
210231
# check if cache is overflowing
211232
if (@stats.keys + keyValueSet.length >= @options.maxKeys && @options.maxKeys > -1)
@@ -215,7 +236,7 @@ module.exports = class NodeCache extends EventEmitter
215236
# loop over keyValueSet to validate key and ttl
216237

217238
for keyValuePair in keyValueSet
218-
{ key, value, ttl } = keyValuePair
239+
{ key, val, ttl } = keyValuePair
219240

220241
# check if there is ttl and it's a number
221242
if ttl and typeof ttl isnt "number"
@@ -228,8 +249,8 @@ module.exports = class NodeCache extends EventEmitter
228249
throw err
229250

230251
for keyValuePair in keyValueSet
231-
{ key, value, ttl } = keyValuePair
232-
@set(key, value, ttl)
252+
{ key, val, ttl } = keyValuePair
253+
@set(key, val, ttl)
233254
return true
234255

235256
# ## del
@@ -635,4 +656,4 @@ module.exports = class NodeCache extends EventEmitter
635656
"ECACHEFULL": "Cache max key size exceeded"
636657
"EKEYTYPE": "The key argument has to be of type `string` or `number`. Found: `__key`"
637658
"EKEYSTYPE": "The keys argument has to be an array."
638-
"ETTLTYPE": "The ttl argument has to be an integer."
659+
"ETTLTYPE": "The ttl argument has to be a number."

_src/test/mocha_test.coffee

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ localCacheNoDelete = new nodeCache({
3434
deleteOnExpire: false
3535
})
3636

37+
localCacheMset = new nodeCache({
38+
stdTTL: 0
39+
})
40+
3741
BENCH = {}
3842

3943
# just for testing disable the check period
@@ -1107,5 +1111,68 @@ describe "`#{pkg.name}@#{pkg.version}` on `node@#{process.version}`", () ->
11071111
false.should.eql cachedRegex.test(noMatch)
11081112
return
11091113
return
1114+
1115+
describe "mset", () ->
1116+
before () ->
1117+
state =
1118+
keyValueSet: [
1119+
1120+
key: randomString 10
1121+
val: randomString 10
1122+
,
1123+
key: randomString 10
1124+
val: randomString 10
1125+
1126+
]
1127+
1128+
return
1129+
1130+
it "mset an array of key value pairs", () ->
1131+
res = localCacheMset.mset state.keyValueSet
1132+
true.should.eql res
1133+
2.should.eql localCacheMset.getStats().keys
1134+
return
1135+
1136+
it "mset - integer key", () ->
1137+
localCacheMset.flushAll()
1138+
state.keyValueSet[0].key = randomNumber 10
1139+
res = localCacheMset.mset state.keyValueSet
1140+
true.should.eql res
1141+
2.should.eql localCacheMset.getStats().keys
1142+
return
1143+
1144+
it "mset - boolean key throw error", () ->
1145+
localCacheMset.flushAll()
1146+
state.keyValueSet[0].key = true
1147+
1148+
(() -> localCacheMset.mset(state.keyValueSet)).should.throw({
1149+
name: "EKEYTYPE"
1150+
message: "The key argument has to be of type `string` or `number`. Found: `boolean`"
1151+
})
1152+
return
1153+
1154+
it "mset - object key throw error", () ->
1155+
localCacheMset.flushAll()
1156+
state.keyValueSet[0].key = { a: 1 }
1157+
1158+
(() -> localCacheMset.mset(state.keyValueSet)).should.throw({
1159+
name: "EKEYTYPE"
1160+
message: "The key argument has to be of type `string` or `number`. Found: `object`"
1161+
})
1162+
return
1163+
1164+
it "mset - ttl type error check", () ->
1165+
localCacheMset.flushAll()
1166+
state.keyValueSet[0].ttl = { a: 1 }
1167+
1168+
(() -> localCacheMset.mset(state.keyValueSet)).should.throw({
1169+
name: "ETTLTYPE"
1170+
message: "The ttl argument has to be a number."
1171+
})
1172+
return
1173+
1174+
1175+
1176+
return
11101177

11111178
return

0 commit comments

Comments
 (0)