From 6f898a5f7f84003171e3850af32f1c0190ebc05e Mon Sep 17 00:00:00 2001 From: dailyrandomphoto Date: Sat, 28 Sep 2019 01:22:44 +0800 Subject: [PATCH 1/4] update test cases to verify whether the cache works properly --- test/scripts/hexo/hexo.js | 54 +++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/test/scripts/hexo/hexo.js b/test/scripts/hexo/hexo.js index c0f0c1758b..0f93448d19 100644 --- a/test/scripts/hexo/hexo.js +++ b/test/scripts/hexo/hexo.js @@ -452,17 +452,41 @@ describe('Hexo', () => { it('_generate() - reset cache for new route', () => { let count = 0; - hexo.theme.setView('test.swig', '{{ page.count }}'); + hexo.theme.setView('test.swig', '{{ page.count() }}'); hexo.extend.generator.register('test', () => ({ path: 'test', layout: 'test', - data: {count: count++} + data: {count: () => count++} })); // First generation - return hexo._generate({cache: true}).then(() => checkStream(route.get('test'), '0')).then(() => // Second generation - hexo._generate({cache: true})).then(() => checkStream(route.get('test'), '1')); + return hexo._generate({cache: true}) + .then(() => checkStream(route.get('test'), '0')) + .then(() => checkStream(route.get('test'), '0')) // should return cached result + .then(() => hexo._generate({cache: true})) // Second generation + .then(() => checkStream(route.get('test'), '1')) + .then(() => checkStream(route.get('test'), '1')); // should return cached result + }); + + it('_generate() - cache disabled and use new route', () => { + let count = 0; + + hexo.theme.setView('test.swig', '{{ page.count() }}'); + + hexo.extend.generator.register('test', () => ({ + path: 'test', + layout: 'test', + data: {count: () => count++} + })); + + // First generation + return hexo._generate({cache: false}) + .then(() => checkStream(route.get('test'), '0')) + .then(() => checkStream(route.get('test'), '1')) + .then(() => hexo._generate({cache: false})) // Second generation + .then(() => checkStream(route.get('test'), '2')) + .then(() => checkStream(route.get('test'), '3')); }); it('_generate() - cache disabled & update template', () => { @@ -473,10 +497,24 @@ describe('Hexo', () => { layout: 'test' })); - return hexo._generate({cache: false}).then(() => checkStream(route.get('test'), '0')).then(() => { - hexo.theme.setView('test.swig', '1'); - return checkStream(route.get('test'), '1'); - }); + return hexo._generate({cache: false}) + .then(() => checkStream(route.get('test'), '0')) + .then(() => hexo.theme.setView('test.swig', '1')) + .then(() => checkStream(route.get('test'), '1')); + }); + + it('_generate() - cache enabled & update template', () => { + hexo.theme.setView('test.swig', '0'); + + hexo.extend.generator.register('test', () => ({ + path: 'test', + layout: 'test' + })); + + return hexo._generate({cache: true}) + .then(() => checkStream(route.get('test'), '0')) + .then(() => hexo.theme.setView('test.swig', '1')) + .then(() => checkStream(route.get('test'), '0')); // should return cached result }); it('execFilter()', () => { From 09075b61e213dd54a34a12d933f649662dcb1e70 Mon Sep 17 00:00:00 2001 From: dailyrandomphoto Date: Sat, 28 Sep 2019 01:37:17 +0800 Subject: [PATCH 2/4] fix(fragment_cache): reset the cache on generateBefore event so we can enable cache on watch mode. --- lib/plugins/helper/fragment_cache.js | 7 ++++++- test/scripts/helpers/fragment_cache.js | 11 ++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/plugins/helper/fragment_cache.js b/lib/plugins/helper/fragment_cache.js index 95c1a9a5fe..b54468b372 100644 --- a/lib/plugins/helper/fragment_cache.js +++ b/lib/plugins/helper/fragment_cache.js @@ -1,7 +1,12 @@ 'use strict'; module.exports = ctx => { - const cache = {}; + let cache = {}; + + ctx.on('generateBefore', function() { + // reset cache for watch mode + cache = {}; + }); return function fragmentCache(id, fn) { if (this.cache && cache[id] != null) return cache[id]; diff --git a/test/scripts/helpers/fragment_cache.js b/test/scripts/helpers/fragment_cache.js index 5a816cb0cf..92ff914fe6 100644 --- a/test/scripts/helpers/fragment_cache.js +++ b/test/scripts/helpers/fragment_cache.js @@ -1,7 +1,9 @@ 'use strict'; describe('fragment_cache', () => { - const fragment_cache = require('../../../lib/plugins/helper/fragment_cache')(); + const Hexo = require('../../../lib/hexo'); + const hexo = new Hexo(__dirname); + const fragment_cache = require('../../../lib/plugins/helper/fragment_cache')(hexo); fragment_cache.call({cache: true}, 'foo', () => 123); @@ -12,4 +14,11 @@ describe('fragment_cache', () => { it('cache disabled', () => { fragment_cache.call({cache: false}, 'foo', () => 456).should.eql(456); }); + + it('should reset cache on generateBefore', () => { + fragment_cache.call({cache: true}, 'foo', () => 789).should.eql(456); + // reset cache + hexo.emit('generateBefore'); + fragment_cache.call({cache: true}, 'foo', () => 789).should.eql(789); + }); }); From 9d952e45a17e1a45a26050c28bf39d09e58de4b4 Mon Sep 17 00:00:00 2001 From: dailyrandomphoto Date: Sat, 28 Sep 2019 01:41:50 +0800 Subject: [PATCH 3/4] fix(cache): Disable save cache for rendered contents when generate files. Added at (https://github.com/hexojs/hexo/commit/e8e45ed0f379f975147a214fbf52b2c28a6c806a#diff-b9bb6fa7bb069bab9948daba90c6c3b2). Should enable save cache for rendered contents when run server mode. --- lib/hexo/index.js | 18 ++++++++++++++---- test/scripts/hexo/hexo.js | 1 + 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/hexo/index.js b/lib/hexo/index.js index 4060dc4228..07e5cfc229 100644 --- a/lib/hexo/index.js +++ b/lib/hexo/index.js @@ -51,6 +51,7 @@ function Hexo(base = process.cwd(), args = {}) { silent: Boolean(args.silent), env: process.env.NODE_ENV || 'development', version: pkg.version, + cmd: args._ ? args._[0] : '', init: false }; @@ -247,11 +248,16 @@ Hexo.prototype.load = function(callback) { this.source.process(), this.theme.process() ]); - }).then(() => this._generate({cache: true})).asCallback(callback); + }).then(() => this._generate({cache: false})).asCallback(callback); }; Hexo.prototype.watch = function(callback) { - this._watchBox = debounce(() => this._generate({cache: false}), 100); + let useCache = false; + if (this.env.cmd.startsWith('s')) { + // enable cache when run hexo server + useCache = true; + } + this._watchBox = debounce(() => this._generate({cache: useCache}), 100); return loadDatabase(this).then(() => { this.log.info('Start processing'); @@ -264,7 +270,7 @@ Hexo.prototype.watch = function(callback) { this.source.on('processAfter', this._watchBox); this.theme.on('processAfter', this._watchBox); - return this._generate({cache: false}); + return this._generate({cache: useCache}); }).asCallback(callback); }; @@ -339,6 +345,8 @@ const createLoadThemeRoute = function(generatorResult, locals, ctx) { const layout = unique(castArray(generatorResult.layout)); const layoutLength = layout.length; + // allways use cache in fragment_cache + locals.cache = true; return () => { if (useCache && routeCache.has(generatorResult)) return routeCache.get(generatorResult); @@ -349,7 +357,9 @@ const createLoadThemeRoute = function(generatorResult, locals, ctx) { if (view) { log.debug(`Rendering HTML ${name}: ${chalk.magenta(path)}`); return view.render(locals).tap(result => { - routeCache.set(generatorResult, result); + if (useCache) { + routeCache.set(generatorResult, result); + } }).tapCatch(err => { log.error({ err }, `Render HTML failed: ${chalk.magenta(path)}`); }); diff --git a/test/scripts/hexo/hexo.js b/test/scripts/hexo/hexo.js index 0f93448d19..52e98d769c 100644 --- a/test/scripts/hexo/hexo.js +++ b/test/scripts/hexo/hexo.js @@ -62,6 +62,7 @@ describe('Hexo', () => { silent: false, env: process.env.NODE_ENV || 'development', version, + cmd: '', init: false }); hexo.config_path.should.eql(pathFn.join(__dirname, '_config.yml')); From 1ff82654cfa23ded9fb10ba5577fc50b2fdd61b7 Mon Sep 17 00:00:00 2001 From: dailyrandomphoto Date: Thu, 10 Oct 2019 10:34:21 +0800 Subject: [PATCH 4/4] Change to arrow function --- lib/plugins/helper/fragment_cache.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/plugins/helper/fragment_cache.js b/lib/plugins/helper/fragment_cache.js index b54468b372..6510b8de7b 100644 --- a/lib/plugins/helper/fragment_cache.js +++ b/lib/plugins/helper/fragment_cache.js @@ -3,10 +3,8 @@ module.exports = ctx => { let cache = {}; - ctx.on('generateBefore', function() { - // reset cache for watch mode - cache = {}; - }); + // reset cache for watch mode + ctx.on('generateBefore', () => { cache = {}; }); return function fragmentCache(id, fn) { if (this.cache && cache[id] != null) return cache[id];