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 | |
| 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')
| -rw-r--r-- | node_modules/node-file-cache/lib/index.d.ts | 35 | ||||
| -rw-r--r-- | node_modules/node-file-cache/lib/index.js | 118 | ||||
| -rw-r--r-- | node_modules/node-file-cache/lib/index.js.map | 1 |
3 files changed, 154 insertions, 0 deletions
diff --git a/node_modules/node-file-cache/lib/index.d.ts b/node_modules/node-file-cache/lib/index.d.ts new file mode 100644 index 0000000..c2fd02b --- /dev/null +++ b/node_modules/node-file-cache/lib/index.d.ts @@ -0,0 +1,35 @@ +export interface ICacheOptions { + file?: string; + life?: number; +} +export interface IRecordOptions { + tags?: string[]; + life?: number; +} +export interface Record { + key: string; + val: any; + life: number; + tags: string[]; +} +export declare function create(options?: ICacheOptions): Cache; +export declare class Cache { + private config; + private db; + constructor(options?: ICacheOptions); + set: (key: string, value: any, options?: IRecordOptions) => Cache; + get: (key: string) => any; + /** + * Clears all records from cache storage + */ + clear: () => Cache; + /** + * Removes records from cache storage + */ + expire(value: any): Cache; + size(): number; + private _set(records); + private _createRecord(key, value, options); + private _createTimestamp(); + private _merge(a, b); +} 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 diff --git a/node_modules/node-file-cache/lib/index.js.map b/node_modules/node-file-cache/lib/index.js.map new file mode 100644 index 0000000..08f9497 --- /dev/null +++ b/node_modules/node-file-cache/lib/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,IAAY,QAAQ,WAAM,OAAO,CAAC,CAAA;AAElC,IAAY,IAAI,WAAM,MAAM,CAAC,CAAA;AAsB7B,gBAAwB,OAAuB;IAE3C,MAAM,CAAC,IAAI,KAAK,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;AACpC,CAAC;AAHe,cAAM,SAGrB,CAAA;AAED;IAKI,eAAa,OAAuB;QAa7B,QAAG,GAAG,UAAU,GAAW,EAAE,KAAU,EAAE,OAAwB;YAEpE,IAAI,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,KAAK,EAAE,OAAO,IAAI,EAAE,CAAC,CAAC;YAE3D,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAG,kBAAkB;YACtC,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,CAAC;YAC1C,MAAM,CAAC,IAAI,CAAC;QAChB,CAAC,CAAA;QAEM,QAAG,GAAG,UAAU,GAAW;YAE9B,IAAI,MAAM,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC;YAE7D,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;gBAAC,MAAM,CAAC,IAAI,CAAC;YAEzB,EAAE,CAAC,CAAC,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC,gBAAgB,EAAE,CAAC,CAAC,CAAC;gBACxC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBACjB,MAAM,CAAC,IAAI,CAAC,CAAI,WAAW;YAC/B,C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