Skip to content

Added a custom prefix to cache keys#9

Open
Moath-SeveralBrands wants to merge 4 commits intomainfrom
Add-a-prefix-to-cache-in-Objection-GraphQL_two_CU-86etq20uh
Open

Added a custom prefix to cache keys#9
Moath-SeveralBrands wants to merge 4 commits intomainfrom
Add-a-prefix-to-cache-in-Objection-GraphQL_two_CU-86etq20uh

Conversation

@Moath-SeveralBrands
Copy link

@Moath-SeveralBrands Moath-SeveralBrands commented Jun 4, 2025

Description

Summary

  • Added a custom prefix to the generated cache keys.
  • The custom cache key should be appended in the request as a variable named requestCacheKey. Note that requestCacheKey should be set in the req object itself (that is, req.requestCacheKey).

Ticket Link

TICKET LINK

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented on my code, particularly in hard-to-understand areas
  • My changes generate no new warnings

@msalahat
Copy link
Contributor

msalahat commented Jun 4, 2025

@Moath-SeveralBrands Moath-SeveralBrands changed the title Added the X-DOMAIN-NAME header as a prefix to cache keys Added a custom prefix to cache keys Jun 11, 2025
@firassziedan firassziedan requested a review from Copilot June 11, 2025 08:13
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Introduces an optional requestCacheKey suffix to all generated Redis cache keys and propagates it through the schema resolution cache lookup.

  • Add requestCacheKey parameter to generateRedisKey and getCache in the caching layer.
  • Append the custom cache key fragment when building the Redis key.
  • Pass requestCacheKey from the GraphQL resolver into the cache check in SchemaBuilder.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
lib/cache/caching.js Updated generateRedisKey and getCache signatures to accept requestCacheKey and append it to the final key.
lib/SchemaBuilder.js Modified _checkCache call to forward request.req.requestCacheKey into the new cache methods.
Comments suppressed due to low confidence (5)

lib/cache/caching.js:50

  • Consider adding or updating JSDoc comments to describe the new requestCacheKey parameter and its effect on the generated key.
generateRedisKey(request, requestCacheKey) {

lib/cache/caching.js:63

  • Update or add JSDoc on getCache to reflect the added requestCacheKey parameter and how it alters cache lookups.
async getCache(request, requestCacheKey) {

lib/cache/caching.js:1

  • Check other cache methods (e.g., setCache, deleteCache) for consistency: they may also need to accept requestCacheKey so all operations use the same key format.
class Cache {

lib/cache/caching.js:55

  • There are no existing tests for the new requestCacheKey path. Add unit tests to verify key generation with and without a custom prefix.
const cacheKey = requestCacheKey ? `_${requestCacheKey}` : ''

lib/SchemaBuilder.js:304

  • Accessing request.req.requestCacheKey without optional chaining could throw if request.req is undefined. Consider using request?.req?.requestCacheKey.
const cached = await this._checkCache(cacheKeyElements, request.req.requestCacheKey)

Comment on lines +54 to +57

const cacheKey = requestCacheKey ? `_${requestCacheKey}` : ''

return `${this.redisKeyPrefix}${cacheKey}_${md5(JSON.stringify(bodyData))}`
Copy link

Copilot AI Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] You could simplify the underscore logic by building an array of segments and joining with underscores, e.g., [this.redisKeyPrefix, requestCacheKey, md5(...)] after filtering out empty values.

Suggested change
const cacheKey = requestCacheKey ? `_${requestCacheKey}` : ''
return `${this.redisKeyPrefix}${cacheKey}_${md5(JSON.stringify(bodyData))}`
const segments = [
this.redisKeyPrefix,
requestCacheKey,
md5(JSON.stringify(bodyData))
].filter(Boolean)
return segments.join('_')

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants