From a7661d70fa65f0749758abe60ed128bd4f6568bb Mon Sep 17 00:00:00 2001 From: jiawei686 <892001108@qq.com> Date: Tue, 25 Jan 2022 14:33:41 +0800 Subject: [PATCH] feat: improve content cache for paragraph --- src/core/ParagraphBase.js | 46 +++++++++++++++++++++++++++++++-------- src/core/hooks/List.js | 15 ++++++------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/core/ParagraphBase.js b/src/core/ParagraphBase.js index 03bd8724..8d4b6f81 100644 --- a/src/core/ParagraphBase.js +++ b/src/core/ParagraphBase.js @@ -28,7 +28,8 @@ export default class ParagraphBase extends SyntaxBase { constructor({ needCache, defaultCache = {} } = { needCache: false }) { super({}); - this.cacheState = !!needCache; + this.needCache = !!needCache; + this.sign = ''; if (needCache) { this.cache = defaultCache || {}; this.cacheKey = `~~C${cacheCounter}`; @@ -36,6 +37,10 @@ export default class ParagraphBase extends SyntaxBase { } } + toHtml(str, sentenceMakeFunc) { + return str; + } + makeHtml(str, sentenceMakeFunc) { return sentenceMakeFunc(str).html; } @@ -159,31 +164,39 @@ export default class ParagraphBase extends SyntaxBase { * @return {string} cacheKey ~~C0I0_L1$ */ pushCache(str, sign = '', lineCount = 0) { - if (!this.cacheState) { + if (!this.needCache) { return; } const $sign = sign || this.$engine.md5(str); - this.cache[$sign] = str; + this.cache[$sign] = { + content: str, + using: true, + }; return `${this.cacheKey}I${$sign}_L${lineCount}$`; } popCache(sign) { - if (!this.cacheState) { + if (!this.needCache) { return; } - return this.cache[sign] || ''; + return this.cache[sign].content || ''; } - resetCache(defaultCache) { - if (!this.cacheState) { + resetCache() { + if (!this.needCache) { return; } - this.cache = defaultCache || {}; + for (let key in this.cache) { + if (!this.cache[key].using) delete this.cache[key]; + } + for (let key in this.cache) { + this.cache[key].using = false; + } } restoreCache(html) { // restore cached content - if (!this.cacheState) { + if (!this.needCache) { return html; } const regex = new RegExp( @@ -195,6 +208,21 @@ export default class ParagraphBase extends SyntaxBase { return $html; } + /** + * + * @param {string} wholeMatch whole match + */ + checkCache(wholeMatch, sentenceMakeFunc, lineCount = 0) { + this.sign = this.$engine.md5(wholeMatch); + // miss cache + if (!this.cache[this.sign]) { + return this.toHtml(wholeMatch, sentenceMakeFunc); + } + // hit & mark cache + this.cache[this.sign].using = true; + return `${this.cacheKey}I${this.sign}_L${lineCount}$`; + } + mounted() { // console.log('base mounted'); } diff --git a/src/core/hooks/List.js b/src/core/hooks/List.js index 06581ba0..48a45bdf 100644 --- a/src/core/hooks/List.js +++ b/src/core/hooks/List.js @@ -214,21 +214,20 @@ export default class List extends ParagraphBase { return html + childrenHtml; } - toHtml(text, sentenceMakeFunc) { - this.sign = this.$engine.md5(text); + toHtml(wholeMatch, sentenceMakeFunc) { + // 行数计算吸收的空行 + this.emptyLines = wholeMatch.match(/^\n\n/)?.length ?? 0; + const text = wholeMatch.replace(/~0$/g, '').replace(/^\n+/, ''); this.buildTree(makeChecklist(text), sentenceMakeFunc); - return this.renderTree(0); + const result = this.renderTree(0); + return this.pushCache(result, this.sign); } makeHtml(str, sentenceMakeFunc) { let $str = `${str}~0`; if (this.test($str)) { $str = $str.replace(this.RULE.reg, (wholeMatch) => { - // 行数计算吸收的空行 - this.emptyLines = wholeMatch.match(/^\n\n/)?.length ?? 0; - const text = wholeMatch.replace(/~0$/g, '').replace(/^\n+/, ''); - const result = this.toHtml(text, sentenceMakeFunc); - return this.getCacheWithSpace(this.pushCache(result, this.sign), wholeMatch); + return this.getCacheWithSpace(this.checkCache(wholeMatch, sentenceMakeFunc), wholeMatch); }); } $str = $str.replace(/~0$/g, '');