perf: tighten derived filter cache policy

This commit is contained in:
Jonathan Finley 2026-03-13 23:07:57 -04:00
parent cdb0205f8d
commit 80eaccd1e6
7 changed files with 131 additions and 139 deletions

View file

@ -21,6 +21,7 @@ class Database {
// Cached library filter data
this.libraryFilterData = {}
this.libraryFilterDataTtlMs = 1000 * 60 * 30
/** @type {import('./objects/settings/ServerSettings')} */
this.serverSettings = null
@ -583,6 +584,35 @@ class Database {
this.libraryFilterData[libraryId].languages.push(language)
}
getLibraryFilterCache(libraryId, { includeExpired = false } = {}) {
const cacheEntry = this.libraryFilterData[libraryId]
if (!cacheEntry) return undefined
if (includeExpired) return cacheEntry
if (cacheEntry.expiresAt && cacheEntry.expiresAt > Date.now()) {
return cacheEntry
}
delete this.libraryFilterData[libraryId]
return undefined
}
setLibraryFilterCache(libraryId, data, ttlMs = this.libraryFilterDataTtlMs) {
const loadedAt = Date.now()
const expiresAt = loadedAt + ttlMs
const cacheEntry = {
...data,
loadedAt,
expiresAt
}
this.libraryFilterData[libraryId] = cacheEntry
return cacheEntry
}
invalidateLibraryFilterCache(libraryId) {
delete this.libraryFilterData[libraryId]
}
/**
* Used when updating items to make sure author id exists
* If library filter data is set then use that for check