Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/SchemaBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)), 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
Expand Down Expand Up @@ -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
}
}
Expand Down
19 changes: 11 additions & 8 deletions lib/cache/caching.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,25 @@ class Cache {
this.enabled = true
}

generateRedisKey(request) {
generateRedisKey(request, requestCacheKey) {
try {
const bodyData = request
if (!bodyData) return null
return `${this.redisKeyPrefix}_${md5(JSON.stringify(bodyData))}`

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

return `${this.redisKeyPrefix}${cacheKey}_${md5(JSON.stringify(bodyData))}`
Comment on lines +54 to +57

Copilot AI Jun 11, 2025

Copy link

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.
} catch (error) {
return null
}
}

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) => {
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)
Expand All @@ -71,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))
}
}
}
Expand Down