From e8b60defb6b04fb3654208c5e9365bbdbcfcdbda Mon Sep 17 00:00:00 2001 From: mikiher Date: Fri, 21 Feb 2025 09:45:10 +0200 Subject: [PATCH 1/4] Invalidate count cache on entity update --- server/models/Book.js | 4 ++++ server/models/Podcast.js | 4 ++++ server/models/PodcastEpisode.js | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/server/models/Book.js b/server/models/Book.js index 1f4193a2..811a7af0 100644 --- a/server/models/Book.js +++ b/server/models/Book.js @@ -201,6 +201,10 @@ class Book extends Model { Book.addHook('afterCreate', async (instance) => { libraryItemsBookFilters.clearCountCache('afterCreate') }) + + Book.addHook('afterUpdate', async (instance) => { + libraryItemsBookFilters.clearCountCache('afterUpdate') + }) } /** diff --git a/server/models/Podcast.js b/server/models/Podcast.js index fa27821d..c72bda27 100644 --- a/server/models/Podcast.js +++ b/server/models/Podcast.js @@ -157,6 +157,10 @@ class Podcast extends Model { Podcast.addHook('afterCreate', async (instance) => { libraryItemsPodcastFilters.clearCountCache('podcast', 'afterCreate') }) + + Podcast.addHook('afterUpdate', async (instance) => { + libraryItemsPodcastFilters.clearCountCache('podcast', 'afterUpdate') + }) } get hasMediaFiles() { diff --git a/server/models/PodcastEpisode.js b/server/models/PodcastEpisode.js index 4746f315..38f1287a 100644 --- a/server/models/PodcastEpisode.js +++ b/server/models/PodcastEpisode.js @@ -140,6 +140,10 @@ class PodcastEpisode extends Model { PodcastEpisode.addHook('afterCreate', async (instance) => { libraryItemsPodcastFilters.clearCountCache('podcastEpisode', 'afterCreate') }) + + PodcastEpisode.addHook('afterUpdate', async (instance) => { + libraryItemsPodcastFilters.clearCountCache('podcastEpisode', 'afterUpdate') + }) } get size() { From 9d7f44f73aca6157e7a4be28361c510200494ad1 Mon Sep 17 00:00:00 2001 From: advplyr Date: Fri, 21 Feb 2025 17:39:36 -0600 Subject: [PATCH 2/4] Fix RSS Feed Open query --- server/utils/queries/libraryItemsBookFilters.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/server/utils/queries/libraryItemsBookFilters.js b/server/utils/queries/libraryItemsBookFilters.js index d446a5e9..3787cc84 100644 --- a/server/utils/queries/libraryItemsBookFilters.js +++ b/server/utils/queries/libraryItemsBookFilters.js @@ -434,19 +434,17 @@ module.exports = { const libraryItemIncludes = [] const bookIncludes = [] - if (includeRSSFeed) { + + if (filterGroup === 'feed-open' || includeRSSFeed) { + const rssFeedRequired = filterGroup === 'feed-open' libraryItemIncludes.push({ model: Database.feedModel, - required: filterGroup === 'feed-open', - separate: true + required: rssFeedRequired, + separate: !rssFeedRequired }) } - if (filterGroup === 'feed-open' && !includeRSSFeed) { - libraryItemIncludes.push({ - model: Database.feedModel, - required: true - }) - } else if (filterGroup === 'share-open') { + + if (filterGroup === 'share-open') { bookIncludes.push({ model: Database.mediaItemShareModel, required: true From d6b58c2f10f66c5ef36fc720da2c901657b75021 Mon Sep 17 00:00:00 2001 From: mikiher Date: Sun, 23 Feb 2025 08:03:10 +0200 Subject: [PATCH 3/4] Revert "Invalidate count cache on entity update" This reverts commit e8b60defb6b04fb3654208c5e9365bbdbcfcdbda. --- server/models/Book.js | 4 ---- server/models/Podcast.js | 4 ---- server/models/PodcastEpisode.js | 4 ---- 3 files changed, 12 deletions(-) diff --git a/server/models/Book.js b/server/models/Book.js index 811a7af0..1f4193a2 100644 --- a/server/models/Book.js +++ b/server/models/Book.js @@ -201,10 +201,6 @@ class Book extends Model { Book.addHook('afterCreate', async (instance) => { libraryItemsBookFilters.clearCountCache('afterCreate') }) - - Book.addHook('afterUpdate', async (instance) => { - libraryItemsBookFilters.clearCountCache('afterUpdate') - }) } /** diff --git a/server/models/Podcast.js b/server/models/Podcast.js index c72bda27..fa27821d 100644 --- a/server/models/Podcast.js +++ b/server/models/Podcast.js @@ -157,10 +157,6 @@ class Podcast extends Model { Podcast.addHook('afterCreate', async (instance) => { libraryItemsPodcastFilters.clearCountCache('podcast', 'afterCreate') }) - - Podcast.addHook('afterUpdate', async (instance) => { - libraryItemsPodcastFilters.clearCountCache('podcast', 'afterUpdate') - }) } get hasMediaFiles() { diff --git a/server/models/PodcastEpisode.js b/server/models/PodcastEpisode.js index 38f1287a..4746f315 100644 --- a/server/models/PodcastEpisode.js +++ b/server/models/PodcastEpisode.js @@ -140,10 +140,6 @@ class PodcastEpisode extends Model { PodcastEpisode.addHook('afterCreate', async (instance) => { libraryItemsPodcastFilters.clearCountCache('podcastEpisode', 'afterCreate') }) - - PodcastEpisode.addHook('afterUpdate', async (instance) => { - libraryItemsPodcastFilters.clearCountCache('podcastEpisode', 'afterUpdate') - }) } get size() { From 364ccd85fe0f9504c66cf7cf332c3534663d50fb Mon Sep 17 00:00:00 2001 From: mikiher Date: Sun, 23 Feb 2025 08:53:57 +0200 Subject: [PATCH 4/4] 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