我有一个简单的API和basic
CRUD operations
我正在使用
MongoDB
具有
mLab
作为我的主数据存储,用于存储用户和任务模式的文档。
Redis caching
getAllUserTasks
经常在发生任何删除/更新/添加时使用和调用。
到目前为止,该应用程序与
蒙哥达
mLab
.
现在谈到Redis部分,我有一个问题,我应该如何构造我的Redis数据库。首先,让我发布到目前为止我在应用程序中拥有的内容:
用户架构:
const userSchema = new Schema({
userName: { type: String, unique: true, required: true },
hashPassword: { type: String, required: true },
firstName: { type: String, required: true },
lastName: { type: String, required: true },
userAge: { type: Number, required: true },
userDetails: { type: String, required: true },
userCreatedOn: { type: Date, default: Date.now }
});
任务架构:
const taskSchema = new Schema({
//Below is userAssigned which is ref for above User document.
userAssigned: { type: Schema.Types.ObjectId, ref: User},
taskDesc: { type: String, required: true },
taskCreatedOn: { type: Date, default: Date.now },
taskDueDate: { type: Date, required: true }
});
getAllUserTasks函数:
async function getAllUserTasks(userParam) {
return await Task.find({ userAssigned: userParam.userId })
.lean()
.sort({ taskDueDate: "asc" });
}
现在
getAllUserTasks
Array
我在谷歌上搜索了一下,发现我可以用
https://redislabs.com/
我的Redis数据库。现在,我应该如何构造我的Redis数据库以高效地获取任务?1.我是否应该将上面的模式复制到Redis数据库,我的任务文档就在那里。
2.我应该有一个
key
比如“任务”和
value
将是我从中获得的一系列任务。请在
getAllUserTasks
呼叫如果是这种情况,我如何确保一旦任务被删除/更新/添加,Redis数据库将相应更新?以下是我的更新/创建/删除方法,仅供参考:
创建任务
async function createTask(userParam) {
if (await User.findOne({ _id: userParam.userAssigned })) {
const task = new Task(userParam);
await task.save();
} else {
throw "User does not exist";
}
}
更新任务
async function updateTask(id, userParam) {
return await Task.findOneAndUpdate(
{ _id: id },
userParam,
{
new: true,
overwrite: true
},
function(err) {
if (err) return err;
}
);
}
async function deleteUserTask(id) {
return await Task.findByIdAndRemove(id, function(err) {
if (err) {
return err;
}
});
}
由于我是Redis的新手,我非常感谢您的帮助。谢谢