Skip to content

Commit

Permalink
Add per file listener for caching. (#1242)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored Jun 4, 2020
1 parent cbd5be9 commit aa57264
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 110 deletions.
15 changes: 12 additions & 3 deletions lib/util/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const proxyHandler = {
* @param {Object} [options] Storage options.
* @param {Boolean} [options.lodashPath=false] Set true to treat name as a lodash path.
* @param {Boolean} [options.disableCache=false] Set true to disable json object cache.
* @param {Boolean} [options.disableCacheByFile=false] Set true to cleanup cache for every fs change.
*
* @example
* class extend Generator {
Expand All @@ -62,20 +63,28 @@ class Storage {

_.defaults(options, {
lodash: false,
disableCache: false
disableCache: false,
disableCacheByFile: false
});

assert(configPath, 'A config filepath is required to create a storage');

this.path = configPath;
this.name = name;
this.fs = fs;
this.existed = Object.keys(this._store).length > 0;
this.indent = 2;
this.lodashPath = options.lodashPath;
this.disableCache = options.disableCache;
this.disableCacheByFile = options.disableCacheByFile;

this.existed = Object.keys(this._store).length > 0;

this.fs.store.on('change', filename => {
// At mem-fs 1.1.3 filename is not passed to the event.
if (this.disableCacheByFile || (filename && filename !== this.path)) {
return;
}

this.fs.store.on('change', () => {
delete this._cachedStore;
});
}
Expand Down
128 changes: 22 additions & 106 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"inquirer": "^6.3.1",
"jsdoc": "^3.6.3",
"lint-staged": "^8.1.7",
"mem-fs": "^1.1.3",
"mem-fs": "^1.2.0",
"mocha": "^7.1.1",
"mockery": "^2.1.0",
"nock": "^12.0.3",
Expand Down
41 changes: 41 additions & 0 deletions test/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,45 @@ describe('Storage', () => {
assert.deepStrictEqual({ ...proxy }, { foo: 'bar', john: 'doe' });
});
});

describe('caching', () => {
beforeEach(function() {
// Make sure the cache is loaded.
// on instantiation a change event is emitted when the file loads to mem-fs
this.store.get('foo');
});

it('loads', function() {
assert(this.store._cachedStore);
});

it("doesn't loads when disabled", function() {
const store = new Storage('test', this.fs, this.storePath, { disableCache: true });
assert(store._cachedStore === undefined);
store.get('foo');
assert(store._cachedStore === undefined);
});

it('cleanups when the file changes', function() {
this.fs.writeJSON(this.store.path, {});
assert(this.store._cachedStore === undefined);
});

it("doesn't cleanup when another file changes", function() {
this.fs.write('a.txt', 'anything');
assert(this.store._cachedStore);
});

it('cleanups when per file cache is disabled and another file changes', function() {
this.fs.writeJSON(this.store.path, { disableCacheByFile: true });
this.fs.write('a.txt', 'anything');
assert(this.store._cachedStore === undefined);
});

// Compatibility for mem-fs <= 1.1.3
it('cleanups when change event argument is undefined', function() {
this.memFs.emit('change');
assert(this.store._cachedStore === undefined);
});
});
});

0 comments on commit aa57264

Please sign in to comment.