我正在为我在特定模式上的“查找”查询实现某种缓存,并且我的缓存与前\后查询 Hook 一起工作。
问题是如何正确取消“查找”查询?
mySchema.pre('find', function(next){
var result = cache.Get();
if(result){
//cancel query if we have a result from cache
abort();
} else {
next();
}
});
为了实现这个 promise ?
Model.find({..})
.select('...')
.then(function (result) {
//We can reach here and work with the cached results
});
最佳答案
由于另一个非缓存原因,我自己无法找到合理的解决方案,但如果您自己的特定缓存方法不是太重要,我建议您查看 mongoose-cache , 运行良好并且由于它的依赖性而具有简单的设置:node-lru-cache ,查看更多选项。
关于node.js - 如何取消 'pre' 钩子(Hook)中的 Mongoose 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31671024/