Sequelize 防止缓存
Sequelize prevent cache
我一直在我的应用程序中使用 mongoose,但我在特定应用程序中使用 sequelize。
但是我在客户端遇到缓存问题。
比如我的分类列表里有编辑的功能,当我编辑保存的时候,客户端会再次调用listing,显示更新后的数据。
这对 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
}))
}
我通过添加以下 header 规则解决了我的问题
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()
})
我一直在我的应用程序中使用 mongoose,但我在特定应用程序中使用 sequelize。
但是我在客户端遇到缓存问题。
比如我的分类列表里有编辑的功能,当我编辑保存的时候,客户端会再次调用listing,显示更新后的数据。
这对 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
}))
}
我通过添加以下 header 规则解决了我的问题
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()
})