Implement cache entry removal queue in diskCache

587c528f6de1139ca711273562095955b18e90d0

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +36 -8Showing whitespace changes
src/endpoints/characters.js+36 -8
@@ -39,6 +39,35 @@ const diskCache = {
3939 */
4040 _instance: null,
4141 /**
42+ * @type {NodeJS.Timeout?}
43+ * @private
44+ */
45+ _removalInterval: null,
46+ /**
47+ * Processes the removal queue.
48+ * @returns {Promise<void>}
49+ * @private
50+ */
51+ _removeCacheEntries: async function() {
52+ try {
53+ if (!useDiskCache || this.removalQueue.length === 0) {
54+ return;
55+ }
56+
57+ const keys = await diskCache.instance().then(i => i.keys());
58+ for (const item of this.removalQueue) {
59+ const key = keys.find(k => k.startsWith(item));
60+ if (key) {
61+ await diskCache.instance().then(i => i.removeItem(key));
62+ }
63+ }
64+ console.info('Removed cache entries:', this.removalQueue);
65+ this.removalQueue = [];
66+ } catch (error) {
67+ console.error('Error while removing cache entries:', error);
68+ }
69+ },
70+ /**
4271 * Gets the disk cache instance.
4372 * @returns {Promise<import('node-persist').LocalStorage>}
4473 */
@@ -50,8 +79,14 @@ const diskCache = {
5079 const cacheDir = path.join(globalThis.DATA_ROOT, '_cache', 'characters');
5180 this._instance = storage.create({ dir: cacheDir, ttl: false });
5281 await this._instance.init();
82+ this._removalInterval = setInterval(this._removeCacheEntries.bind(this), 60000);
5383 return this._instance;
5484 },
85+ /**
86+ * Queue for removal of cache entries.
87+ * @type {string[]}
88+ */
89+ removalQueue: [],
5590};
5691
5792/**
@@ -131,15 +166,8 @@ async function writeCharacterData(inputFile, data, outputFile, request, crop = u
131166 }
132167 }
133168 if (useDiskCache && !Buffer.isBuffer(inputFile)) {
134- const keys = await diskCache.instance().then(i => i.keys());
169+ diskCache.removalQueue.push(inputFile);
135- for (const key of keys) {
136- if (key.startsWith(inputFile)) {
137- await diskCache.instance().then(i => i.removeItem(key));
138- break;
139170 }
140- }
141- }
142-
143171 /**
144172 * Read the image, resize, and save it as a PNG into the buffer.
145173 * @returns {Promise<Buffer>} Image buffer