0
我试图设置一个mongo,节点和redis服务器,除Redis缓存外,其他所有的缓存似乎都加载了旧数据。node.js | Redis缓存加载旧数据
将Redis取出并使用pure Mongo返回正确的数据,如果再次单击提交,它将加载之前提交的数据,因此我知道缓存和Mongo正在更新。
这是当前代码我有更新使用蒙戈和Redis的用户...
updateUserById: (newUser, redis, id, callback) => {
User.findByIdAndUpdate(id, {
$set: {
"firstName": newUser.firstName,
"lastName": newUser.lastName,
"email": newUser.email
}
}, (err, doc) => {
if (err) { throw new Error(err); }
else if (!doc) { callback('No user with that ID to update cache.') }
else {
redis.set(id, JSON.stringify(doc), (err) => {
console.log('Caching updated user ' + id);
if (err) { callback(err); throw new Error(err); }
else {
const result = {
"success": "true",
"message": "User updated successfully"
};
callback(result);
}
})
}
});
}
这是Redis的和蒙戈回报用户信息...
userById: async (redis, id, callback) => {
redis.get(id, async (err, reply) => {
if (err) {
callback(null); throw new Error(err);
} else if (reply) {
callback(JSON.parse(reply));
} else {
await User.findById(id, (err, doc) => {
if (err || !doc) { callback(null); throw new Error(err); }
else {
redis.set(id, JSON.stringify(doc),() => {
callback(doc);
})
}
})
}
})
}
我发现它与doc
有关updateUserById