@@ -5,19 +5,17 @@ const Keyv = require('keyv')
5
5
const BPromise = require ( 'bluebird' )
6
6
const crypto = require ( 'crypto' )
7
7
8
- const cache = new Keyv ( )
9
-
10
8
const CACHEABLE_METHODS = [ 'GET' ]
11
9
const INTERVAL = 200
12
10
const X_RESPONSE_CACHE = 'x-response-cache'
13
11
const X_RESPONSE_CACHE_HIT = 'hit'
14
12
const X_RESPONSE_CACHE_MISS = 'miss'
15
13
16
- const isCacheableRequest = ( req ) => {
14
+ function isCacheableRequest ( req ) {
17
15
return CACHEABLE_METHODS . includes ( req . raw . method )
18
16
}
19
17
20
- const buildCacheKey = ( req , { headers} ) => {
18
+ function buildCacheKey ( req , { headers} ) {
21
19
const { url, headers : requestHeaders } = req . raw
22
20
const additionalCondition = headers . reduce ( ( acc , header ) => {
23
21
return `${ acc } __${ header } :${ requestHeaders [ header ] || '' } `
@@ -29,7 +27,7 @@ const buildCacheKey = (req, {headers}) => {
29
27
return key
30
28
}
31
29
32
- const waitForCacheFulfilled = async ( key , timeout ) => {
30
+ async function waitForCacheFulfilled ( cache , key , timeout ) {
33
31
let cachedString = await cache . get ( key )
34
32
let waitedFor = 0
35
33
@@ -46,54 +44,53 @@ const waitForCacheFulfilled = async (key, timeout) => {
46
44
return cachedString
47
45
}
48
46
49
- const createOnRequestHandler = ( {
50
- ttl,
51
- additionalCondition : { headers} ,
52
- } ) => async ( req , res ) => {
53
- if ( ! isCacheableRequest ( req ) ) {
54
- return
55
- }
47
+ function createOnRequestHandler ( { ttl, additionalCondition : { headers} } ) {
48
+ return async function handler ( req , res ) {
49
+ if ( ! isCacheableRequest ( req ) ) {
50
+ return
51
+ }
56
52
57
- const key = buildCacheKey ( req , { headers} )
58
- const requestKey = `${ key } __requested`
59
- const isRequestExisted = await cache . get ( requestKey )
53
+ const cache = this . responseCache
54
+ const key = buildCacheKey ( req , { headers} )
55
+ const requestKey = `${ key } __requested`
56
+ const isRequestExisted = await cache . get ( requestKey )
60
57
61
- if ( isRequestExisted ) {
62
- const cachedString = await waitForCacheFulfilled ( key , ttl )
58
+ if ( isRequestExisted ) {
59
+ const cachedString = await waitForCacheFulfilled ( cache , key , ttl )
63
60
64
- if ( cachedString ) {
65
- const cached = JSON . parse ( cachedString )
66
- res . header ( X_RESPONSE_CACHE , X_RESPONSE_CACHE_HIT )
61
+ if ( cachedString ) {
62
+ const cached = JSON . parse ( cachedString )
63
+ res . header ( X_RESPONSE_CACHE , X_RESPONSE_CACHE_HIT )
67
64
68
- return res . code ( cached . statusCode ) . send ( cached . payload )
65
+ return res . code ( cached . statusCode ) . send ( cached . payload )
66
+ } else {
67
+ res . header ( X_RESPONSE_CACHE , X_RESPONSE_CACHE_MISS )
68
+ }
69
69
} else {
70
+ await cache . set ( requestKey , 'cached' , ttl )
70
71
res . header ( X_RESPONSE_CACHE , X_RESPONSE_CACHE_MISS )
71
72
}
72
- } else {
73
- await cache . set ( requestKey , 'cached' , ttl )
74
- res . header ( X_RESPONSE_CACHE , X_RESPONSE_CACHE_MISS )
75
73
}
76
74
}
77
75
78
- const createOnSendHandler = ( { ttl, additionalCondition : { headers} } ) => async (
79
- req ,
80
- res ,
81
- payload ,
82
- ) => {
83
- if ( ! isCacheableRequest ( req ) ) {
84
- return
85
- }
86
-
87
- const key = buildCacheKey ( req , { headers} )
76
+ function createOnSendHandler ( { ttl, additionalCondition : { headers} } ) {
77
+ return async function handler ( req , res , payload ) {
78
+ if ( ! isCacheableRequest ( req ) ) {
79
+ return
80
+ }
88
81
89
- await cache . set (
90
- key ,
91
- JSON . stringify ( {
92
- statusCode : res . statusCode ,
93
- payload,
94
- } ) ,
95
- ttl ,
96
- )
82
+ const cache = this . responseCache
83
+ const key = buildCacheKey ( req , { headers} )
84
+
85
+ await cache . set (
86
+ key ,
87
+ JSON . stringify ( {
88
+ statusCode : res . statusCode ,
89
+ payload,
90
+ } ) ,
91
+ ttl ,
92
+ )
93
+ }
97
94
}
98
95
99
96
const responseCachingPlugin = (
@@ -103,7 +100,9 @@ const responseCachingPlugin = (
103
100
) => {
104
101
const headers = additionalCondition . headers || [ ]
105
102
const opts = { ttl, additionalCondition : { headers} }
103
+ const responseCache = new Keyv ( )
106
104
105
+ instance . decorate ( 'responseCache' , responseCache )
107
106
instance . addHook ( 'onRequest' , createOnRequestHandler ( opts ) )
108
107
instance . addHook ( 'onSend' , createOnSendHandler ( opts ) )
109
108
0 commit comments