Skip to content

Commit 7207f41

Browse files
committed
Check the cache key
1 parent 71f3ecc commit 7207f41

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lib/cache/caching.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ class Cache {
4343
}
4444

4545
generateRedisKey(request) {
46-
return `${this.redisKeyPrefix}_${md5(request.body)}`
46+
try {
47+
const bodyData = request.body || request.res
48+
return `${this.redisKeyPrefix}_${md5(bodyData)}`
49+
} catch (error) {
50+
return null
51+
}
4752
}
4853

4954
async getCache(request) {
50-
if (this.enabled) {
55+
const cacheKey = this.generateRedisKey(request)
56+
if (this.enabled && cacheKey) {
5157
return new Promise((resolve, reject) => {
5258
this.redisClient.get(this.generateRedisKey(request), (error, value) => {
5359
if (error) return reject(error)
@@ -59,7 +65,8 @@ class Cache {
5965
}
6066

6167
async cacheResult(request, result) {
62-
if (this.enabled) {
68+
const cacheKey = this.generateRedisKey(request)
69+
if (this.enabled && cacheKey) {
6370
await this.redisClient.setex(this.generateRedisKey(request), parseInt(this.timeout), JSON.stringify(result))
6471
}
6572
}

0 commit comments

Comments
 (0)