mirror of
				https://github.com/advplyr/audiobookshelf.git
				synced 2025-10-27 11:18:14 +01:00 
			
		
		
		
	Merge pull request #4020 from mikiher/invalidate-count-cache-on-entity-update
Invalidate count cache on entity update
This commit is contained in:
		
						commit
						a864c6bcc6
					
				@ -344,22 +344,28 @@ module.exports = {
 | 
				
			|||||||
    countCache.clear()
 | 
					    countCache.clear()
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async findAndCountAll(findOptions, limit, offset) {
 | 
					  async findAndCountAll(findOptions, limit, offset, useCountCache) {
 | 
				
			||||||
    const findOptionsKey = stringifySequelizeQuery(findOptions)
 | 
					    const model = Database.bookModel
 | 
				
			||||||
    Logger.debug(`[LibraryItemsBookFilters] findOptionsKey: ${findOptionsKey}`)
 | 
					    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.limit = limit || null
 | 
				
			||||||
      findOptions.offset = offset
 | 
					      findOptions.offset = offset
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (countCache.has(findOptionsKey)) {
 | 
					      const rows = await model.findAll(findOptions)
 | 
				
			||||||
      const rows = await Database.bookModel.findAll(findOptions)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      return { rows, count: countCache.get(findOptionsKey) }
 | 
					      return { rows, count: countCache.get(countCacheKey) }
 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      const result = await Database.bookModel.findAndCountAll(findOptions)
 | 
					 | 
				
			||||||
      countCache.set(findOptionsKey, result.count)
 | 
					 | 
				
			||||||
      return result
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    findOptions.limit = limit || null
 | 
				
			||||||
 | 
					    findOptions.offset = offset
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return await model.findAndCountAll(findOptions)
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
@ -434,19 +440,17 @@ module.exports = {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const libraryItemIncludes = []
 | 
					    const libraryItemIncludes = []
 | 
				
			||||||
    const bookIncludes = []
 | 
					    const bookIncludes = []
 | 
				
			||||||
    if (includeRSSFeed) {
 | 
					
 | 
				
			||||||
 | 
					    if (filterGroup === 'feed-open' || includeRSSFeed) {
 | 
				
			||||||
 | 
					      const rssFeedRequired = filterGroup === 'feed-open'
 | 
				
			||||||
      libraryItemIncludes.push({
 | 
					      libraryItemIncludes.push({
 | 
				
			||||||
        model: Database.feedModel,
 | 
					        model: Database.feedModel,
 | 
				
			||||||
        required: filterGroup === 'feed-open',
 | 
					        required: rssFeedRequired,
 | 
				
			||||||
        separate: true
 | 
					        separate: !rssFeedRequired
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (filterGroup === 'feed-open' && !includeRSSFeed) {
 | 
					
 | 
				
			||||||
      libraryItemIncludes.push({
 | 
					    if (filterGroup === 'share-open') {
 | 
				
			||||||
        model: Database.feedModel,
 | 
					 | 
				
			||||||
        required: true
 | 
					 | 
				
			||||||
      })
 | 
					 | 
				
			||||||
    } else if (filterGroup === 'share-open') {
 | 
					 | 
				
			||||||
      bookIncludes.push({
 | 
					      bookIncludes.push({
 | 
				
			||||||
        model: Database.mediaItemShareModel,
 | 
					        model: Database.mediaItemShareModel,
 | 
				
			||||||
        required: true
 | 
					        required: true
 | 
				
			||||||
@ -608,7 +612,7 @@ module.exports = {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const findAndCountAll = process.env.QUERY_PROFILING ? profile(this.findAndCountAll) : this.findAndCountAll
 | 
					    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 libraryItems = books.map((bookExpanded) => {
 | 
				
			||||||
      const libraryItem = bookExpanded.libraryItem
 | 
					      const libraryItem = bookExpanded.libraryItem
 | 
				
			||||||
 | 
				
			|||||||
@ -105,11 +105,13 @@ module.exports = {
 | 
				
			|||||||
    countCache.clear()
 | 
					    countCache.clear()
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async findAndCountAll(findOptions, model, limit, offset) {
 | 
					  async findAndCountAll(findOptions, model, limit, offset, useCountCache) {
 | 
				
			||||||
    const cacheKey = stringifySequelizeQuery(findOptions)
 | 
					    if (useCountCache) {
 | 
				
			||||||
    if (!countCache.has(cacheKey)) {
 | 
					      const countCacheKey = stringifySequelizeQuery(findOptions)
 | 
				
			||||||
 | 
					      Logger.debug(`[LibraryItemsPodcastFilters] countCacheKey: ${countCacheKey}`)
 | 
				
			||||||
 | 
					      if (!countCache.has(countCacheKey)) {
 | 
				
			||||||
        const count = await model.count(findOptions)
 | 
					        const count = await model.count(findOptions)
 | 
				
			||||||
      countCache.set(cacheKey, count)
 | 
					        countCache.set(countCacheKey, count)
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      findOptions.limit = limit || null
 | 
					      findOptions.limit = limit || null
 | 
				
			||||||
@ -117,10 +119,13 @@ module.exports = {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      const rows = await model.findAll(findOptions)
 | 
					      const rows = await model.findAll(findOptions)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return {
 | 
					      return { rows, count: countCache.get(countCacheKey) }
 | 
				
			||||||
      rows,
 | 
					 | 
				
			||||||
      count: countCache.get(cacheKey)
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    findOptions.limit = limit || null
 | 
				
			||||||
 | 
					    findOptions.offset = offset
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return await model.findAndCountAll(findOptions)
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
@ -199,7 +204,7 @@ module.exports = {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const findAndCountAll = process.env.QUERY_PROFILING ? profile(this.findAndCountAll) : this.findAndCountAll
 | 
					    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 libraryItems = podcasts.map((podcastExpanded) => {
 | 
				
			||||||
      const libraryItem = podcastExpanded.libraryItem
 | 
					      const libraryItem = podcastExpanded.libraryItem
 | 
				
			||||||
@ -323,7 +328,7 @@ module.exports = {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const findAndCountAll = process.env.QUERY_PROFILING ? profile(this.findAndCountAll) : this.findAndCountAll
 | 
					    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 libraryItems = podcastEpisodes.map((ep) => {
 | 
				
			||||||
      const libraryItem = ep.podcast.libraryItem
 | 
					      const libraryItem = ep.podcast.libraryItem
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user