快速浏览缓存行为搞笑 [英] express view cache acting funny

查看:187
本文介绍了快速浏览缓存行为搞笑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在快速/翡翠的视图缓存中遇到一些有趣的东西。控制器通过Mongoose从MongoDB获取一篇文章,并将其交给res.render函数。但是,运行几分钟后,Express将为该路由的所有请求提供相同的编译模板。这甚至发生在共享的.jade包含在各种模板中使用。



数据库正在获取正确的文章,如果我将一些随机字符串传递给模板,我总是得到相同的输出。



这是控制器功能:

 code> exports.show = function(req,res){
var articleId;
articleId = req.params.id;
Article.findOne({
_id:articleId
})。populate('author')。exec(function(err,article){
if(err){
console.log(err);
} else {
res.render('articles / show',{
article:article,
articleId:article.id
});
}
});
};

这是路由:

  app.get('/ articles /:id',articles.show); 

同样的事情发生在我是以生产或开发模式运行。



有没有人用Express / Jade这种麻烦?

解决方案

编辑:
请注意,快速设置视图缓存已启用生产:
请参阅 express docs


查看缓存启用视图模板编辑缓存,默认情况下
生效启用


尝试在您的应用配置部分添加此行:

  app.disable('view缓存'); 

另外,尝试添加缓存控制头

  res.setHeader('Cache-Control','no-cache'); 
res.render('articles / show',{
...

w3.org docs:


Cahce-Control



使用Cache-Control常规头字段指定
请求/响应链中的所有缓存机制必须遵守的指令
。伪指令指定要
的行为阻止缓存不利地干扰请求或
响应这些指令通常会覆盖默认缓存
算法。缓存指令是单向的,因为请求中的伪指令
并不意味着在响应中给出相同的伪指令是


如果您需要更高级的控件,请考虑其他字段,如max-age,中的ifference-between-cache-control-max-age-0-and-no-cache也是一个很好的资源,你会看到不同的浏览器可能会实现这个rfc略有不同。 >

I'm running into some funny stuff with the view cache in express/Jade. The controller fetches an article from MongoDB via Mongoose and hands it to the res.render function. However, after running for a couple of minutes Express starts serving the same compiled template for all requests to that route. This even happens to shared .jade includes that are used in various templates.

The database is fetching the correct articles and it doesn't matter if I pass some random strings to the template, I always get the same output.

This is the controller function:

exports.show = function(req, res) {
  var articleId;
  articleId = req.params.id;
  Article.findOne({
    _id: articleId
  }).populate('author').exec(function(err, article) {
    if (err) {
      console.log(err);
    } else {
      res.render('articles/show', {
        article: article,
        articleId: article.id
      });
    }
  });
};

And that's the route:

app.get('/articles/:id', articles.show);

The same things happen whether I'm running in production or development mode.

Has anyone run into this kind of toruble with Express/Jade?

解决方案

Edit: Notice that express sets view cache enabled for production: see express docs

view cache Enables view template compilation caching, enabled in production by default

Try adding this line in your app config section:

app.disable('view cache');

Also, try adding cache-control headers

res.setHeader('Cache-Control', 'no-cache');
res.render('articles/show', {
...

From w3.org docs:

Cahce-Control

The Cache-Control general-header field is used to specify directives that MUST be obeyed by all caching mechanisms along the request/response chain. The directives specify behavior intended to prevent caches from adversely interfering with the request or response. These directives typically override the default caching algorithms. Cache directives are unidirectional in that the presence of a directive in a request does not imply that the same directive is to be given in the response.

If you need a more advanced control, consider other fields like max-age, this question is also a good resource, you'll see that different browsers may implement this rfc slightly different.

这篇关于快速浏览缓存行为搞笑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆