fragment_cache.js 369 B

123456789101112131415161718
  1. 'use strict';
  2. const { Cache } = require('hexo-util');
  3. module.exports = ctx => {
  4. const cache = new Cache();
  5. // reset cache for watch mode
  6. ctx.on('generateBefore', () => { cache.flush(); });
  7. return function fragmentCache(id, fn) {
  8. if (this.cache) return cache.apply(id, fn);
  9. const result = fn();
  10. cache.set(id, result);
  11. return result;
  12. };
  13. };