Добавлен параметр "queryCacheEnabled" в конфиг

This commit is contained in:
Book Pauk
2022-08-25 23:35:12 +07:00
parent 6222827593
commit 1e5c97dfac
3 changed files with 73 additions and 62 deletions

View File

@@ -18,6 +18,7 @@ module.exports = {
loggingEnabled: true, loggingEnabled: true,
maxFilesDirSize: 1024*1024*1024,//1Gb maxFilesDirSize: 1024*1024*1024,//1Gb
queryCacheEnabled: true,
cacheCleanInterval: 60,//minutes cacheCleanInterval: 60,//minutes
webConfigParams: ['name', 'version', 'branch'], webConfigParams: ['name', 'version', 'branch'],

View File

@@ -6,6 +6,7 @@ const branchFilename = __dirname + '/application_env';
const propsToSave = [ const propsToSave = [
'loggingEnabled', 'loggingEnabled',
'maxFilesDirSize', 'maxFilesDirSize',
'queryCacheEnabled',
'cacheCleanInterval', 'cacheCleanInterval',
'server', 'server',
]; ];

View File

@@ -76,9 +76,7 @@ class DbSearcher {
db.searchCache.authorIdsAll.push(row.id); db.searchCache.authorIdsAll.push(row.id);
} }
} else {//оптимизация } else {//оптимизация
for (const id of db.searchCache.authorIdsAll) { authorIds = new Set(db.searchCache.authorIdsAll);
authorIds.add(id);
}
} }
} }
@@ -202,31 +200,34 @@ class DbSearcher {
result = await this.selectAuthorIds(query); result = await this.selectAuthorIds(query);
} else {//непустой запрос } else {//непустой запрос
const key = JSON.stringify(keyArr); if (this.config.queryCacheEnabled) {
const key = JSON.stringify(keyArr);
const rows = await db.select({table: 'query_cache', where: `@@id(${db.esc(key)})`});
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()}],
});
if (rows.length) {//нашли в кеше result = rows[0].value;
await db.insert({ } else {//не нашли в кеше, ищем в поисковых таблицах
table: 'query_time', result = await this.selectAuthorIds(query);
replace: true,
rows: [{id: key, time: Date.now()}],
});
result = rows[0].value; await db.insert({
} else {//не нашли в кеше, ищем в поисковых таблицах 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.selectAuthorIds(query); result = await this.selectAuthorIds(query);
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()}],
});
} }
} }
@@ -262,6 +263,26 @@ class DbSearcher {
} }
} }
async selectBookList(authorId) {
const db = this.db;
//выборка автора по authorId
const rows = await db.select({
table: 'author_book',
where: `@@id(${db.esc(authorId)})`
});
let author = '';
let books = [];
if (rows.length) {
author = rows[0].author;
books = rows[0].books;
}
return {author, books};
}
async getBookList(authorId) { async getBookList(authorId) {
if (this.closed) if (this.closed)
throw new Error('DbSearcher closed'); throw new Error('DbSearcher closed');
@@ -273,47 +294,35 @@ class DbSearcher {
let result; let result;
const key = `author_books-${authorId}`; if (this.config.queryCacheEnabled) {
const key = `author_books-${authorId}`;
const rows = await db.select({table: 'query_cache', where: `@@id(${db.esc(key)})`});
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()}],
});
if (rows.length) {//нашли в кеше result = rows[0].value;
await db.insert({ } else {//не нашли в кеше
table: 'query_time', result = await this.selectBookList(authorId);
replace: true,
rows: [{id: key, time: Date.now()}],
});
result = rows[0].value; //кладем в кеш
} else {//не нашли в кеше await db.insert({
table: 'query_cache',
//выборка автора по authorId replace: true,
const rows = await db.select({ rows: [{id: key, value: result}],
table: 'author_book', });
where: `@@id(${db.esc(authorId)})` await db.insert({
}); table: 'query_time',
replace: true,
let author = ''; rows: [{id: key, time: Date.now()}],
let books = []; });
if (rows.length) {
author = rows[0].author;
books = rows[0].books;
} }
} else {
result = {author, books}; 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()}],
});
} }
return result; return result;
@@ -324,7 +333,7 @@ class DbSearcher {
async periodicCleanCache() { async periodicCleanCache() {
this.timer = null; this.timer = null;
const cleanInterval = 5*1000;//this.config.cacheCleanInterval*60*1000; const cleanInterval = this.config.cacheCleanInterval*60*1000;
try { try {
const db = this.db; const db = this.db;