From 4dec8c265d15fef8ea65b74690354684e51eb369 Mon Sep 17 00:00:00 2001 From: mikiher Date: Fri, 17 Nov 2023 08:47:40 +0200 Subject: [PATCH] Add ApiCacheManager --- server/managers/ApiCacheManager | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 server/managers/ApiCacheManager diff --git a/server/managers/ApiCacheManager b/server/managers/ApiCacheManager new file mode 100644 index 00000000..9d80fdb2 --- /dev/null +++ b/server/managers/ApiCacheManager @@ -0,0 +1,43 @@ +const { LRUCache } = require('lru-cache') +const Logger = require('../Logger') +const { measure } = require('../utils/timing') + +class ApiCacheManager { + constructor() { + this.options = { + max: 1000, + maxSize: 10 * 1000 * 1000, + sizeCalculation: item => item.length, + } + } + + init() { + this.cache = new LRUCache(this.options) + } + + 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 + } + 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() + }) + } + } +} +module.exports = ApiCacheManager \ No newline at end of file