【问题标题】:Sequelize prevent cache续集防止缓存
【发布时间】:2021-04-20 00:46:35
【问题描述】:

我一直在我的应用程序中使用 mongoose,但是我在特定应用程序中使用了 sequelize。

但是我在客户端缓存方面遇到问题。

例如,在我的分类列表中,有编辑的功能,当我编辑保存时,客户端应用程序再次调用该列表,以显示更新的数据。

这对 mongoose 非常有效,但使用 sequelize 缓存不会让数据在屏幕上更新。

下面是我如何在服务器端进行列表和编辑的示例

const get = (req, res, next) => {
    categoriesModel.findAll({})
    .then(data => res.json({
        status: true,
        data: data
    }))
    .catch(error => res.json({
        status: false,
        data: [],
        msg: error
    }))
}

const put = (req, res, next) => {
    categoriesModel.update({
        title: req.body.title,
        active: req.body.active
    },
    {
        where: {
            id: req.params.id
        }
    })
    .then(data => res.json({
        status: true,
        data: data,
        msg: 'Success!'
    }))
    .catch(error => res.json({
        status: false,
        data: [],
        msg: error
    }))
}

【问题讨论】:

  • 你指的是什么缓存?浏览器的缓存? Sequelize 没有开箱即用的缓存,是吗?
  • @cbr 浏览器缓存,在chrome开发者控制台中,在网络标签中显示(磁盘缓存)
  • 您是否在后端的其他地方设置了一些与缓存相关的标头?尝试显式禁用 API 路由的缓存,即 res.set("Cache-Control", "no-cache, no-store, must-revalidate"); res.set("Pragma", "no-cache"); res.set("Expires", 0);
  • 您在浏览器的开发人员工具的“网络”选项卡中为该响应获得了哪些响应标头?
  • 是的,解决了!我在“Cache-Control”中添加了 max-age=0,谢谢!

标签: mysql node.js sequelize.js


【解决方案1】:

我通过添加以下标题规则解决了我的问题

app.use(function(req, res, next) {
    res.set("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0")
    res.set("Pragma", "no-cache")
    res.set("Expires", 0)
    next()
})

【讨论】:

    猜你喜欢
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2020-05-27
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多