Добавлено кеширование запросов 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,6 +271,22 @@ class DbSearcher {
try { try {
const db = this.db; const db = this.db;
let result;
const key = `author_books-${authorId}`;
const rows = await db.select({table: 'query_cache', where: `@@id(${db.esc(key)})`});
if (rows.length) {//нашли в кеше
await db.insert({
table: 'query_time',
replace: true,
rows: [{id: key, time: Date.now()}],
});
result = rows[0].value;
} else {//не нашли в кеше
//выборка автора по authorId //выборка автора по authorId
const rows = await db.select({ const rows = await db.select({
table: 'author', table: 'author',
@@ -279,20 +295,35 @@ class DbSearcher {
}); });
let author = ''; let author = '';
let result = []; let books = [];
if (rows.length) { if (rows.length) {
author = rows[0].author; author = rows[0].author;
//выборка книг по bookId //выборка книг по bookId
result = await db.select({ books = await db.select({
table: 'book', table: 'book',
//map: `(r) => ({})`, //map: `(r) => ({})`,
where: `@@id(${db.esc(rows[0].bookId)})`, where: `@@id(${db.esc(rows[0].bookId)})`,
}); });
} }
return {author, books: result}; 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 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();
} }
//загружаем БД //загружаем БД