From 1f2197bfa154b8f082c68dd9dd206b08a4fce993 Mon Sep 17 00:00:00 2001 From: Moath-SeveralBrands Date: Wed, 4 Jun 2025 14:46:34 +0300 Subject: [PATCH 1/4] Added the X-DOMAIN-NAME header as a prefix to cache keys --- lib/cache/caching.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cache/caching.js b/lib/cache/caching.js index 201b61a..c46456b 100644 --- a/lib/cache/caching.js +++ b/lib/cache/caching.js @@ -51,7 +51,7 @@ class Cache { try { const bodyData = request if (!bodyData) return null - return `${this.redisKeyPrefix}_${md5(JSON.stringify(bodyData))}` + return `${request.req.headers['x-domain-name']}_${this.redisKeyPrefix}_${md5(JSON.stringify(bodyData))}` } catch (error) { return null } From 8c4bf9376bd2e8c2aa99de405b58539ee0fdd634 Mon Sep 17 00:00:00 2001 From: Moath-SeveralBrands Date: Wed, 4 Jun 2025 18:19:01 +0300 Subject: [PATCH 2/4] Changed the logic of the prefix to be more generic --- lib/SchemaBuilder.js | 2 +- lib/cache/caching.js | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/SchemaBuilder.js b/lib/SchemaBuilder.js index 8e6da93..38c90e4 100644 --- a/lib/SchemaBuilder.js +++ b/lib/SchemaBuilder.js @@ -299,7 +299,7 @@ class SchemaBuilder { _resolverForModel(modelData, extraQuery) { return async(ctx, ignore1, request, data) => { - const cacheKeyElements = { res: JSON.parse(JSON.stringify(request.res || request.body)), ignore1 } + const cacheKeyElements = { res: JSON.parse(JSON.stringify(request.res || request.body)), requestCacheKey: request.req.requestCacheKey, ignore1 } const cached = await this._checkCache(cacheKeyElements) const skipCache = request?.res?.skipCache ?? request?.body?.skipCache ?? false diff --git a/lib/cache/caching.js b/lib/cache/caching.js index c46456b..04039a2 100644 --- a/lib/cache/caching.js +++ b/lib/cache/caching.js @@ -49,9 +49,12 @@ class Cache { generateRedisKey(request) { try { - const bodyData = request + const { requestCacheKey, ...bodyData } = request if (!bodyData) return null - return `${request.req.headers['x-domain-name']}_${this.redisKeyPrefix}_${md5(JSON.stringify(bodyData))}` + + const cacheKey = requestCacheKey ? `_${requestCacheKey}` : '' + + return `${this.redisKeyPrefix}${cacheKey}_${md5(JSON.stringify(bodyData))}` } catch (error) { return null } From 6c66beef1d18bc973be8b0e07c01ab69b2206865 Mon Sep 17 00:00:00 2001 From: Moath-SeveralBrands Date: Wed, 11 Jun 2025 10:44:28 +0300 Subject: [PATCH 3/4] Moved requestCacheKey into a separate parameter --- lib/SchemaBuilder.js | 8 ++++---- lib/cache/caching.js | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/SchemaBuilder.js b/lib/SchemaBuilder.js index 38c90e4..72903ae 100644 --- a/lib/SchemaBuilder.js +++ b/lib/SchemaBuilder.js @@ -293,15 +293,15 @@ class SchemaBuilder { throw new Error(`relation type "${relation.constructor.name}" is not supported`) } - async _checkCache(request) { - return await this.cache.getCache(request) || null + async _checkCache(request, requestCacheKey) { + return await this.cache.getCache(request, requestCacheKey) || null } _resolverForModel(modelData, extraQuery) { return async(ctx, ignore1, request, data) => { - const cacheKeyElements = { res: JSON.parse(JSON.stringify(request.res || request.body)), requestCacheKey: request.req.requestCacheKey, ignore1 } + const cacheKeyElements = { res: JSON.parse(JSON.stringify(request.res || request.body)), ignore1 } - const cached = await this._checkCache(cacheKeyElements) + const cached = await this._checkCache(cacheKeyElements, request.req.requestCacheKey) const skipCache = request?.res?.skipCache ?? request?.body?.skipCache ?? false if (cached && !skipCache) { return cached diff --git a/lib/cache/caching.js b/lib/cache/caching.js index 04039a2..25a2987 100644 --- a/lib/cache/caching.js +++ b/lib/cache/caching.js @@ -47,9 +47,9 @@ class Cache { this.enabled = true } - generateRedisKey(request) { + generateRedisKey(request, requestCacheKey) { try { - const { requestCacheKey, ...bodyData } = request + const bodyData = request if (!bodyData) return null const cacheKey = requestCacheKey ? `_${requestCacheKey}` : '' @@ -60,8 +60,8 @@ class Cache { } } - async getCache(request) { - const cacheKey = this.generateRedisKey(request) + async getCache(request, requestCacheKey) { + const cacheKey = this.generateRedisKey(request, requestCacheKey) if (this.enabled && cacheKey) { return new Promise((resolve, reject) => { From 4ce495b1d1653bfff1304550b83ea6638122eda6 Mon Sep 17 00:00:00 2001 From: Moath-SeveralBrands Date: Wed, 11 Jun 2025 14:40:55 +0300 Subject: [PATCH 4/4] Added requestCacheKey to cacheResult also --- lib/SchemaBuilder.js | 2 +- lib/cache/caching.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/SchemaBuilder.js b/lib/SchemaBuilder.js index 72903ae..6624265 100644 --- a/lib/SchemaBuilder.js +++ b/lib/SchemaBuilder.js @@ -345,7 +345,7 @@ class SchemaBuilder { } const result = await builder.then(toJson) - this.cache && result ? await this.cache?.cacheResult(cacheKeyElements, result) : null + this.cache && result ? await this.cache?.cacheResult(cacheKeyElements, result, request.req.requestCacheKey) : null return result } } diff --git a/lib/cache/caching.js b/lib/cache/caching.js index 25a2987..b07b19d 100644 --- a/lib/cache/caching.js +++ b/lib/cache/caching.js @@ -65,7 +65,7 @@ class Cache { if (this.enabled && cacheKey) { return new Promise((resolve, reject) => { - this.redisClient.get(this.generateRedisKey(request), (error, value) => { + this.redisClient.get(this.generateRedisKey(request, requestCacheKey), (error, value) => { if (error) return reject(error) if (value) return resolve(JSON.parse(value)) return resolve(null) @@ -74,10 +74,10 @@ class Cache { } } - async cacheResult(request, result) { - const cacheKey = this.generateRedisKey(request) + async cacheResult(request, result, requestCacheKey) { + const cacheKey = this.generateRedisKey(request, requestCacheKey) if (this.enabled && cacheKey) { - await this.redisClient.setex(this.generateRedisKey(request), parseInt(this.timeout), JSON.stringify(result)) + await this.redisClient.setex(this.generateRedisKey(request, requestCacheKey), parseInt(this.timeout), JSON.stringify(result)) } } }