Skip to content

Commit

Permalink
feat(fragment_cache): bring up lru
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Dec 2, 2019
1 parent e423773 commit dd14ff2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/plugins/helper/fragment_cache.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
'use strict';

const LRU = require('lru-cache');

module.exports = ctx => {
let cache = {};
const cache = new LRU(50);

// reset cache for watch mode
ctx.on('generateBefore', () => { cache = {}; });
ctx.on('generateBefore', () => { cache.reset(); });

return function fragmentCache(id, fn) {
if (this.cache && cache[id] != null) return cache[id];
if (this.cache && cache.has(id)) return cache.get(id);

cache[id] = fn();
const result = fn();
cache.set(id, result);
return result;
};
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"hexo-util": "^1.6.1",
"js-yaml": "^3.12.0",
"lodash": "^4.17.11",
"lru-cache": "^5.1.1",
"micromatch": "^4.0.2",
"moment": "^2.22.2",
"moment-timezone": "^0.5.21",
Expand Down

0 comments on commit dd14ff2

Please sign in to comment.