Skip to content

Commit

Permalink
feat: improve content cache for paragraph
Browse files Browse the repository at this point in the history
  • Loading branch information
jiawei686 authored and humyfred committed Feb 28, 2022
1 parent 8016645 commit a7661d7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
46 changes: 37 additions & 9 deletions src/core/ParagraphBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ 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}`;
cacheCounter += 1;
}
}

toHtml(str, sentenceMakeFunc) {
return str;
}

makeHtml(str, sentenceMakeFunc) {
return sentenceMakeFunc(str).html;
}
Expand Down Expand Up @@ -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(
Expand All @@ -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');
}
Expand Down
15 changes: 7 additions & 8 deletions src/core/hooks/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '');
Expand Down

0 comments on commit a7661d7

Please sign in to comment.