Express + EJS如何开启页面缓存,请指教。
发布于 2 个月前 作者 zouzhenxing 296 次浏览 来自 问答

如题,请各位指教!

4 回复

你到底知不知道什么是缓存

@captainblue2013 我想开启的是静态页面缓存,把渲染后的结果保存起来。我看到ejs里面有开启缓存的设置,但不知道在Express中如何处理。如果有做过的,希望能帮我解答下。

Caching

EJS ships with a basic in-process cache for caching the intermediate JavaScript functions used to render templates. It’s easy to plug in LRU caching using Node’s lru-cache library:

var ejs = require('ejs')
  , LRU = require('lru-cache');
ejs.cache = LRU(100); // LRU cache with 100-item limit

If you want to clear the EJS cache, call ejs.clearCache. If you’re using the LRU cache and need a different limit, simple reset ejs.cache to a new instance of the LRU.

LZ,菜菜表示不懂你为什么要用这个,有没有有关这个的详细一点资料,让我mark下。。

去翻了下express的源码,

// set .cache unless explicitly provided
  if (renderOptions.cache == null) {
    renderOptions.cache = this.enabled('view cache');
  }

  // primed cache
  if (renderOptions.cache) {
    view = cache[name];
  }

  // view
  if (!view) {
    var View = this.get('view');

    view = new View(name, {
	// ...

http://expressjs.com/zh-cn/4x/api.html#app.render

The local variable cache is reserved for enabling view cache. Set it to true, if you want to cache view during development; view caching is enabled in production by default.

回到顶部