Skip to content

Commit 00e9156

Browse files
committed
Merge branch '224-cluster-caching' of https://github.com/CenterForDigitalHumanities/rerum_server_nodejs into 224-cluster-caching
2 parents ae2d163 + 293e589 commit 00e9156

2 files changed

Lines changed: 7 additions & 24 deletions

File tree

cache/index.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,8 @@ class ClusterCache {
261261
/**
262262
* Delete specific key from cache
263263
* @param {string} key - Cache key to delete
264-
* @param {boolean} countAsInvalidation - Deprecated parameter (kept for backwards compatibility)
265264
*/
266-
async delete(key, countAsInvalidation = false) {
267-
const startTime = Date.now()
268-
const workerId = process.env.pm_id || process.pid
269-
265+
async delete(key) {
270266
try {
271267
// Only delete from cluster cache in PM2 mode to avoid IPC timeouts
272268
if (this.isPM2) {
@@ -279,7 +275,6 @@ class ClusterCache {
279275
this.keySizes.delete(key)
280276
this.totalBytes -= size
281277
this.localCache.delete(key)
282-
const duration = Date.now() - startTime
283278
return true
284279
} catch (err) {
285280
this.localCache.delete(key)
@@ -289,7 +284,6 @@ class ClusterCache {
289284
const size = this.keySizes.get(key) || 0
290285
this.keySizes.delete(key)
291286
this.totalBytes -= size
292-
const duration = Date.now() - startTime
293287
return false
294288
}
295289
}
@@ -494,7 +488,7 @@ class ClusterCache {
494488
}
495489

496490
if (regex.test(key)) {
497-
deletePromises.push(this.delete(key, true))
491+
deletePromises.push(this.delete(key))
498492
matchedKeys.push(key)
499493
invalidatedKeys.add(key)
500494
count++
@@ -800,9 +794,6 @@ class ClusterCache {
800794
* @returns {Promise<number>} Number of cache entries invalidated
801795
*/
802796
async invalidateByObject(obj, invalidatedKeys = new Set()) {
803-
const startTime = Date.now()
804-
const workerId = process.env.pm_id || process.pid
805-
806797
if (!obj || typeof obj !== 'object') {
807798
return 0
808799
}
@@ -814,7 +805,6 @@ class ClusterCache {
814805
if (this.isPM2) {
815806
try {
816807
// Scan all keys directly from cluster cache (all workers)
817-
const clusterGetStart = Date.now()
818808
const keysMap = await this.clusterCache.keys()
819809
const uniqueKeys = new Set()
820810

@@ -830,7 +820,6 @@ class ClusterCache {
830820
}
831821

832822
keysToCheck = Array.from(uniqueKeys)
833-
const clusterGetDuration = Date.now() - clusterGetStart
834823
} catch (err) {
835824
keysToCheck = Array.from(this.allKeys).filter(k =>
836825
k.startsWith('query:') || k.startsWith('search:') || k.startsWith('searchPhrase:')
@@ -881,7 +870,7 @@ class ClusterCache {
881870
const queryParams = JSON.parse(queryJson)
882871

883872
if (this.objectMatchesQuery(obj, queryParams)) {
884-
await this.delete(cacheKey, true) // Pass true to count this deletion
873+
await this.delete(cacheKey)
885874
invalidatedKeys.add(cacheKey)
886875
count++
887876
}
@@ -891,8 +880,6 @@ class ClusterCache {
891880
}
892881
}
893882

894-
const duration = Date.now() - startTime
895-
896883
return count
897884
}
898885

cache/middleware.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,12 @@ const invalidateCache = (req, res, next) => {
224224
const primeId = extractId(updatedObject?.__rerum?.history?.prime)
225225

226226
if (!invalidatedKeys.has(`id:${objIdShort}`)) {
227-
cache.delete(`id:${objIdShort}`, true)
227+
cache.delete(`id:${objIdShort}`)
228228
invalidatedKeys.add(`id:${objIdShort}`)
229229
}
230230

231231
if (previousId && previousId !== 'root' && !invalidatedKeys.has(`id:${previousId}`)) {
232-
cache.delete(`id:${previousId}`, true)
232+
cache.delete(`id:${previousId}`)
233233
invalidatedKeys.add(`id:${previousId}`)
234234
}
235235

@@ -261,12 +261,12 @@ const invalidateCache = (req, res, next) => {
261261
const primeId = extractId(deletedObject?.__rerum?.history?.prime)
262262

263263
if (!invalidatedKeys.has(`id:${objIdShort}`)) {
264-
cache.delete(`id:${objIdShort}`, true)
264+
cache.delete(`id:${objIdShort}`)
265265
invalidatedKeys.add(`id:${objIdShort}`)
266266
}
267267

268268
if (previousId && previousId !== 'root' && !invalidatedKeys.has(`id:${previousId}`)) {
269-
cache.delete(`id:${previousId}`, true)
269+
cache.delete(`id:${previousId}`)
270270
invalidatedKeys.add(`id:${previousId}`)
271271
}
272272

@@ -287,15 +287,11 @@ const invalidateCache = (req, res, next) => {
287287
}
288288

289289
res.json = async (data) => {
290-
// Add worker ID header for debugging cache sync
291-
res.set('X-Worker-ID', process.env.pm_id || process.pid)
292290
await performInvalidation(data)
293291
return originalJson(data)
294292
}
295293

296294
res.send = async (data) => {
297-
// Add worker ID header for debugging cache sync
298-
res.set('X-Worker-ID', process.env.pm_id || process.pid)
299295
await performInvalidation(data)
300296
return originalSend(data)
301297
}

0 commit comments

Comments
 (0)