Skip to content

Commit

Permalink
feat(fragment_cache): use lfu instead of lru
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Dec 2, 2019
1 parent dd14ff2 commit c7c34cd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/plugins/helper/fragment_cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

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

module.exports = ctx => {
const cache = new LRU(50);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
"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",
"node-lfu-cache": "^5.0.0",
"nunjucks": "^3.1.3",
"pretty-hrtime": "^1.0.3",
"resolve": "^1.8.1",
Expand Down
17 changes: 17 additions & 0 deletions test/scripts/helpers/fragment_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,21 @@ describe('fragment_cache', () => {
hexo.emit('generateBefore');
fragment_cache.call({cache: true}, 'foo', () => 789).should.eql(789);
});

it('should delete oldest & coldest cache when meet size limit', () => {
fragment_cache.call({cache: true}, 'cold', () => 123);
for (let i = 1; i <= 10; i++) {
fragment_cache.call({cache: true}, 'hot', () => 456);
}

const random = (min, max) => Math.round(Math.random() * (max - min)) + min;
for (let i = 1; i <= 60; i++) {
fragment_cache.call({cache: true}, String(100 + i), () => random(100, 900));
}

// The cold cache should be deleted
fragment_cache.call({cache: true}, 'cold', () => 789).should.eql(789);
// The hot cache should not be deleted
fragment_cache.call({cache: true}, 'hot', () => 789).should.eql(456);
});
});

0 comments on commit c7c34cd

Please sign in to comment.