Добавлен параметр "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,8 +200,8 @@ class DbSearcher {
result = await this.selectAuthorIds(query); result = await this.selectAuthorIds(query);
} else {//непустой запрос } else {//непустой запрос
if (this.config.queryCacheEnabled) {
const key = JSON.stringify(keyArr); 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) {//нашли в кеше if (rows.length) {//нашли в кеше
@@ -228,6 +226,9 @@ class DbSearcher {
rows: [{id: key, time: Date.now()}], rows: [{id: key, time: Date.now()}],
}); });
} }
} else {
result = await this.selectAuthorIds(query);
}
} }
return result; return result;
@@ -262,31 +263,9 @@ class DbSearcher {
} }
} }
async getBookList(authorId) { async selectBookList(authorId) {
if (this.closed)
throw new Error('DbSearcher closed');
this.searchFlag++;
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_book', table: 'author_book',
@@ -301,7 +280,34 @@ class DbSearcher {
books = rows[0].books; books = rows[0].books;
} }
result = {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({ await db.insert({
@@ -315,6 +321,9 @@ class DbSearcher {
rows: [{id: key, time: Date.now()}], rows: [{id: key, time: Date.now()}],
}); });
} }
} else {
result = await this.selectBookList(authorId);
}
return result; return result;
} finally { } finally {
@@ -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;