Убрал лишнее кеширование

This commit is contained in:
Book Pauk
2022-10-10 20:57:30 +07:00
parent 7cf5e91cc4
commit 09b7c38348

View File

@@ -263,7 +263,13 @@ class DbSearcher {
} }
} }
async selectBookList(authorId) { async getBookList(authorId) {
if (this.closed)
throw new Error('DbSearcher closed');
this.searchFlag++;
try {
const db = this.db; const db = this.db;
//выборка автора по authorId //выборка автора по authorId
@@ -281,77 +287,11 @@ class DbSearcher {
} }
return {author, books}; return {author, books};
}
async getBookList(authorId) {
if (this.closed)
throw new Error('DbSearcher closed');
this.searchFlag++;
try {
const db = this.db;
let result;
if (this.config.queryCacheEnabled) {
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 {//не нашли в кеше
result = await this.selectBookList(authorId);
//кладем в кеш
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()}],
});
}
} else {
result = await this.selectBookList(authorId);
}
return result;
} finally { } finally {
this.searchFlag--; this.searchFlag--;
} }
} }
async selectSeriesBookList(seriesId) {
const db = this.db;
//выборка серии по seriesId
const rows = await db.select({
table: 'series',
where: `@@id(${db.esc(seriesId)})`
});
let books = [];
if (rows.length) {
books = await db.select({
table: 'book',
where: `@@id(${db.esc(rows[0].bookId)})`
});
}
return {books: JSON.stringify(books)};
}
async getSeriesBookList(seriesId) { async getSeriesBookList(seriesId) {
if (this.closed) if (this.closed)
throw new Error('DbSearcher closed'); throw new Error('DbSearcher closed');
@@ -361,40 +301,13 @@ class DbSearcher {
try { try {
const db = this.db; const db = this.db;
let result; //выборка серии по seriesId
const rows = await db.select({
if (this.config.queryCacheEnabled) { table: 'series',
const key = `series_books-${seriesId}`; where: `@@id(${db.esc(seriesId)})`
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; return {books: (rows.length ? rows[0].books : '')};
} else {//не нашли в кеше
result = await this.selectSeriesBookList(seriesId);
//кладем в кеш
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()}],
});
}
} else {
result = await this.selectSeriesBookList(seriesId);
}
return result;
} finally { } finally {
this.searchFlag--; this.searchFlag--;
} }