@@ -38,103 +38,112 @@ async function hash(string) {
38
38
return hashHex ;
39
39
}
40
40
41
- // Updates the cache with the results of a query
42
- // :param json: the incoming request in json
43
- // :param body: the body to cache
44
- // :return: true if successful, false if not
45
- async function updateCache ( env , query , variables , body , ttl = '' , specialCache = '' ) {
46
- try {
47
- if ( ! env . CACHE_BASIC_AUTH ) {
48
- console . warn ( 'env.CACHE_BASIC_AUTH is not set; skipping cache check' ) ;
49
- return false ;
50
- }
51
- if ( cachePaused ) {
52
- console . warn ( 'Cache paused; skipping cache update' ) ;
53
- return false ;
41
+ const cacheMachine = {
42
+ createKey : ( environment , query , variables , specialCache = '' ) => {
43
+ if ( typeof variables !== 'string' ) {
44
+ variables = JSON . stringify ( variables ) ;
54
45
}
55
- // Get the cacheKey from the request
56
46
query = query . trim ( ) ;
57
- const cacheKey = await hash ( env . ENVIRONMENT + query + JSON . stringify ( variables ) + specialCache ) ;
58
- console . log ( `Caching ${ body . length } byte response for ${ env . ENVIRONMENT } environment${ ttl ? ` for ${ ttl } seconds` : '' } ` ) ;
59
-
60
- // headers and POST body
61
- const headersPost = {
62
- body : JSON . stringify ( { key : cacheKey , value : body , ttl } ) ,
63
- method : 'POST' ,
64
- headers : {
65
- 'content-type' : 'application/json;charset=UTF-8' ,
66
- 'Authorization' : `Basic ${ env . CACHE_BASIC_AUTH } `
67
- } ,
68
- timeout : 10000 ,
69
- } ;
70
-
71
- // Update the cache
72
- const response = await fetchWithTimeout ( `${ cacheUrl } /api/cache` , headersPost ) ;
73
-
74
- // Log non-200 responses
75
- if ( response . status !== 200 ) {
76
- console . error ( `failed to write to cache: ${ response . status } ` ) ;
47
+ return hash ( environment + query + variables + specialCache ) ;
48
+ } ,
49
+ // Checks the caching service to see if a request has been cached
50
+ // :param json: the json payload of the incoming worker request
51
+ // :return: json results of the item found in the cache or false if not found
52
+ get : async ( env , query , variables , specialCache = '' ) => {
53
+ try {
54
+ if ( ! env . CACHE_BASIC_AUTH ) {
55
+ console . warn ( 'env.CACHE_BASIC_AUTH is not set; skipping cache check' ) ;
56
+ return false ;
57
+ }
58
+ if ( cachePaused ) {
59
+ console . warn ( 'Cache paused; skipping cache check' ) ;
60
+ return false ;
61
+ }
62
+ query = query . trim ( ) ;
63
+ const cacheKey = await cacheMachine . createKey ( env . ENVIRONMENT , query , variables , specialCache ) ;
64
+ if ( ! cacheKey ) {
65
+ console . warn ( 'Skipping cache check; key is empty' ) ;
66
+ return false ;
67
+ }
68
+
69
+ const response = await fetchWithTimeout ( `${ cacheUrl } /api/cache?key=${ cacheKey } ` , {
70
+ headers : {
71
+ 'content-type' : 'application/json;charset=UTF-8' ,
72
+ 'Authorization' : `Basic ${ env . CACHE_BASIC_AUTH } `
73
+ } ,
74
+ } ) ;
75
+ cacheFailCount = 0 ;
76
+ if ( response . status === 200 ) {
77
+ return await response . json ( ) ;
78
+ } else if ( response . status !== 404 ) {
79
+ console . error ( `failed to read from cache: ${ response . status } ` ) ;
80
+ }
81
+
77
82
return false
78
- }
79
- cacheFailCount = 0 ;
80
- return true
81
- } catch ( error ) {
82
- if ( error . message === 'The operation was aborted due to timeout' ) {
83
- console . warn ( 'Updating cache timed out' ) ;
84
- pauseCache ( ) ;
85
- return false ;
86
- }
87
- console . error ( 'updateCache error: ' + error . message ) ;
88
- return false ;
89
- }
90
- }
91
-
92
- // Checks the caching service to see if a request has been cached
93
- // :param json: the json payload of the incoming worker request
94
- // :return: json results of the item found in the cache or false if not found
95
- async function checkCache ( env , query , variables , specialCache = '' ) {
96
- try {
97
- if ( ! env . CACHE_BASIC_AUTH ) {
98
- console . warn ( 'env.CACHE_BASIC_AUTH is not set; skipping cache check' ) ;
83
+ } catch ( error ) {
84
+ if ( error . message === 'The operation was aborted due to timeout' ) {
85
+ console . warn ( 'Checking cache timed out' ) ;
86
+ pauseCache ( ) ;
87
+ return false ;
88
+ }
89
+ console . error ( 'checkCache error: ' + error . message ) ;
99
90
return false ;
100
91
}
101
- if ( cachePaused ) {
102
- console . warn ( 'Cache paused; skipping cache check' ) ;
92
+ } ,
93
+ // Updates the cache with the results of a query
94
+ // :param json: the incoming request in json
95
+ // :param body: the body to cache
96
+ // :return: true if successful, false if not
97
+ put : async ( env , body , options = { } ) => {
98
+ try {
99
+ if ( ! env . CACHE_BASIC_AUTH ) {
100
+ console . warn ( 'env.CACHE_BASIC_AUTH is not set; skipping cache put' ) ;
101
+ return false ;
102
+ }
103
+ if ( cachePaused ) {
104
+ console . warn ( 'Cache paused; skipping cache update' ) ;
105
+ return false ;
106
+ }
107
+ if ( ! options . key && ! options . query ) {
108
+ console . warn ( 'Key or query not provided, skipping cache put' ) ;
109
+ return false ;
110
+ }
111
+ let { key, query, variables, ttl = 60 , specialCache = '' } = options ;
112
+ if ( ! key ) {
113
+ query = query . trim ( ) ;
114
+ key = await cacheMachine . createKey ( env . ENVIRONMENT , query , variables , specialCache ) ;
115
+ }
116
+ ttl = String ( ttl ) ;
117
+ console . log ( `Caching ${ body . length } byte response for ${ env . ENVIRONMENT } environment${ ttl ? ` for ${ ttl } seconds` : '' } ` ) ;
118
+
119
+ // Update the cache
120
+ const response = await fetchWithTimeout ( `${ cacheUrl } /api/cache` , {
121
+ body : JSON . stringify ( { key, value : body , ttl } ) ,
122
+ method : 'POST' ,
123
+ headers : {
124
+ 'content-type' : 'application/json;charset=UTF-8' ,
125
+ 'Authorization' : `Basic ${ env . CACHE_BASIC_AUTH } `
126
+ } ,
127
+ timeout : 10000 ,
128
+ } ) ;
129
+
130
+ // Log non-200 responses
131
+ if ( response . status !== 200 ) {
132
+ console . error ( `failed to write to cache: ${ response . status } ` ) ;
133
+ return false
134
+ }
135
+ cacheFailCount = 0 ;
136
+ return true
137
+ } catch ( error ) {
138
+ if ( error . message === 'The operation was aborted due to timeout' ) {
139
+ console . warn ( 'Updating cache timed out' ) ;
140
+ pauseCache ( ) ;
141
+ return false ;
142
+ }
143
+ console . error ( 'updateCache error: ' + error . message ) ;
103
144
return false ;
104
145
}
105
- query = query . trim ( ) ;
106
- const cacheKey = await hash ( env . ENVIRONMENT + query + JSON . stringify ( variables ) + specialCache ) ;
107
- if ( ! cacheKey ) {
108
- console . warn ( 'Skipping cache check; key is empty' ) ;
109
- return false ;
110
- }
111
-
112
- const response = await fetchWithTimeout ( `${ cacheUrl } /api/cache?key=${ cacheKey } ` , {
113
- headers : {
114
- 'content-type' : 'application/json;charset=UTF-8' ,
115
- 'Authorization' : `Basic ${ env . CACHE_BASIC_AUTH } `
116
- } ,
117
- } ) ;
118
- cacheFailCount = 0 ;
119
- if ( response . status === 200 ) {
120
- return await response . json ( ) ;
121
- } else if ( response . status !== 404 ) {
122
- console . error ( `failed to read from cache: ${ response . status } ` ) ;
123
- }
124
-
125
- return false
126
- } catch ( error ) {
127
- if ( error . message === 'The operation was aborted due to timeout' ) {
128
- console . warn ( 'Checking cache timed out' ) ;
129
- pauseCache ( ) ;
130
- return false ;
131
- }
132
- console . error ( 'checkCache error: ' + error . message ) ;
133
- return false ;
134
- }
135
- }
136
-
137
- export default {
138
- get : checkCache ,
139
- put : updateCache
146
+ } ,
140
147
} ;
148
+
149
+ export default cacheMachine ;
0 commit comments