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
48 changes: 48 additions & 0 deletions _src/lib/node_cache.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,53 @@ module.exports = class NodeCache extends EventEmitter

# return true
return true


# ## mset
#
# set multiple key,value and ttl
#
# **Parameters:**
#
# * `keyValueSet` ( Object[] ): an array of object which includes key,value and ttl
#
# **Example:**
#
# myCache.mset([
# {
# key:"myKey",
# val:"myValue"
# ttl:[optionsl]
# }
# ])
#
#

mset: ( keyValueSet ) =>
# check if cache is overflowing
if (@stats.keys + keyValueSet.length >= @options.maxKeys && @options.maxKeys > -1)
_err = @_error( "ECACHEFULL" )
throw _err

# loop over keyValueSet to validate key and ttl

for keyValuePair in keyValueSet
{ key, val, ttl } = keyValuePair

# check if there is ttl and it's a number
if ttl and typeof ttl isnt "number"
_err = @_error( "ETTLTYPE" )
throw _err


# handle invalid key types
if (err = @_isInvalidKey( key ))?
throw err

for keyValuePair in keyValueSet
{ key, val, ttl } = keyValuePair
@set(key, val, ttl)
return true

# ## del
#
Expand Down Expand Up @@ -609,3 +656,4 @@ module.exports = class NodeCache extends EventEmitter
"ECACHEFULL": "Cache max key size exceeded"
"EKEYTYPE": "The key argument has to be of type `string` or `number`. Found: `__key`"
"EKEYSTYPE": "The keys argument has to be an array."
"ETTLTYPE": "The ttl argument has to be a number."
67 changes: 67 additions & 0 deletions _src/test/mocha_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ localCacheNoDelete = new nodeCache({
deleteOnExpire: false
})

localCacheMset = new nodeCache({
stdTTL: 0
})

BENCH = {}

# just for testing disable the check period
Expand Down Expand Up @@ -1107,5 +1111,68 @@ describe "`#{pkg.name}@#{pkg.version}` on `node@#{process.version}`", () ->
false.should.eql cachedRegex.test(noMatch)
return
return

describe "mset", () ->
before () ->
state =
keyValueSet: [

key: randomString 10
val: randomString 10
,
key: randomString 10
val: randomString 10

]

return

it "mset an array of key value pairs", () ->
res = localCacheMset.mset state.keyValueSet
true.should.eql res
2.should.eql localCacheMset.getStats().keys
return

it "mset - integer key", () ->
localCacheMset.flushAll()
state.keyValueSet[0].key = randomNumber 10
res = localCacheMset.mset state.keyValueSet
true.should.eql res
2.should.eql localCacheMset.getStats().keys
return

it "mset - boolean key throw error", () ->
localCacheMset.flushAll()
state.keyValueSet[0].key = true

(() -> localCacheMset.mset(state.keyValueSet)).should.throw({
name: "EKEYTYPE"
message: "The key argument has to be of type `string` or `number`. Found: `boolean`"
})
return

it "mset - object key throw error", () ->
localCacheMset.flushAll()
state.keyValueSet[0].key = { a: 1 }

(() -> localCacheMset.mset(state.keyValueSet)).should.throw({
name: "EKEYTYPE"
message: "The key argument has to be of type `string` or `number`. Found: `object`"
})
return

it "mset - ttl type error check", () ->
localCacheMset.flushAll()
state.keyValueSet[0].ttl = { a: 1 }

(() -> localCacheMset.mset(state.keyValueSet)).should.throw({
name: "ETTLTYPE"
message: "The ttl argument has to be a number."
})
return



return

return