@@ -17,6 +17,7 @@ import SyntaxBase, { HOOKS_TYPE_LIST } from './SyntaxBase';
1717import { prependLineFeedForParagraph } from '@/utils/lineFeed' ;
1818import { getIsClassicBrFromLocal , testKeyInLocal } from '@/utils/config' ;
1919import { blockNames } from '@/utils/sanitize' ;
20+ import LRUCache from '../utils/LRUCache' ;
2021
2122let cacheCounter = 0 ;
2223// ~~C${cacheCounter}I${cacheIndex}$
@@ -33,11 +34,10 @@ export default class ParagraphBase extends SyntaxBase {
3334 this . needCache = ! ! needCache ;
3435 this . sign = '' ;
3536 if ( needCache ) {
36- this . cache = defaultCache || { } ;
37+ this . cache = new LRUCache ( 2000 ) ;
3738 this . cacheKey = `~~C${ cacheCounter } ` ;
3839 cacheCounter += 1 ;
3940 }
40- this . failedResetCacheTimes = 0 ;
4141 this . cacheData = { } ;
4242 this . cacheDataMap = [ ] ;
4343 }
@@ -311,34 +311,29 @@ export default class ParagraphBase extends SyntaxBase {
311311 }
312312 const $sign = sign || this . $engine . hash ( str ) ;
313313 const key = `${ this . cacheKey } I${ $sign } _L${ lineCount } $` ;
314- this . cache [ $sign ] = {
314+ this . cache . set ( $sign , {
315315 content : str ,
316316 key,
317- } ;
317+ } ) ;
318318 return key ;
319319 }
320320
321321 popCache ( sign ) {
322322 if ( ! this . needCache ) {
323323 return ;
324324 }
325- return this . cache [ sign ] . content || '' ;
325+ return this . cache . get ( sign ) ? .content || '' ;
326326 }
327327
328328 testHasCache ( sign ) {
329- if ( ! this . needCache || ! this . cache [ sign ] ) {
329+ if ( ! this . needCache || ! this . cache . get ( sign ) ) {
330330 return false ;
331331 }
332- return this . cache [ sign ] . key ;
332+ return this . cache . get ( sign ) ? .key ;
333333 }
334334
335335 // 当缓存全部被消费后,调用此方法清理多大的缓存
336- resetCache ( ) {
337- if ( ! this . needCache ) {
338- return ;
339- }
340- this . cache = { } ;
341- }
336+ resetCache ( ) { }
342337
343338 restoreCache ( html ) {
344339 // restore cached content
@@ -350,20 +345,6 @@ export default class ParagraphBase extends SyntaxBase {
350345 'g' ,
351346 ) ;
352347 const $html = html . replace ( regex , ( match , cacheSign ) => this . popCache ( cacheSign . replace ( / _ L \d + $ / , '' ) ) ) ;
353- if ( this . timer ) {
354- clearTimeout ( this . timer ) ;
355- this . failedResetCacheTimes += 1 ;
356- this . timer = null ;
357- }
358- this . timer = setTimeout ( ( ) => {
359- this . resetCache ( ) ;
360- } , 500 ) ;
361- if ( this . failedResetCacheTimes > 5 ) {
362- this . failedResetCacheTimes = 0 ;
363- setTimeout ( ( ) => {
364- this . resetCache ( ) ;
365- } , 500 ) ;
366- }
367348 return $html ;
368349 }
369350
@@ -374,7 +355,7 @@ export default class ParagraphBase extends SyntaxBase {
374355 checkCache ( wholeMatch , sentenceMakeFunc , lineCount = 0 ) {
375356 this . sign = this . $engine . hash ( wholeMatch ) ;
376357 // miss cache
377- if ( ! this . cache [ this . sign ] ) {
358+ if ( ! this . cache . get ( this . sign ) ) {
378359 return this . toHtml ( wholeMatch , sentenceMakeFunc ) ;
379360 }
380361 return `${ this . cacheKey } I${ this . sign } _L${ lineCount } $` ;
0 commit comments