Prevent ApiCacheManager from caching invalidated in-flight responses; fix model name resolution

This commit is contained in:
mikiher committed 2026-11-29 07:20:02 +02:00
1 parent 5de038dafd
commit acaf0d3949
2 files changed
+122 -12

No files matched your search

+44 -12
View File
@@ -2,6 +2,10 @@ const { LRUCache } = require('lru-cache')
const Logger = require('../Logger')
const Database = require('../Database')
const PERSONALIZED_URL_PATTERN = /^\/libraries\/[^/]+\/personalized/
const RECENT_EPISODES_URL_PATTERN = /^\/libraries\/[^/]+\/recent-episodes/
const ME_URL_PATTERN = /^\/me(\/|\?|$)/
class ApiCacheManager {
defaultCacheOptions = { max: 1000, maxSize: 10 * 1000 * 1000, sizeCalculation: (item) => item.body.length + JSON.stringify(item.headers).length }
defaultTtlOptions = { ttl: 30 * 60 * 1000 }
@@ -12,6 +16,9 @@ class ApiCacheManager {
constructor(cache = new LRUCache(this.defaultCacheOptions), ttlOptions = this.defaultTtlOptions) {
this.cache = cache
this.ttlOptions = ttlOptions
this.globalVersion = 0
this.personalizedVersion = 0
this.meVersion = 0
}
init(database = Database) {
@@ -20,12 +27,25 @@ class ApiCacheManager {
}
getModelName(model) {
if (typeof model?.name === 'string') return model.name
if (typeof model?.model?.name === 'string') return model.model.name
if (typeof model?.constructor?.name === 'string' && model.constructor.name !== 'Object') return model.constructor.name
const target = Array.isArray(model) ? model[0] : model
if (typeof target?.constructor?.name === 'string' && target.constructor.name !== 'Object' && target.constructor.name !== 'Function') {
return target.constructor.name
}
if (typeof target?.model?.name === 'string') return target.model.name
if (typeof target?.name === 'string') return target.name
return 'unknown'
}
getCacheVersion(url) {
if (PERSONALIZED_URL_PATTERN.test(url) || RECENT_EPISODES_URL_PATTERN.test(url)) {
return this.globalVersion + this.personalizedVersion
}
if (ME_URL_PATTERN.test(url)) {
return this.globalVersion + this.meVersion
}
return this.globalVersion
}
clearByUrlPattern(urlPattern) {
let removed = 0
for (const key of this.cache.keys()) {
@@ -45,10 +65,15 @@ class ApiCacheManager {
let removedPersonalized = 0
let removedRecentEpisodes = 0
if (this.modelsInvalidatingPersonalized.has(modelName)) {
removedPersonalized = this.clearByUrlPattern(/^\/libraries\/[^/]+\/personalized/)
removedRecentEpisodes = this.clearByUrlPattern(/^\/libraries\/[^/]+\/recent-episodes/)
this.personalizedVersion++
removedPersonalized = this.clearByUrlPattern(PERSONALIZED_URL_PATTERN)
removedRecentEpisodes = this.clearByUrlPattern(RECENT_EPISODES_URL_PATTERN)
}
let removedMe = 0
if (this.modelsInvalidatingMe.has(modelName)) {
this.meVersion++
removedMe = this.clearByUrlPattern(ME_URL_PATTERN)
}
const removedMe = this.modelsInvalidatingMe.has(modelName) ? this.clearByUrlPattern(/^\/me(\/|\?|$)/) : 0
Logger.debug(`[ApiCacheManager] ${modelName}.${hook}: cleared user-progress cache slices (personalized=${removedPersonalized}, recentEpisodes=${removedRecentEpisodes}, me=${removedMe})`)
}
@@ -60,6 +85,7 @@ class ApiCacheManager {
}
Logger.debug(`[ApiCacheManager] ${modelName}.${hook}: Clearing cache`)
this.globalVersion++
this.cache.clear()
}
@@ -70,6 +96,7 @@ class ApiCacheManager {
Logger.info(`[ApiCacheManager] Resetting cache`)
this.init()
this.globalVersion++
this.cache.clear()
}
@@ -96,15 +123,20 @@ class ApiCacheManager {
res.send(cached.body)
return
}
const requestCacheVersion = this.getCacheVersion(key.url)
res.originalSend = res.send
res.send = (body) => {
Logger.debug(`[ApiCacheManager] Cache miss: ${stringifiedKey}`)
const cached = { body, headers: res.getHeaders(), statusCode: res.statusCode }
if (key.url.search(/^\/libraries\/.*?\/personalized/) !== -1) {
Logger.debug(`[ApiCacheManager] Caching with ${this.ttlOptions.ttl} ms TTL`)
this.cache.set(stringifiedKey, cached, this.ttlOptions)
if (this.getCacheVersion(key.url) !== requestCacheVersion) {
Logger.debug(`[ApiCacheManager] Skipping cache set for invalidated in-flight request: ${stringifiedKey}`)
} else {
this.cache.set(stringifiedKey, cached)
Logger.debug(`[ApiCacheManager] Cache miss: ${stringifiedKey}`)
const cached = { body, headers: res.getHeaders(), statusCode: res.statusCode }
if (PERSONALIZED_URL_PATTERN.test(key.url)) {
Logger.debug(`[ApiCacheManager] Caching with ${this.ttlOptions.ttl} ms TTL`)
this.cache.set(stringifiedKey, cached, this.ttlOptions)
} else {
this.cache.set(stringifiedKey, cached)
}
}
res.originalSend(body)
}