diff options
| author | 8cy <[email protected]> | 2020-04-03 02:37:42 -0700 |
|---|---|---|
| committer | 8cy <[email protected]> | 2020-04-03 02:37:42 -0700 |
| commit | 60867fb030bae582082340ead7dbc7efdc2f5398 (patch) | |
| tree | 4c6a7356351be2e4914e15c4703172597c45656e /node_modules/node-file-cache/lib/index.js | |
| parent | commenting (diff) | |
| download | s5nical-60867fb030bae582082340ead7dbc7efdc2f5398.tar.xz s5nical-60867fb030bae582082340ead7dbc7efdc2f5398.zip | |
2020/04/03, 02:34, v1.2.0
Diffstat (limited to 'node_modules/node-file-cache/lib/index.js')
| -rw-r--r-- | node_modules/node-file-cache/lib/index.js | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/node_modules/node-file-cache/lib/index.js b/node_modules/node-file-cache/lib/index.js new file mode 100644 index 0000000..c50ebe3 --- /dev/null +++ b/node_modules/node-file-cache/lib/index.js @@ -0,0 +1,118 @@ +'use strict'; +var database = require('lowdb'); +var util = require('util'); +function create(options) { + return new Cache(options || {}); +} +exports.create = create; +var Cache = (function () { + function Cache(options) { + this.set = function (key, value, options) { + var record = this._createRecord(key, value, options || {}); + this.expire(key); // remove previous + this.db.get('index').push(record).value(); + return this; + }; + this.get = function (key) { + var record = this.db.get('index').find({ key: key }).value(); + if (!record) + return null; + if (record.life < this._createTimestamp()) { + this.expire(key); + return null; // expired + } + return record.val; + }; + /** + * Clears all records from cache storage + */ + this.clear = function () { + this.db.set('index', []).value(); + return this; + }; + this.config = this._merge({ + file: 'store.json', + life: 3600 // one hour + }, options || {}); + this.db = database(this.config.file); + this.db.defaults({ + index: [] + }).value(); + } + /** + * Removes records from cache storage + */ + Cache.prototype.expire = function (value) { + var _ = this.db._; + var removed, staying; + switch (true) { + case util.isFunction(value): + // remove by filter callback + removed = this.db.get('index') + .filter(value) + .map('key') + .value(); + break; + case util.isArray(value): + // remove by tags + removed = this.db.get('index') + .filter(function (record) { return _.intersection(record.tags, value).length; }) + .map('key') + .value(); + break; + case util.isString(value): + // remove by key + removed = this.db.get('index') + .filter(function (record) { return record.key === value; }) + .map('key') + .value(); + break; + default: + throw new Error('Unsupported expiration method: ' + (typeof value)); + } + staying = this.db.get('index') + .filter(function (record) { return removed.indexOf(record.key) < 0; }) + .value(); + this._set(staying); + return this; + }; + Cache.prototype.size = function () { + return this.db.get('index').value().length; + }; + Cache.prototype._set = function (records) { + this.db.set('index', records).value(); + }; + Cache.prototype._createRecord = function (key, value, options) { + var tags = options.tags || []; + var span = options.life || this.config.life; + var life = span * 1000 + this._createTimestamp(); + return { + key: key, + val: value, + life: life, + tags: tags + }; + }; + Cache.prototype._createTimestamp = function () { + return new Date().getTime(); + }; + Cache.prototype._merge = function (a, b) { + for (var p in b) { + try { + if (b[p].constructor === Object) { + a[p] = this._merge(a[p], b[p]); + } + else { + a[p] = b[p]; + } + } + catch (e) { + a[p] = b[p]; + } + } + return a; + }; + return Cache; +}()); +exports.Cache = Cache; +//# sourceMappingURL=index.js.map
\ No newline at end of file |