123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769 |
- (function() {
-
- var EventEmitter, NodeCache, _assignIn, _isArray, _isFunction, _isNumber, _isObject, _isString, _size, _template, clone,
- boundMethodCheck = function(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new Error('Bound instance method accessed before binding'); } },
- indexOf = [].indexOf;
- _assignIn = require("lodash/assignIn");
- _isArray = require("lodash/isArray");
- _isString = require("lodash/isString");
- _isFunction = require("lodash/isFunction");
- _isNumber = require("lodash/isNumber");
- _isObject = require("lodash/isObject");
- _size = require("lodash/size");
- _template = require("lodash/template");
- clone = require("clone");
- EventEmitter = require('events').EventEmitter;
-
- module.exports = NodeCache = (function() {
- class NodeCache extends EventEmitter {
- constructor(options = {}) {
- super();
-
-
-
-
-
-
-
-
-
-
- this.get = this.get.bind(this);
-
-
-
-
-
-
-
-
-
- this.mget = this.mget.bind(this);
-
-
-
-
-
-
-
-
-
-
-
-
- this.set = this.set.bind(this);
-
-
-
-
-
-
-
-
-
-
-
-
- this.del = this.del.bind(this);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- this.ttl = this.ttl.bind(this);
-
-
-
-
-
-
-
-
-
-
-
-
- this.getTtl = this.getTtl.bind(this);
-
-
-
-
-
-
-
-
-
- this.keys = this.keys.bind(this);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- this.getStats = this.getStats.bind(this);
-
-
-
-
-
-
-
-
-
-
-
-
- this.flushAll = this.flushAll.bind(this);
-
-
-
-
- this.close = this.close.bind(this);
-
-
-
- this._checkData = this._checkData.bind(this);
-
-
- this._check = this._check.bind(this);
-
-
- this._isInvalidKey = this._isInvalidKey.bind(this);
-
-
- this._wrap = this._wrap.bind(this);
-
-
- this._getValLength = this._getValLength.bind(this);
-
-
- this._error = this._error.bind(this);
-
-
- this._initErrors = this._initErrors.bind(this);
- this.options = options;
- this._initErrors();
-
- this.data = {};
-
- this.options = _assignIn({
-
- forceString: false,
-
- objectValueSize: 80,
- promiseValueSize: 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(key, cb, errorOnMissing) {
- var _err, _ret, err;
- boundMethodCheck(this, NodeCache);
-
- 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;
- }
- }
- mget(keys, cb) {
- var _err, err, i, key, len, oRet;
- boundMethodCheck(this, NodeCache);
-
- 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;
- }
- set(key, value, ttl, cb) {
- var err, existent;
- boundMethodCheck(this, NodeCache);
-
- 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;
- }
- del(keys, cb) {
- var delCount, err, i, key, len, oldVal;
- boundMethodCheck(this, NodeCache);
-
- 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;
- }
- ttl() {
- var arg, args, cb, err, i, key, len, ttl;
- boundMethodCheck(this, NodeCache);
-
- [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;
- }
- }
- 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;
- }
- }
- getTtl(key, cb) {
- var _ttl, err;
- boundMethodCheck(this, NodeCache);
- 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;
- }
- }
- keys(cb) {
- var _keys;
- boundMethodCheck(this, NodeCache);
- _keys = Object.keys(this.data);
- if (cb != null) {
- cb(null, _keys);
- }
- return _keys;
- }
- getStats() {
- boundMethodCheck(this, NodeCache);
- return this.stats;
- }
- flushAll(_startPeriod = true) {
- boundMethodCheck(this, NodeCache);
-
-
- this.data = {};
-
- this.stats = {
- hits: 0,
- misses: 0,
- keys: 0,
- ksize: 0,
- vsize: 0
- };
-
- this._killCheckPeriod();
- this._checkData(_startPeriod);
- this.emit("flush");
- }
- close() {
- boundMethodCheck(this, NodeCache);
- this._killCheckPeriod();
- }
- _checkData(startPeriod = true) {
- var key, ref, value;
- boundMethodCheck(this, NodeCache);
- 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();
- }
- }
- }
-
-
- _killCheckPeriod() {
- if (this.checkTimeout != null) {
- return clearTimeout(this.checkTimeout);
- }
- }
- _check(key, data) {
- var _retval;
- boundMethodCheck(this, NodeCache);
- _retval = true;
-
-
- if (data.t !== 0 && data.t < Date.now()) {
- if (this.options.deleteOnExpire) {
- _retval = false;
- this.del(key);
- }
- this.emit("expired", key, this._unwrap(data));
- }
- return _retval;
- }
- _isInvalidKey(key) {
- var ref;
- boundMethodCheck(this, NodeCache);
- if (ref = typeof key, indexOf.call(this.validKeyTypes, ref) < 0) {
- return this._error("EKEYTYPE", {
- type: typeof key
- });
- }
- }
- _wrap(value, ttl, asClone = true) {
- var livetime, now, oReturn, ttlMultiplicator;
- boundMethodCheck(this, NodeCache);
- 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
- };
- }
-
-
- _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;
- }
-
-
- _getKeyLength(key) {
- return key.length;
- }
- _getValLength(value) {
- boundMethodCheck(this, NodeCache);
- 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 (typeof (value != null ? value.then : void 0) === "function") {
-
-
- return this.options.promiseValueSize;
- } else if (_isObject(value)) {
-
- return this.options.objectValueSize * _size(value);
- } else {
-
- return 0;
- }
- }
- _error(type, data = {}, cb) {
- var error;
- boundMethodCheck(this, NodeCache);
-
- 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;
- }
- }
- _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);
- }
- }
- };
- NodeCache.prototype._ERRORS = {
- "ENOTFOUND": "Key `<%= key %>` not found",
- "EKEYTYPE": "The key argument has to be of type `string` or `number`. Found: `<%= type %>`",
- "EKEYSTYPE": "The keys argument has to be an array."
- };
- return NodeCache;
- }).call(this);
- }).call(this);
|