node-cache
Advanced tools
Comparing version 4.2.0 to 4.2.1
@@ -120,3 +120,3 @@ // Type definitions for node-cache 4.1 | ||
/** | ||
* flush the hole data and reset the stats | ||
* flush the whole data and reset the stats | ||
*/ | ||
@@ -123,0 +123,0 @@ flushAll(): void; |
/* | ||
* node-cache 4.1.1 ( 2018-01-31 ) | ||
* node-cache 4.2.1 ( 2019-07-24 ) | ||
* https://github.com/mpneuried/nodecache | ||
@@ -15,4 +15,4 @@ * | ||
exports.version = '4.1.1'; | ||
exports.version = '4.2.1'; | ||
}).call(this); |
/* | ||
* node-cache 4.1.1 ( 2018-01-31 ) | ||
* node-cache 4.2.1 ( 2019-07-24 ) | ||
* https://github.com/mpneuried/nodecache | ||
@@ -11,8 +11,6 @@ * | ||
(function() { | ||
// lodash requires | ||
var EventEmitter, NodeCache, _assignIn, _isArray, _isFunction, _isNumber, _isObject, _isString, _size, _template, clone, | ||
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | ||
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | ||
hasProp = {}.hasOwnProperty, | ||
slice = [].slice, | ||
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | ||
boundMethodCheck = function(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new Error('Bound instance method accessed before binding'); } }, | ||
indexOf = [].indexOf; | ||
@@ -39,100 +37,273 @@ _assignIn = require("lodash/assignIn"); | ||
module.exports = NodeCache = (function(superClass) { | ||
extend(NodeCache, superClass); | ||
// generate superclass | ||
module.exports = NodeCache = (function() { | ||
class NodeCache extends EventEmitter { | ||
constructor(options = {}) { | ||
super(); | ||
// ## get | ||
function NodeCache(options) { | ||
this.options = options != null ? options : {}; | ||
this._initErrors = bind(this._initErrors, this); | ||
this._error = bind(this._error, this); | ||
this._getValLength = bind(this._getValLength, this); | ||
this._wrap = bind(this._wrap, this); | ||
this._isInvalidKey = bind(this._isInvalidKey, this); | ||
this._check = bind(this._check, this); | ||
this._checkData = bind(this._checkData, this); | ||
this.close = bind(this.close, this); | ||
this.flushAll = bind(this.flushAll, this); | ||
this.getStats = bind(this.getStats, this); | ||
this.keys = bind(this.keys, this); | ||
this.getTtl = bind(this.getTtl, this); | ||
this.ttl = bind(this.ttl, this); | ||
this.del = bind(this.del, this); | ||
this.set = bind(this.set, this); | ||
this.mget = bind(this.mget, this); | ||
this.get = bind(this.get, this); | ||
this._initErrors(); | ||
this.data = {}; | ||
this.options = _assignIn({ | ||
forceString: false, | ||
objectValueSize: 80, | ||
arrayValueSize: 40, | ||
stdTTL: 0, | ||
checkperiod: 600, | ||
useClones: true, | ||
errorOnMissing: false, | ||
deleteOnExpire: true | ||
}, this.options); | ||
this.stats = { | ||
hits: 0, | ||
misses: 0, | ||
keys: 0, | ||
ksize: 0, | ||
vsize: 0 | ||
}; | ||
this.validKeyTypes = ["string", "number"]; | ||
this._checkData(); | ||
return; | ||
} | ||
// get a cached key and change the stats | ||
NodeCache.prototype.get = function(key, cb, errorOnMissing) { | ||
var _err, _ret, err; | ||
if (typeof cb === "boolean" && arguments.length === 2) { | ||
errorOnMissing = cb; | ||
cb = void 0; | ||
// **Parameters:** | ||
// * `key` ( String | Number ): cache key | ||
// * `[cb]` ( Function ): Callback function | ||
// * `[errorOnMissing=false]` ( Boolean ) return a error to the `cb` or throw it if no `cb` is used. Otherwise the get will return `undefined` on a miss. | ||
// **Example:** | ||
// myCache.get "myKey", ( err, val )-> | ||
// console.log( err, val ) | ||
// return | ||
this.get = this.get.bind(this); | ||
// ## mget | ||
// get multiple cached keys at once and change the stats | ||
// **Parameters:** | ||
// * `keys` ( String|Number[] ): an array of keys | ||
// * `[cb]` ( Function ): Callback function | ||
// **Example:** | ||
// myCache.mget [ "foo", "bar" ], ( err, val )-> | ||
// console.log( err, val ) | ||
// return | ||
this.mget = this.mget.bind(this); | ||
// ## set | ||
// set a cached key and change the stats | ||
// **Parameters:** | ||
// * `key` ( String | Number ): cache key | ||
// * `value` ( Any ): A element to cache. If the option `option.forceString` is `true` the module trys to translate it to a serialized JSON | ||
// * `[ ttl ]` ( Number | String ): ( optional ) The time to live in seconds. | ||
// * `[cb]` ( Function ): Callback function | ||
// **Example:** | ||
// myCache.set "myKey", "my_String Value", ( err, success )-> | ||
// console.log( err, success ) | ||
// myCache.set "myKey", "my_String Value", "10", ( err, success )-> | ||
// console.log( err, success ) | ||
this.set = this.set.bind(this); | ||
// ## del | ||
// remove keys | ||
// **Parameters:** | ||
// * `keys` ( String | Number | String|Number[] ): cache key to delete or a array of cache keys | ||
// * `[cb]` ( Function ): Callback function | ||
// **Return** | ||
// ( Number ): Number of deleted keys | ||
// **Example:** | ||
// myCache.del( "myKey" ) | ||
// myCache.del( "myKey", ( err, delCount )-> | ||
// console.log( err, delCount ) | ||
// return | ||
this.del = this.del.bind(this); | ||
// ## ttl | ||
// reset or redefine the ttl of a key. `ttl` = 0 means infinite lifetime. | ||
// If `ttl` is not passed the default ttl is used. | ||
// If `ttl` < 0 the key will be deleted. | ||
// **Parameters:** | ||
// * `key` ( String | Number ): cache key to reset the ttl value | ||
// * `ttl` ( Number ): ( optional -> options.stdTTL || 0 ) The time to live in seconds | ||
// * `[cb]` ( Function ): Callback function | ||
// **Return** | ||
// ( Boolen ): key found and ttl set | ||
// **Example:** | ||
// myCache.ttl( "myKey" ) // will set ttl to default ttl | ||
// myCache.ttl( "myKey", 1000, ( err, keyFound )-> | ||
// console.log( err, success ) | ||
this.ttl = this.ttl.bind(this); | ||
// ## getTtl | ||
// receive the ttl of a key. | ||
// **Parameters:** | ||
// * `key` ( String | Number ): cache key to check the ttl value | ||
// * `[cb]` ( Function ): Callback function | ||
// **Return** | ||
// ( Number|undefined ): The timestamp in ms when the key will expire, 0 if it will never expire or undefined if it not exists | ||
// **Example:** | ||
// ts = myCache.getTtl( "myKey" ) | ||
// myCache.getTtl( "myKey",( err, ttl )-> | ||
// console.log( err, ttl ) | ||
// return | ||
this.getTtl = this.getTtl.bind(this); | ||
// ## keys | ||
// list all keys within this cache | ||
// **Parameters:** | ||
// * `[cb]` ( Function ): Callback function | ||
// **Return** | ||
// ( Array ): An array of all keys | ||
// **Example:** | ||
// _keys = myCache.keys() | ||
// # [ "foo", "bar", "fizz", "buzz", "anotherKeys" ] | ||
this.keys = this.keys.bind(this); | ||
// ## getStats | ||
// get the stats | ||
// **Parameters:** | ||
// - | ||
// **Return** | ||
// ( Object ): Stats data | ||
// **Example:** | ||
// myCache.getStats() | ||
// # { | ||
// # hits: 0, | ||
// # misses: 0, | ||
// # keys: 0, | ||
// # ksize: 0, | ||
// # vsize: 0 | ||
// # } | ||
this.getStats = this.getStats.bind(this); | ||
// ## flushAll | ||
// flush the whole data and reset the stats | ||
// **Example:** | ||
// myCache.flushAll() | ||
// myCache.getStats() | ||
// # { | ||
// # hits: 0, | ||
// # misses: 0, | ||
// # keys: 0, | ||
// # ksize: 0, | ||
// # vsize: 0 | ||
// # } | ||
this.flushAll = this.flushAll.bind(this); | ||
// ## close | ||
// This will clear the interval timeout which is set on checkperiod option. | ||
// **Example:** | ||
// myCache.close() | ||
this.close = this.close.bind(this); | ||
// ## _checkData | ||
// internal housekeeping method. | ||
// Check all the cached data and delete the invalid values | ||
this._checkData = this._checkData.bind(this); | ||
// ## _check | ||
// internal method the check the value. If it's not valid any more delete it | ||
this._check = this._check.bind(this); | ||
// ## _isInvalidKey | ||
// internal method to check if the type of a key is either `number` or `string` | ||
this._isInvalidKey = this._isInvalidKey.bind(this); | ||
// ## _wrap | ||
// internal method to wrap a value in an object with some metadata | ||
this._wrap = this._wrap.bind(this); | ||
// ## _getValLength | ||
// internal method to calculate the value length | ||
this._getValLength = this._getValLength.bind(this); | ||
// ## _error | ||
// internal method to handle an error message | ||
this._error = this._error.bind(this); | ||
// ## _initErrors | ||
// internal method to generate error message templates | ||
this._initErrors = this._initErrors.bind(this); | ||
this.options = options; | ||
this._initErrors(); | ||
// container for cached data | ||
this.data = {}; | ||
// module options | ||
this.options = _assignIn({ | ||
// convert all elements to string | ||
forceString: false, | ||
// used standard size for calculating value size | ||
objectValueSize: 80, | ||
promiseValueSize: 80, | ||
arrayValueSize: 40, | ||
// standard time to live in seconds. 0 = infinity; | ||
stdTTL: 0, | ||
// time in seconds to check all data and delete expired keys | ||
checkperiod: 600, | ||
// en/disable cloning of variables. If `true` you'll get a copy of the cached variable. If `false` you'll save and get just the reference | ||
useClones: true, | ||
// en/disable throwing errors when trying to `.get` missing or expired values. | ||
errorOnMissing: false, | ||
// whether values should be deleted automatically at expiration | ||
deleteOnExpire: true | ||
}, this.options); | ||
// statistics container | ||
this.stats = { | ||
hits: 0, | ||
misses: 0, | ||
keys: 0, | ||
ksize: 0, | ||
vsize: 0 | ||
}; | ||
// pre allocate valid keytypes array | ||
this.validKeyTypes = ["string", "number"]; | ||
// initalize checking period | ||
this._checkData(); | ||
return; | ||
} | ||
if ((err = this._isInvalidKey(key)) != null) { | ||
if (cb != null) { | ||
cb(err); | ||
return; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
if ((this.data[key] != null) && this._check(key, this.data[key])) { | ||
this.stats.hits++; | ||
_ret = this._unwrap(this.data[key]); | ||
if (cb != null) { | ||
cb(null, _ret); | ||
} | ||
return _ret; | ||
} else { | ||
this.stats.misses++; | ||
if (this.options.errorOnMissing || errorOnMissing) { | ||
_err = this._error("ENOTFOUND", { | ||
key: key | ||
}, cb); | ||
if (_err != null) { | ||
throw _err; | ||
} | ||
return; | ||
} else { | ||
if (cb != null) { | ||
cb(null, void 0); | ||
} | ||
} | ||
return void 0; | ||
} | ||
}; | ||
NodeCache.prototype.mget = function(keys, cb) { | ||
var _err, err, i, key, len, oRet; | ||
if (!_isArray(keys)) { | ||
_err = this._error("EKEYSTYPE"); | ||
if (cb != null) { | ||
cb(_err); | ||
get(key, cb, errorOnMissing) { | ||
var _err, _ret, err; | ||
boundMethodCheck(this, NodeCache); | ||
// handle passing in errorOnMissing without cb | ||
if (typeof cb === "boolean" && arguments.length === 2) { | ||
errorOnMissing = cb; | ||
cb = void 0; | ||
} | ||
return _err; | ||
} | ||
oRet = {}; | ||
for (i = 0, len = keys.length; i < len; i++) { | ||
key = keys[i]; | ||
// handle invalid key types | ||
if ((err = this._isInvalidKey(key)) != null) { | ||
@@ -146,58 +317,84 @@ if (cb != null) { | ||
} | ||
// get data and incremet stats | ||
if ((this.data[key] != null) && this._check(key, this.data[key])) { | ||
this.stats.hits++; | ||
oRet[key] = this._unwrap(this.data[key]); | ||
_ret = this._unwrap(this.data[key]); | ||
if (cb != null) { | ||
// return data | ||
cb(null, _ret); | ||
} | ||
return _ret; | ||
} else { | ||
// if not found return a error | ||
this.stats.misses++; | ||
if (this.options.errorOnMissing || errorOnMissing) { | ||
_err = this._error("ENOTFOUND", { | ||
key: key | ||
}, cb); | ||
if (_err != null) { | ||
throw _err; | ||
} | ||
return; | ||
} else { | ||
if (cb != null) { | ||
cb(null, void 0); | ||
} | ||
} | ||
return void 0; | ||
} | ||
} | ||
if (cb != null) { | ||
cb(null, oRet); | ||
} | ||
return oRet; | ||
}; | ||
NodeCache.prototype.set = function(key, value, ttl, cb) { | ||
var err, existent; | ||
if (this.options.forceString && !_isString(value)) { | ||
value = JSON.stringify(value); | ||
} | ||
if (arguments.length === 3 && _isFunction(ttl)) { | ||
cb = ttl; | ||
ttl = this.options.stdTTL; | ||
} | ||
if ((err = this._isInvalidKey(key)) != null) { | ||
mget(keys, cb) { | ||
var _err, err, i, key, len, oRet; | ||
boundMethodCheck(this, NodeCache); | ||
// convert a string to an array of one key | ||
if (!_isArray(keys)) { | ||
_err = this._error("EKEYSTYPE"); | ||
if (cb != null) { | ||
cb(_err); | ||
} | ||
return _err; | ||
} | ||
// define return | ||
oRet = {}; | ||
for (i = 0, len = keys.length; i < len; i++) { | ||
key = keys[i]; | ||
// handle invalid key types | ||
if ((err = this._isInvalidKey(key)) != null) { | ||
if (cb != null) { | ||
cb(err); | ||
return; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
// get data and increment stats | ||
if ((this.data[key] != null) && this._check(key, this.data[key])) { | ||
this.stats.hits++; | ||
oRet[key] = this._unwrap(this.data[key]); | ||
} else { | ||
// if not found return a error | ||
this.stats.misses++; | ||
} | ||
} | ||
if (cb != null) { | ||
cb(err); | ||
return; | ||
} else { | ||
throw err; | ||
// return all found keys | ||
cb(null, oRet); | ||
} | ||
return oRet; | ||
} | ||
existent = false; | ||
if (this.data[key]) { | ||
existent = true; | ||
this.stats.vsize -= this._getValLength(this._unwrap(this.data[key], false)); | ||
} | ||
this.data[key] = this._wrap(value, ttl); | ||
this.stats.vsize += this._getValLength(value); | ||
if (!existent) { | ||
this.stats.ksize += this._getKeyLength(key); | ||
this.stats.keys++; | ||
} | ||
this.emit("set", key, value); | ||
if (cb != null) { | ||
cb(null, true); | ||
} | ||
return true; | ||
}; | ||
NodeCache.prototype.del = function(keys, cb) { | ||
var delCount, err, i, key, len, oldVal; | ||
if (!_isArray(keys)) { | ||
keys = [keys]; | ||
} | ||
delCount = 0; | ||
for (i = 0, len = keys.length; i < len; i++) { | ||
key = keys[i]; | ||
set(key, value, ttl, cb) { | ||
var err, existent; | ||
boundMethodCheck(this, NodeCache); | ||
// force the data to string | ||
if (this.options.forceString && !_isString(value)) { | ||
value = JSON.stringify(value); | ||
} | ||
// remap the arguments if `ttl` is not passed | ||
if (arguments.length === 3 && _isFunction(ttl)) { | ||
cb = ttl; | ||
ttl = this.options.stdTTL; | ||
} | ||
// handle invalid key types | ||
if ((err = this._isInvalidKey(key)) != null) { | ||
@@ -211,266 +408,354 @@ if (cb != null) { | ||
} | ||
if (this.data[key] != null) { | ||
// internal helper variables | ||
existent = false; | ||
// remove existing data from stats | ||
if (this.data[key]) { | ||
existent = true; | ||
this.stats.vsize -= this._getValLength(this._unwrap(this.data[key], false)); | ||
this.stats.ksize -= this._getKeyLength(key); | ||
this.stats.keys--; | ||
delCount++; | ||
oldVal = this.data[key]; | ||
delete this.data[key]; | ||
this.emit("del", key, oldVal.v); | ||
} else { | ||
this.stats.misses++; | ||
} | ||
// set the value | ||
this.data[key] = this._wrap(value, ttl); | ||
this.stats.vsize += this._getValLength(value); | ||
// only add the keys and key-size if the key is new | ||
if (!existent) { | ||
this.stats.ksize += this._getKeyLength(key); | ||
this.stats.keys++; | ||
} | ||
this.emit("set", key, value); | ||
if (cb != null) { | ||
// return true | ||
cb(null, true); | ||
} | ||
return true; | ||
} | ||
if (cb != null) { | ||
cb(null, delCount); | ||
} | ||
return delCount; | ||
}; | ||
NodeCache.prototype.ttl = function() { | ||
var arg, args, cb, err, i, key, len, ttl; | ||
key = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : []; | ||
for (i = 0, len = args.length; i < len; i++) { | ||
arg = args[i]; | ||
switch (typeof arg) { | ||
case "number": | ||
ttl = arg; | ||
break; | ||
case "function": | ||
cb = arg; | ||
del(keys, cb) { | ||
var delCount, err, i, key, len, oldVal; | ||
boundMethodCheck(this, NodeCache); | ||
// convert keys to an array of itself | ||
if (!_isArray(keys)) { | ||
keys = [keys]; | ||
} | ||
} | ||
ttl || (ttl = this.options.stdTTL); | ||
if (!key) { | ||
if (cb != null) { | ||
cb(null, false); | ||
delCount = 0; | ||
for (i = 0, len = keys.length; i < len; i++) { | ||
key = keys[i]; | ||
// handle invalid key types | ||
if ((err = this._isInvalidKey(key)) != null) { | ||
if (cb != null) { | ||
cb(err); | ||
return; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
// only delete if existent | ||
if (this.data[key] != null) { | ||
// calc the stats | ||
this.stats.vsize -= this._getValLength(this._unwrap(this.data[key], false)); | ||
this.stats.ksize -= this._getKeyLength(key); | ||
this.stats.keys--; | ||
delCount++; | ||
// delete the value | ||
oldVal = this.data[key]; | ||
delete this.data[key]; | ||
// return true | ||
this.emit("del", key, oldVal.v); | ||
} else { | ||
// if the key has not been found return an error | ||
this.stats.misses++; | ||
} | ||
} | ||
return false; | ||
} | ||
if ((err = this._isInvalidKey(key)) != null) { | ||
if (cb != null) { | ||
cb(err); | ||
return; | ||
} else { | ||
throw err; | ||
cb(null, delCount); | ||
} | ||
return delCount; | ||
} | ||
if ((this.data[key] != null) && this._check(key, this.data[key])) { | ||
if (ttl >= 0) { | ||
this.data[key] = this._wrap(this.data[key].v, ttl, false); | ||
} else { | ||
this.del(key); | ||
ttl() { | ||
var arg, args, cb, err, i, key, len, ttl; | ||
boundMethodCheck(this, NodeCache); | ||
// change args if only key and callback are passed | ||
[key, ...args] = arguments; | ||
for (i = 0, len = args.length; i < len; i++) { | ||
arg = args[i]; | ||
switch (typeof arg) { | ||
case "number": | ||
ttl = arg; | ||
break; | ||
case "function": | ||
cb = arg; | ||
} | ||
} | ||
if (cb != null) { | ||
cb(null, true); | ||
ttl || (ttl = this.options.stdTTL); | ||
if (!key) { | ||
if (cb != null) { | ||
cb(null, false); | ||
} | ||
return false; | ||
} | ||
return true; | ||
} else { | ||
if (cb != null) { | ||
cb(null, false); | ||
// handle invalid key types | ||
if ((err = this._isInvalidKey(key)) != null) { | ||
if (cb != null) { | ||
cb(err); | ||
return; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
return false; | ||
// check for existant data and update the ttl value | ||
if ((this.data[key] != null) && this._check(key, this.data[key])) { | ||
// if ttl < 0 delete the key. otherwise reset the value | ||
if (ttl >= 0) { | ||
this.data[key] = this._wrap(this.data[key].v, ttl, false); | ||
} else { | ||
this.del(key); | ||
} | ||
if (cb != null) { | ||
cb(null, true); | ||
} | ||
return true; | ||
} else { | ||
if (cb != null) { | ||
// return false if key has not been found | ||
cb(null, false); | ||
} | ||
return false; | ||
} | ||
} | ||
}; | ||
NodeCache.prototype.getTtl = function(key, cb) { | ||
var _ttl, err; | ||
if (!key) { | ||
if (cb != null) { | ||
cb(null, void 0); | ||
getTtl(key, cb) { | ||
var _ttl, err; | ||
boundMethodCheck(this, NodeCache); | ||
if (!key) { | ||
if (cb != null) { | ||
cb(null, void 0); | ||
} | ||
return void 0; | ||
} | ||
return void 0; | ||
} | ||
if ((err = this._isInvalidKey(key)) != null) { | ||
if (cb != null) { | ||
cb(err); | ||
return; | ||
// handle invalid key types | ||
if ((err = this._isInvalidKey(key)) != null) { | ||
if (cb != null) { | ||
cb(err); | ||
return; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
// check for existant data and update the ttl value | ||
if ((this.data[key] != null) && this._check(key, this.data[key])) { | ||
_ttl = this.data[key].t; | ||
if (cb != null) { | ||
cb(null, _ttl); | ||
} | ||
return _ttl; | ||
} else { | ||
throw err; | ||
if (cb != null) { | ||
// return undefined if key has not been found | ||
cb(null, void 0); | ||
} | ||
return void 0; | ||
} | ||
} | ||
if ((this.data[key] != null) && this._check(key, this.data[key])) { | ||
_ttl = this.data[key].t; | ||
keys(cb) { | ||
var _keys; | ||
boundMethodCheck(this, NodeCache); | ||
_keys = Object.keys(this.data); | ||
if (cb != null) { | ||
cb(null, _ttl); | ||
cb(null, _keys); | ||
} | ||
return _ttl; | ||
} else { | ||
if (cb != null) { | ||
cb(null, void 0); | ||
} | ||
return void 0; | ||
return _keys; | ||
} | ||
}; | ||
NodeCache.prototype.keys = function(cb) { | ||
var _keys; | ||
_keys = Object.keys(this.data); | ||
if (cb != null) { | ||
cb(null, _keys); | ||
getStats() { | ||
boundMethodCheck(this, NodeCache); | ||
return this.stats; | ||
} | ||
return _keys; | ||
}; | ||
NodeCache.prototype.getStats = function() { | ||
return this.stats; | ||
}; | ||
flushAll(_startPeriod = true) { | ||
boundMethodCheck(this, NodeCache); | ||
// parameter just for testing | ||
NodeCache.prototype.flushAll = function(_startPeriod) { | ||
if (_startPeriod == null) { | ||
_startPeriod = true; | ||
// set data empty | ||
this.data = {}; | ||
// reset stats | ||
this.stats = { | ||
hits: 0, | ||
misses: 0, | ||
keys: 0, | ||
ksize: 0, | ||
vsize: 0 | ||
}; | ||
// reset check period | ||
this._killCheckPeriod(); | ||
this._checkData(_startPeriod); | ||
this.emit("flush"); | ||
} | ||
this.data = {}; | ||
this.stats = { | ||
hits: 0, | ||
misses: 0, | ||
keys: 0, | ||
ksize: 0, | ||
vsize: 0 | ||
}; | ||
this._killCheckPeriod(); | ||
this._checkData(_startPeriod); | ||
this.emit("flush"); | ||
}; | ||
NodeCache.prototype.close = function() { | ||
this._killCheckPeriod(); | ||
}; | ||
close() { | ||
boundMethodCheck(this, NodeCache); | ||
this._killCheckPeriod(); | ||
} | ||
NodeCache.prototype._checkData = function(startPeriod) { | ||
var key, ref, value; | ||
if (startPeriod == null) { | ||
startPeriod = true; | ||
} | ||
ref = this.data; | ||
for (key in ref) { | ||
value = ref[key]; | ||
this._check(key, value); | ||
} | ||
if (startPeriod && this.options.checkperiod > 0) { | ||
this.checkTimeout = setTimeout(this._checkData, this.options.checkperiod * 1000, startPeriod); | ||
if (this.checkTimeout.unref != null) { | ||
this.checkTimeout.unref(); | ||
_checkData(startPeriod = true) { | ||
var key, ref, value; | ||
boundMethodCheck(this, NodeCache); | ||
ref = this.data; | ||
// run the housekeeping method | ||
for (key in ref) { | ||
value = ref[key]; | ||
this._check(key, value); | ||
} | ||
if (startPeriod && this.options.checkperiod > 0) { | ||
this.checkTimeout = setTimeout(this._checkData, this.options.checkperiod * 1000, startPeriod); | ||
if (this.checkTimeout.unref != null) { | ||
this.checkTimeout.unref(); | ||
} | ||
} | ||
} | ||
}; | ||
NodeCache.prototype._killCheckPeriod = function() { | ||
if (this.checkTimeout != null) { | ||
return clearTimeout(this.checkTimeout); | ||
// ## _killCheckPeriod | ||
// stop the checkdata period. Only needed to abort the script in testing mode. | ||
_killCheckPeriod() { | ||
if (this.checkTimeout != null) { | ||
return clearTimeout(this.checkTimeout); | ||
} | ||
} | ||
}; | ||
NodeCache.prototype._check = function(key, data) { | ||
var _retval; | ||
_retval = true; | ||
if (data.t !== 0 && data.t < Date.now()) { | ||
if (this.options.deleteOnExpire) { | ||
_retval = false; | ||
this.del(key); | ||
_check(key, data) { | ||
var _retval; | ||
boundMethodCheck(this, NodeCache); | ||
_retval = true; | ||
// data is invalid if the ttl is too old and is not 0 | ||
// console.log data.t < Date.now(), data.t, Date.now() | ||
if (data.t !== 0 && data.t < Date.now()) { | ||
if (this.options.deleteOnExpire) { | ||
_retval = false; | ||
this.del(key); | ||
} | ||
this.emit("expired", key, this._unwrap(data)); | ||
} | ||
this.emit("expired", key, this._unwrap(data)); | ||
return _retval; | ||
} | ||
return _retval; | ||
}; | ||
NodeCache.prototype._isInvalidKey = function(key) { | ||
var ref; | ||
if (ref = typeof key, indexOf.call(this.validKeyTypes, ref) < 0) { | ||
return this._error("EKEYTYPE", { | ||
type: typeof key | ||
}); | ||
_isInvalidKey(key) { | ||
var ref; | ||
boundMethodCheck(this, NodeCache); | ||
if (ref = typeof key, indexOf.call(this.validKeyTypes, ref) < 0) { | ||
return this._error("EKEYTYPE", { | ||
type: typeof key | ||
}); | ||
} | ||
} | ||
}; | ||
NodeCache.prototype._wrap = function(value, ttl, asClone) { | ||
var livetime, now, oReturn, ttlMultiplicator; | ||
if (asClone == null) { | ||
asClone = true; | ||
} | ||
if (!this.options.useClones) { | ||
asClone = false; | ||
} | ||
now = Date.now(); | ||
livetime = 0; | ||
ttlMultiplicator = 1000; | ||
if (ttl === 0) { | ||
_wrap(value, ttl, asClone = true) { | ||
var livetime, now, oReturn, ttlMultiplicator; | ||
boundMethodCheck(this, NodeCache); | ||
if (!this.options.useClones) { | ||
asClone = false; | ||
} | ||
// define the time to live | ||
now = Date.now(); | ||
livetime = 0; | ||
} else if (ttl) { | ||
livetime = now + (ttl * ttlMultiplicator); | ||
} else { | ||
if (this.options.stdTTL === 0) { | ||
livetime = this.options.stdTTL; | ||
ttlMultiplicator = 1000; | ||
// use given ttl | ||
if (ttl === 0) { | ||
livetime = 0; | ||
} else if (ttl) { | ||
livetime = now + (ttl * ttlMultiplicator); | ||
} else { | ||
livetime = now + (this.options.stdTTL * ttlMultiplicator); | ||
// use standard ttl | ||
if (this.options.stdTTL === 0) { | ||
livetime = this.options.stdTTL; | ||
} else { | ||
livetime = now + (this.options.stdTTL * ttlMultiplicator); | ||
} | ||
} | ||
// return the wrapped value | ||
return oReturn = { | ||
t: livetime, | ||
v: asClone ? clone(value) : value | ||
}; | ||
} | ||
return oReturn = { | ||
t: livetime, | ||
v: asClone ? clone(value) : value | ||
}; | ||
}; | ||
NodeCache.prototype._unwrap = function(value, asClone) { | ||
if (asClone == null) { | ||
asClone = true; | ||
// ## _unwrap | ||
// internal method to extract get the value out of the wrapped value | ||
_unwrap(value, asClone = true) { | ||
if (!this.options.useClones) { | ||
asClone = false; | ||
} | ||
if (value.v != null) { | ||
if (asClone) { | ||
return clone(value.v); | ||
} else { | ||
return value.v; | ||
} | ||
} | ||
return null; | ||
} | ||
if (!this.options.useClones) { | ||
asClone = false; | ||
// ## _getKeyLength | ||
// internal method the calculate the key length | ||
_getKeyLength(key) { | ||
return key.length; | ||
} | ||
if (value.v != null) { | ||
if (asClone) { | ||
return clone(value.v); | ||
_getValLength(value) { | ||
boundMethodCheck(this, NodeCache); | ||
if (_isString(value)) { | ||
// if the value is a String get the real length | ||
return value.length; | ||
} else if (this.options.forceString) { | ||
// force string if it's defined and not passed | ||
return JSON.stringify(value).length; | ||
} else if (_isArray(value)) { | ||
// if the data is an Array multiply each element with a defined default length | ||
return this.options.arrayValueSize * value.length; | ||
} else if (_isNumber(value)) { | ||
return 8; | ||
} else if (typeof (value != null ? value.then : void 0) === "function") { | ||
// if the data is a Promise, use defined default | ||
// (can't calculate actual/resolved value size synchronously) | ||
return this.options.promiseValueSize; | ||
} else if (_isObject(value)) { | ||
// if the data is an Object multiply each element with a defined default length | ||
return this.options.objectValueSize * _size(value); | ||
} else { | ||
return value.v; | ||
// default fallback | ||
return 0; | ||
} | ||
} | ||
return null; | ||
}; | ||
NodeCache.prototype._getKeyLength = function(key) { | ||
return key.length; | ||
}; | ||
NodeCache.prototype._getValLength = function(value) { | ||
if (_isString(value)) { | ||
return value.length; | ||
} else if (this.options.forceString) { | ||
return JSON.stringify(value).length; | ||
} else if (_isArray(value)) { | ||
return this.options.arrayValueSize * value.length; | ||
} else if (_isNumber(value)) { | ||
return 8; | ||
} else if (_isObject(value)) { | ||
return this.options.objectValueSize * _size(value); | ||
} else { | ||
return 0; | ||
_error(type, data = {}, cb) { | ||
var error; | ||
boundMethodCheck(this, NodeCache); | ||
// generate the error object | ||
error = new Error(); | ||
error.name = type; | ||
error.errorcode = type; | ||
error.message = this.ERRORS[type] != null ? this.ERRORS[type](data) : "-"; | ||
error.data = data; | ||
if (cb && _isFunction(cb)) { | ||
// return the error | ||
cb(error, null); | ||
} else { | ||
// if no callback is defined return the error object | ||
return error; | ||
} | ||
} | ||
}; | ||
NodeCache.prototype._error = function(type, data, cb) { | ||
var error; | ||
if (data == null) { | ||
data = {}; | ||
_initErrors() { | ||
var _errMsg, _errT, ref; | ||
boundMethodCheck(this, NodeCache); | ||
this.ERRORS = {}; | ||
ref = this._ERRORS; | ||
for (_errT in ref) { | ||
_errMsg = ref[_errT]; | ||
this.ERRORS[_errT] = _template(_errMsg); | ||
} | ||
} | ||
error = new Error(); | ||
error.name = type; | ||
error.errorcode = type; | ||
error.message = this.ERRORS[type] != null ? this.ERRORS[type](data) : "-"; | ||
error.data = data; | ||
if (cb && _isFunction(cb)) { | ||
cb(error, null); | ||
} else { | ||
return error; | ||
} | ||
}; | ||
NodeCache.prototype._initErrors = function() { | ||
var _errMsg, _errT, ref; | ||
this.ERRORS = {}; | ||
ref = this._ERRORS; | ||
for (_errT in ref) { | ||
_errMsg = ref[_errT]; | ||
this.ERRORS[_errT] = _template(_errMsg); | ||
} | ||
}; | ||
@@ -486,4 +771,4 @@ | ||
})(EventEmitter); | ||
}).call(this); | ||
}).call(this); |
@@ -28,3 +28,3 @@ { | ||
], | ||
"version": "4.2.0", | ||
"version": "4.2.1", | ||
"author": "mpneuried <mp@tcs.de>", | ||
@@ -40,3 +40,3 @@ "maintainers": [ | ||
"email": "erdiicodes@gmail.com", | ||
"url": "https://blog.werise.de/" | ||
"url": "https://erdii.engineering/" | ||
} | ||
@@ -56,3 +56,3 @@ ], | ||
"scripts": { | ||
"test": "COFFEECOV_INIT_ALL=false mocha --compilers coffee:coffee-script/register --require coffee-coverage/register-istanbul _src/test/mocha_test.coffee -R spec && tsc", | ||
"test": "COFFEECOV_INIT_ALL=false mocha --require coffee-script/register --require coffee-coverage/register-istanbul _src/test/mocha_test.coffee -R spec && tsc", | ||
"test-docker": "SILENT_MODE=1 mocha test/mocha_test.js -R min && tsc", | ||
@@ -63,21 +63,20 @@ "build": "grunt build" | ||
"clone": "2.x", | ||
"lodash": "4.x" | ||
"lodash": "^4.17.15" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^8.9.4", | ||
"coffee-coverage": "1.x", | ||
"coffee-coverage": "^3.0.1", | ||
"coffee-script": "1.x", | ||
"coveralls": "2.x", | ||
"grunt": "0.4.x", | ||
"coveralls": "^3.0.3", | ||
"grunt": "^1.0.4", | ||
"grunt-banner": "0.6.x", | ||
"grunt-cli": "^1.2.0", | ||
"grunt-contrib-clean": "1.0.x", | ||
"grunt-contrib-coffee": "1.0.x", | ||
"grunt-contrib-watch": "1.x", | ||
"grunt-contrib-coffee": "^2.1.0", | ||
"grunt-contrib-watch": "^1.1.0", | ||
"grunt-include-replace": "3.2.x", | ||
"grunt-mocha-cli": "2.x", | ||
"grunt-regarde": "0.1.x", | ||
"grunt-run": "0.5.x", | ||
"grunt-mocha-cli": "^4.0.0", | ||
"grunt-run": "^0.8.1", | ||
"istanbul": "0.x", | ||
"mocha": "3.x", | ||
"mocha": "^6.1.4", | ||
"should": "11.x", | ||
@@ -84,0 +83,0 @@ "typescript": "^2.6.1" |
@@ -20,7 +20,9 @@ node-cache | ||
**Since `4.1.0`**: | ||
*Key-validation*: The keys can be given as either `string` or `number`, but are casted to a `string` internally anyway. | ||
All other types will either throw an error or call the callback with an error. | ||
## ATTENTION - BREAKING MAJOR RELEASE INCOMING!!! | ||
The upcoming 5.0.0 Release will drop support for node versions before 6.x! | ||
(We are thinking about dropping node 6.x too, because it recently reached end-of-life.) | ||
# Install | ||
@@ -52,3 +54,3 @@ | ||
**Note:** `true` is recommended, because it'll behave like a server-based caching. You should set `false` if you want to save mutable objects or other complex types with mutability involved and wanted. | ||
_Here's a [simple code exmaple](https://runkit.com/mpneuried/useclones-example-83) showing the different behavior_ | ||
_Here's a [simple code example](https://runkit.com/mpneuried/useclones-example-83) showing the different behavior_ | ||
- `deleteOnExpire`: *(default: `true`)* whether variables will be deleted automatically when they expire. | ||
@@ -62,2 +64,6 @@ If `true` the variable will be deleted. If `false` the variable will remain. You are encouraged to handle the variable upon the event `expired` by yourself. | ||
**Since `4.1.0`**: | ||
*Key-validation*: The keys can be given as either `string` or `number`, but are casted to a `string` internally anyway. | ||
All other types will either throw an error or call the callback with an error. | ||
## Store a key (SET): | ||
@@ -524,2 +530,3 @@ | ||
|:--:|:--:|:--| | ||
|4.2.1|2019-07-22|Upgrade lodash to version 4.17.15 to suppress messages about unrelated security vulnerability| | ||
|4.2.0|2018-02-01|Add options.promiseValueSize for promise value. Thanks to [Ryan Roemer](https://github.com/ryan-roemer) for the pull [#84]; Added option `deleteOnExpire`; Added DefinitelyTyped Typescript definitions. Thanks to [Ulf Seltmann](https://github.com/useltmann) for the pulls [#90] and [#92]; Thanks to [Daniel Jin](https://github.com/danieljin) for the readme fix in pull [#93]; Optimized test and ci configs.| | ||
@@ -526,0 +533,0 @@ |4.1.1|2016-12-21|fix internal check interval for node < 0.10.25, thats the default node for ubuntu 14.04. Thanks to [Jimmy Hwang](https://github.com/JimmyHwang) for the pull [#78](https://github.com/mpneuried/nodecache/pull/78); added more docker tests| |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54491
17
9
1011
599
Updatedlodash@^4.17.15