Add count cache to libraryItemsPodcastQueries

This commit is contained in:
mikiher 2025-02-16 09:40:29 +02:00
parent a5508cdc4c
commit 21343b5aa0

View File

@ -1,6 +1,9 @@
const Sequelize = require('sequelize') const Sequelize = require('sequelize')
const Database = require('../../Database') const Database = require('../../Database')
const Logger = require('../../Logger') const Logger = require('../../Logger')
const stringifySequelizeQuery = require('../stringifySequelizeQuery')
const countCache = new Map()
module.exports = { module.exports = {
/** /**
@ -96,6 +99,28 @@ module.exports = {
return [] return []
}, },
clearCountCache() {
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)
}
findOptions.limit = limit
findOptions.offset = offset
const rows = await model.findAll(findOptions)
return {
rows,
count: countCache.get(cacheKey)
}
},
/** /**
* Get library items for podcast media type using filter and sort * Get library items for podcast media type using filter and sort
* @param {string} libraryId * @param {string} libraryId
@ -151,7 +176,7 @@ module.exports = {
replacements = { ...replacements, ...userPermissionPodcastWhere.replacements } replacements = { ...replacements, ...userPermissionPodcastWhere.replacements }
podcastWhere.push(...userPermissionPodcastWhere.podcastWhere) podcastWhere.push(...userPermissionPodcastWhere.podcastWhere)
const { rows: podcasts, count } = await Database.podcastModel.findAndCountAll({ const findOptions = {
where: podcastWhere, where: podcastWhere,
replacements, replacements,
distinct: true, distinct: true,
@ -167,10 +192,10 @@ module.exports = {
} }
], ],
order: this.getOrder(sortBy, sortDesc), order: this.getOrder(sortBy, sortDesc),
subQuery: false, subQuery: false
limit: limit || null, }
offset
}) const { rows: podcasts, count } = await this.findAndCountAll(findOptions, Database.podcastModel, limit, offset)
const libraryItems = podcasts.map((podcastExpanded) => { const libraryItems = podcasts.map((podcastExpanded) => {
const libraryItem = podcastExpanded.libraryItem const libraryItem = podcastExpanded.libraryItem
@ -270,7 +295,7 @@ module.exports = {
const userPermissionPodcastWhere = this.getUserPermissionPodcastWhereQuery(user) const userPermissionPodcastWhere = this.getUserPermissionPodcastWhereQuery(user)
const { rows: podcastEpisodes, count } = await Database.podcastEpisodeModel.findAndCountAll({ const findOptions = {
where: podcastEpisodeWhere, where: podcastEpisodeWhere,
replacements: userPermissionPodcastWhere.replacements, replacements: userPermissionPodcastWhere.replacements,
include: [ include: [
@ -289,10 +314,10 @@ module.exports = {
...podcastEpisodeIncludes ...podcastEpisodeIncludes
], ],
subQuery: false, subQuery: false,
order: podcastEpisodeOrder, order: podcastEpisodeOrder
limit, }
offset
}) const { rows: podcastEpisodes, count } = await this.findAndCountAll(findOptions, Database.podcastEpisodeModel, limit, offset)
const libraryItems = podcastEpisodes.map((ep) => { const libraryItems = podcastEpisodes.map((ep) => {
const libraryItem = ep.podcast.libraryItem const libraryItem = ep.podcast.libraryItem