From 364ccd85fe0f9504c66cf7cf332c3534663d50fb Mon Sep 17 00:00:00 2001 From: mikiher Date: Sun, 23 Feb 2025 08:53:57 +0200 Subject: [PATCH] Use count cache only when no filter is set --- .../utils/queries/libraryItemsBookFilters.js | 32 +++++++++++-------- .../queries/libraryItemsPodcastFilters.js | 31 ++++++++++-------- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/server/utils/queries/libraryItemsBookFilters.js b/server/utils/queries/libraryItemsBookFilters.js index 3787cc84..7839651b 100644 --- a/server/utils/queries/libraryItemsBookFilters.js +++ b/server/utils/queries/libraryItemsBookFilters.js @@ -344,22 +344,28 @@ module.exports = { countCache.clear() }, - async findAndCountAll(findOptions, limit, offset) { - const findOptionsKey = stringifySequelizeQuery(findOptions) - Logger.debug(`[LibraryItemsBookFilters] findOptionsKey: ${findOptionsKey}`) + async findAndCountAll(findOptions, limit, offset, useCountCache) { + const model = Database.bookModel + if (useCountCache) { + const countCacheKey = stringifySequelizeQuery(findOptions) + Logger.debug(`[LibraryItemsBookFilters] countCacheKey: ${countCacheKey}`) + if (!countCache.has(countCacheKey)) { + const count = await model.count(findOptions) + countCache.set(countCacheKey, count) + } + + findOptions.limit = limit || null + findOptions.offset = offset + + const rows = await model.findAll(findOptions) + + return { rows, count: countCache.get(countCacheKey) } + } findOptions.limit = limit || null findOptions.offset = offset - if (countCache.has(findOptionsKey)) { - const rows = await Database.bookModel.findAll(findOptions) - - return { rows, count: countCache.get(findOptionsKey) } - } else { - const result = await Database.bookModel.findAndCountAll(findOptions) - countCache.set(findOptionsKey, result.count) - return result - } + return await model.findAndCountAll(findOptions) }, /** @@ -606,7 +612,7 @@ module.exports = { } const findAndCountAll = process.env.QUERY_PROFILING ? profile(this.findAndCountAll) : this.findAndCountAll - const { rows: books, count } = await findAndCountAll(findOptions, limit, offset) + const { rows: books, count } = await findAndCountAll(findOptions, limit, offset, !filterGroup) const libraryItems = books.map((bookExpanded) => { const libraryItem = bookExpanded.libraryItem diff --git a/server/utils/queries/libraryItemsPodcastFilters.js b/server/utils/queries/libraryItemsPodcastFilters.js index 7b54eed0..6527cfbd 100644 --- a/server/utils/queries/libraryItemsPodcastFilters.js +++ b/server/utils/queries/libraryItemsPodcastFilters.js @@ -105,22 +105,27 @@ module.exports = { countCache.clear() }, - async findAndCountAll(findOptions, model, limit, offset) { - const cacheKey = stringifySequelizeQuery(findOptions) - if (!countCache.has(cacheKey)) { - const count = await model.count(findOptions) - countCache.set(cacheKey, count) + async findAndCountAll(findOptions, model, limit, offset, useCountCache) { + if (useCountCache) { + const countCacheKey = stringifySequelizeQuery(findOptions) + Logger.debug(`[LibraryItemsPodcastFilters] countCacheKey: ${countCacheKey}`) + if (!countCache.has(countCacheKey)) { + const count = await model.count(findOptions) + countCache.set(countCacheKey, count) + } + + findOptions.limit = limit || null + findOptions.offset = offset + + const rows = await model.findAll(findOptions) + + return { rows, count: countCache.get(countCacheKey) } } findOptions.limit = limit || null findOptions.offset = offset - const rows = await model.findAll(findOptions) - - return { - rows, - count: countCache.get(cacheKey) - } + return await model.findAndCountAll(findOptions) }, /** @@ -199,7 +204,7 @@ module.exports = { const findAndCountAll = process.env.QUERY_PROFILING ? profile(this.findAndCountAll) : this.findAndCountAll - const { rows: podcasts, count } = await findAndCountAll(findOptions, Database.podcastModel, limit, offset) + const { rows: podcasts, count } = await findAndCountAll(findOptions, Database.podcastModel, limit, offset, !filterGroup) const libraryItems = podcasts.map((podcastExpanded) => { const libraryItem = podcastExpanded.libraryItem @@ -323,7 +328,7 @@ module.exports = { const findAndCountAll = process.env.QUERY_PROFILING ? profile(this.findAndCountAll) : this.findAndCountAll - const { rows: podcastEpisodes, count } = await findAndCountAll(findOptions, Database.podcastEpisodeModel, limit, offset) + const { rows: podcastEpisodes, count } = await findAndCountAll(findOptions, Database.podcastEpisodeModel, limit, offset, !filterGroup) const libraryItems = podcastEpisodes.map((ep) => { const libraryItem = ep.podcast.libraryItem