Добавлено кеширование запросов getBookList

This commit is contained in:
Book Pauk
2022-08-23 20:14:20 +07:00
parent b495a16626
commit 102e584850
2 changed files with 48 additions and 17 deletions

View File

@@ -271,28 +271,59 @@ class DbSearcher {
try { try {
const db = this.db; const db = this.db;
//выборка автора по authorId let result;
const rows = await db.select({
table: 'author',
map: `(r) => ({author: r.author, bookId: r.bookId})`,
where: `@@id(${db.esc(authorId)})`
});
let author = ''; const key = `author_books-${authorId}`;
let result = [];
if (rows.length) { const rows = await db.select({table: 'query_cache', where: `@@id(${db.esc(key)})`});
author = rows[0].author;
//выборка книг по bookId if (rows.length) {//нашли в кеше
result = await db.select({ await db.insert({
table: 'book', table: 'query_time',
//map: `(r) => ({})`, replace: true,
where: `@@id(${db.esc(rows[0].bookId)})`, rows: [{id: key, time: Date.now()}],
});
result = rows[0].value;
} else {//не нашли в кеше
//выборка автора по authorId
const rows = await db.select({
table: 'author',
map: `(r) => ({author: r.author, bookId: r.bookId})`,
where: `@@id(${db.esc(authorId)})`
});
let author = '';
let books = [];
if (rows.length) {
author = rows[0].author;
//выборка книг по bookId
books = await db.select({
table: 'book',
//map: `(r) => ({})`,
where: `@@id(${db.esc(rows[0].bookId)})`,
});
}
result = {author, books};
//кладем в кеш
await db.insert({
table: 'query_cache',
replace: true,
rows: [{id: key, value: result}],
});
await db.insert({
table: 'query_time',
replace: true,
rows: [{id: key, time: Date.now()}],
}); });
} }
return {author, books: result}; return result;
} finally { } finally {
this.searchFlag--; this.searchFlag--;
} }

View File

@@ -119,7 +119,7 @@ class WebWorker {
if (!await fs.pathExists(dbPath)) { if (!await fs.pathExists(dbPath)) {
await this.createDb(dbPath); await this.createDb(dbPath);
await utils.freeMemory(); utils.freeMemory();
} }
//загружаем БД //загружаем БД