[英]Cache with sequelize.query
I would like to know how can I use a cache with Sequelize?我想知道如何在 Sequelize 中使用缓存?
I look at the different package ( sequelize-transparent-cache , ...), but the cache use with the model.我查看了不同的 package ( sequelize-transparent-cache , ...),但缓存与 model 一起使用。
How can I use a cache with sequelize.query('SELECT * FROM Books')
and not model?如何使用带有
sequelize.query('SELECT * FROM Books')
而不是 model 的缓存?
For example, using Cache-Aside pattern .例如,使用Cache-Aside 模式。
The pseudo code is as follows:伪代码如下:
async function findAllBooks() {
let books = await cache.get('books');
if(!books) {
books = await sequelize.query('SELECT * FROM Books');
await cache.set('books', books);
}
return books;
}
The sequelize-transparent-cache
package uses the same pattern for findByPk()
method, see the source code: https://github.com/DanielHreben/sequelize-transparent-cache/blob/master/packages/sequelize-transparent-cache/src/methods/class.js#L14 sequelize-transparent-cache
package 对findByPk()
方法使用相同的模式,参见源代码: https://github.com/DanielHreben/sequelize-transparent-cache/blob/master/packages/sequel src/methods/class.js#L14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.