diff --git a/_src/lib/node_cache.coffee b/_src/lib/node_cache.coffee index f89bfa5..847df57 100644 --- a/_src/lib/node_cache.coffee +++ b/_src/lib/node_cache.coffee @@ -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 # @@ -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." \ No newline at end of file diff --git a/_src/test/mocha_test.coffee b/_src/test/mocha_test.coffee index a180abe..c933402 100644 --- a/_src/test/mocha_test.coffee +++ b/_src/test/mocha_test.coffee @@ -34,6 +34,10 @@ localCacheNoDelete = new nodeCache({ deleteOnExpire: false }) +localCacheMset = new nodeCache({ + stdTTL: 0 +}) + BENCH = {} # just for testing disable the check period @@ -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