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
}, this.options);
this.stats = {
hits: 0,
misses: 0,
keys: 0,
ksize: 0,
vsize: 0
};
this.validKeyTypes = ["string", "number"];
this._checkData();
return;
}n/a
function EventEmitter() {
EventEmitter.init.call(this);
}n/a
init = function () {
this.domain = null;
if (EventEmitter.usingDomains) {
// if there is an active domain, then attach to it.
domain = domain || require('domain');
if (domain.active && !(this instanceof domain.Domain)) {
this.domain = domain.active;
}
}
if (!this._events || this._events === Object.getPrototypeOf(this)._events) {
this._events = new EventHandlers();
this._eventsCount = 0;
}
this._maxListeners = this._maxListeners || undefined;
}n/a
listenerCount = function (emitter, type) {
if (typeof emitter.listenerCount === 'function') {
return emitter.listenerCount(type);
} else {
return listenerCount.call(emitter, type);
}
}n/a
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
}, this.options);
this.stats = {
hits: 0,
misses: 0,
keys: 0,
ksize: 0,
vsize: 0
};
this.validKeyTypes = ["string", "number"];
this._checkData();
return;
}n/a
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
}, this.options);
this.stats = {
hits: 0,
misses: 0,
keys: 0,
ksize: 0,
vsize: 0
};
this.validKeyTypes = ["string", "number"];
this._checkData();
return;
}n/a
function EventEmitter() {
EventEmitter.init.call(this);
}n/a
init = function () {
this.domain = null;
if (EventEmitter.usingDomains) {
// if there is an active domain, then attach to it.
domain = domain || require('domain');
if (domain.active && !(this instanceof domain.Domain)) {
this.domain = domain.active;
}
}
if (!this._events || this._events === Object.getPrototypeOf(this)._events) {
this._events = new EventHandlers();
this._eventsCount = 0;
}
this._maxListeners = this._maxListeners || undefined;
}n/a
listenerCount = function (emitter, type) {
if (typeof emitter.listenerCount === 'function') {
return emitter.listenerCount(type);
} else {
return listenerCount.call(emitter, type);
}
}n/a
_check = function (key, data) {
if (data.t !== 0 && data.t < Date.now()) {
this.del(key);
this.emit("expired", key, this._unwrap(data));
return false;
} else {
return true;
}
}...
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 {
..._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();
}
}
}...
hits: 0,
misses: 0,
keys: 0,
ksize: 0,
vsize: 0
};
this.validKeyTypes = ["string", "number"];
this._checkData();
return;
}
NodeCache.prototype.get = function(key, cb, errorOnMissing) {
var _err, _ret, err;
if (typeof cb === "boolean" && arguments.length === 2) {
errorOnMissing = cb;
..._error = function (type, data, cb) {
var error;
if (data == null) {
data = {};
}
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;
}
}...
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 {
..._getKeyLength = function (key) {
return key.length;
}...
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;
..._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;
}
}...
} else {
throw err;
}
}
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++;
}
..._initErrors = function () {
var _errMsg, _errT, ref;
this.ERRORS = {};
ref = this._ERRORS;
for (_errT in ref) {
_errMsg = ref[_errT];
this.ERRORS[_errT] = _template(_errMsg);
}
}...
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,
..._isInvalidKey = function (key) {
var ref;
if (ref = typeof key, indexOf.call(this.validKeyTypes, ref) < 0) {
return this._error("EKEYTYPE", {
type: typeof key
});
}
}...
NodeCache.prototype.get = function(key, cb, errorOnMissing) {
var _err, _ret, err;
if (typeof cb === "boolean" && arguments.length === 2) {
errorOnMissing = cb;
cb = void 0;
}
if ((err = this._isInvalidKey(key)) != null) {
if (cb != null) {
cb(err);
return;
} else {
throw err;
}
}
..._killCheckPeriod = function () {
if (this.checkTimeout != null) {
return clearTimeout(this.checkTimeout);
}
}...
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();
};
..._unwrap = function (value, asClone) {
if (asClone == null) {
asClone = true;
}
if (!this.options.useClones) {
asClone = false;
}
if (value.v != null) {
if (asClone) {
return clone(value.v);
} else {
return value.v;
}
}
return null;
}...
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) {
..._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) {
livetime = 0;
} else if (ttl) {
livetime = now + (ttl * ttlMultiplicator);
} else {
if (this.options.stdTTL === 0) {
livetime = this.options.stdTTL;
} else {
livetime = now + (this.options.stdTTL * ttlMultiplicator);
}
}
return oReturn = {
t: livetime,
v: asClone ? clone(value) : value
};
}...
}
}
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) {
...close = function () {
this._killCheckPeriod();
}...
vsize: 0 // global value size count
}
*/
```
## Close the cache:
`myCache.close()`
This will clear the interval timeout which is set on check period option.
```js
myCache.close();
```
...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
}, this.options);
this.stats = {
hits: 0,
misses: 0,
keys: 0,
ksize: 0,
vsize: 0
};
this.validKeyTypes = ["string", "number"];
this._checkData();
return;
}n/a
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];
if ((err = this._isInvalidKey(key)) != null) {
if (cb != null) {
cb(err);
return;
} else {
throw err;
}
}
if (this.data[key] != null) {
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++;
}
}
if (cb != null) {
cb(null, delCount);
}
return delCount;
}...
**Since `2.0.0`**:
The method for mget changed from `.get( [ "a", "b" ] )` to `.mget( [ "a", "b" ] )`
## Delete a key (DEL):
`myCache.del( key, [callback] )`
Delete a key. Returns the number of deleted entries. A delete will never fail.
```js
myCache.del( "myKey", function( err, count ){
if( !err ){
console.log( count ); // 1
...flushAll = function (_startPeriod) {
if (_startPeriod == null) {
_startPeriod = true;
}
this.data = {};
this.stats = {
hits: 0,
misses: 0,
keys: 0,
ksize: 0,
vsize: 0
};
this._killCheckPeriod();
this._checkData(_startPeriod);
this.emit("flush");
}...
vsize: 0 // global value size count
}
*/
```
## Flush all data (FLUSH):
`myCache.flushAll()`
Flush all data.
```js
myCache.flushAll();
myCache.getStats();
/*
...get = function (key, cb, errorOnMissing) {
var _err, _ret, err;
if (typeof cb === "boolean" && arguments.length === 2) {
errorOnMissing = cb;
cb = void 0;
}
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;
}
}...
success = myCache.set( "myKey", obj, 10000 );
// true
```
## Retrieve a key (GET):
`myCache.get( key, [callback] )`
Gets a saved value from the cache.
Returns a `undefined` if not found or expired.
If the value was found it returns an object with the `key` `value` pair.
```js
myCache.get( "myKey", function( err, value ){
...getStats = function () {
return this.stats;
}...
console.log( mykeys );
// [ "all", "my", "keys", "foo", "bar" ]
```
## Statistics (STATS):
`myCache.getStats()`
Returns the statistics.
```js
myCache.getStats();
/*
{
...getTtl = function (key, cb) {
var _ttl, err;
if (!key) {
if (cb != null) {
cb(null, void 0);
}
return void 0;
}
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])) {
_ttl = this.data[key].t;
if (cb != null) {
cb(null, _ttl);
}
return _ttl;
} else {
if (cb != null) {
cb(null, void 0);
}
return void 0;
}
}...
// ... do something ...
}
});
```
## Get TTL (getTTL):
`myCache.getTtl( key, [callback] )`
Receive the ttl of a key.
You will get:
- `undefined` if the key does not exist
- `0` if this key has no ttl
- a timestamp in ms until the key expires
...keys = function (cb) {
var _keys;
_keys = Object.keys(this.data);
if (cb != null) {
cb(null, _keys);
}
return _keys;
}...
ts = myCache.getTtl( "unknownKey" )
// ts = undefined
```
## List keys (KEYS)
`myCache.keys( [callback] )`
Returns an array of all existing keys.
```js
// async
myCache.keys( function( err, mykeys ){
if( !err ){
...mget = function (keys, cb) {
var _err, err, i, key, len, oRet;
if (!_isArray(keys)) {
_err = this._error("EKEYSTYPE");
if (cb != null) {
cb(_err);
}
return _err;
}
oRet = {};
for (i = 0, len = keys.length; i < len; i++) {
key = keys[i];
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++;
oRet[key] = this._unwrap(this.data[key]);
} else {
this.stats.misses++;
}
}
if (cb != null) {
cb(null, oRet);
}
return oRet;
}...
} catch( err ){
// ENOTFOUND: Key `not-existing-key` not found
}
```
## Get multiple keys (MGET):
`myCache.mget( [ key1, key2, ... ,keyn ], [callback] )`
Gets multiple saved values from the cache.
Returns an empty object `{}` if not found or expired.
If the value was found it returns an object with the `key` `value` pair.
```js
myCache.mget( [ "myKeyA", "myKeyB" ], function( err, value ){
...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) {
if (cb != null) {
cb(err);
return;
} else {
throw err;
}
}
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;
}...
```js
const NodeCache = require( "node-cache" );
const myCache = new NodeCache( { stdTTL: 100, checkperiod: 120 } );
```
## Store a key (SET):
`myCache.set( key, val, [ ttl ], [callback] )`
Sets a `key` `value` pair. It is possible to define a `ttl` (in seconds).
Returns `true` on success.
```js
obj = { my: "Special", variable: 42 };
myCache.set( "myKey", obj, function( err, success ){
...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;
}
}
ttl || (ttl = this.options.stdTTL);
if (!key) {
if (cb != null) {
cb(null, false);
}
return false;
}
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])) {
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) {
cb(null, false);
}
return false;
}
}...
value = myCache.del( [ "A", "B", "C", "D" ] );
// 1 - because A, B and C not exists
```
## Change TTL (TTL):
`myCache.ttl( key, ttl, [callback] )`
Redefine the ttl of a key. Returns true if the key has been found and changed. Otherwise returns false.
If the ttl-argument isn't passed the default-TTL will be used.
The key will be deleted when passing in a `ttl < 0`.
```js
...