Добавлено кеширование запросов getBookList
This commit is contained in:
@@ -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--;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
//загружаем БД
|
//загружаем БД
|
||||||
|
|||||||
Reference in New Issue
Block a user