Add cache middleware to most /libraries get requests

This commit is contained in:
mikiher 2023-11-22 18:40:42 +02:00
parent 4299627f5f
commit 107b4b83c1
2 changed files with 42 additions and 37 deletions

View file

@ -1,42 +1,47 @@
const { LRUCache } = require('lru-cache')
const Logger = require('../Logger')
const { measure } = require('../utils/timing')
const Database = require('../Database')
class ApiCacheManager {
constructor() {
this.options = {
max: 1000,
maxSize: 10 * 1000 * 1000,
sizeCalculation: item => item.length,
}
constructor(options = { max: 1000, maxSize: 10 * 1000 * 1000, sizeCalculation: item => item.length }) {
this.options = options
}
init() {
init(database = Database) {
this.cache = new LRUCache(this.options)
let hooks = ['afterCreate', 'afterUpdate', 'afterDestroy', 'afterBulkCreate', 'afterBulkUpdate', 'afterBulkDestroy']
hooks.forEach(hook => database.sequelize.addHook(hook, (model) => this.clear(model, hook)))
}
clear(model, hook) {
Logger.debug(`[ApiCacheManager] ${model.constructor.name}.${hook}: Clearing cache`)
this.cache.clear()
}
get middleware() {
return (req, res, next) => {
measure('ApiCacheManager.middleware', () => {
const key = req.originalUrl || req.url
Logger.debug(`[ApiCacheManager] Cache key: ${key}`)
Logger.debug(`[ApiCacheManager] Cache: ${this.cache} count: ${this.cache.size} size: ${this.cache.calculatedSize}`)
const cached = this.cache.get(key)
if (cached) {
Logger.debug(`[ApiCacheManager] Cache hit: ${key}`)
res.send(cached)
return
const key = { user: req.user.username, url: req.url }
const stringifiedKey = JSON.stringify(key)
Logger.debug(`[ApiCacheManager] count: ${this.cache.size} size: ${this.cache.calculatedSize}`)
Logger.debug(`[ApiCacheManager] Cache key: ${stringifiedKey}`)
const cached = this.cache.get(stringifiedKey)
if (cached) {
Logger.debug(`[ApiCacheManager] Cache hit: ${stringifiedKey}`)
res.send(cached)
return
}
res.sendResponse = res.send
res.send = (body) => {
Logger.debug(`[ApiCacheManager] Cache miss: ${stringifiedKey}`)
if (key.url.search(/^\/libraries\/.*?\/personalized/) !== -1) {
Logger.debug(`[ApiCacheManager] Caching personalized with 30 minues TTL`)
this.cache.set(stringifiedKey, body, { ttl: 30 * 60 * 1000 })
} else {
this.cache.set(stringifiedKey, body)
}
res.sendResponse = res.send
res.send = (body) => {
Logger.debug(`[ApiCacheManager] Cache miss: ${key}`)
measure('ApiCacheManager.middleware: res.send', () => {
this.cache.set(key, body)
res.sendResponse(body)
})
}
next()
})
res.sendResponse(body)
}
next()
}
}
}