From 587c528f6de1139ca711273562095955b18e90d0 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 19 Mar 2025 23:31:30 +0200 Subject: [PATCH] Implement cache entry removal queue in diskCache --- src/endpoints/characters.js | 44 ++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/endpoints/characters.js b/src/endpoints/characters.js index 2794658ba..c539cda35 100644 --- a/src/endpoints/characters.js +++ b/src/endpoints/characters.js @@ -38,6 +38,35 @@ const diskCache = { * @private */ _instance: null, + /** + * @type {NodeJS.Timeout?} + * @private + */ + _removalInterval: null, + /** + * Processes the removal queue. + * @returns {Promise} + * @private + */ + _removeCacheEntries: async function() { + try { + if (!useDiskCache || this.removalQueue.length === 0) { + return; + } + + const keys = await diskCache.instance().then(i => i.keys()); + for (const item of this.removalQueue) { + const key = keys.find(k => k.startsWith(item)); + if (key) { + await diskCache.instance().then(i => i.removeItem(key)); + } + } + console.info('Removed cache entries:', this.removalQueue); + this.removalQueue = []; + } catch (error) { + console.error('Error while removing cache entries:', error); + } + }, /** * Gets the disk cache instance. * @returns {Promise} @@ -50,8 +79,14 @@ const diskCache = { const cacheDir = path.join(globalThis.DATA_ROOT, '_cache', 'characters'); this._instance = storage.create({ dir: cacheDir, ttl: false }); await this._instance.init(); + this._removalInterval = setInterval(this._removeCacheEntries.bind(this), 60000); return this._instance; }, + /** + * Queue for removal of cache entries. + * @type {string[]} + */ + removalQueue: [], }; /** @@ -131,15 +166,8 @@ async function writeCharacterData(inputFile, data, outputFile, request, crop = u } } if (useDiskCache && !Buffer.isBuffer(inputFile)) { - const keys = await diskCache.instance().then(i => i.keys()); - for (const key of keys) { - if (key.startsWith(inputFile)) { - await diskCache.instance().then(i => i.removeItem(key)); - break; - } - } + diskCache.removalQueue.push(inputFile); } - /** * Read the image, resize, and save it as a PNG into the buffer. * @returns {Promise} Image buffer