-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_handler.js
More file actions
386 lines (370 loc) · 11 KB
/
Copy pathcache_handler.js
File metadata and controls
386 lines (370 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Cache Handler for Node.js applications using Application Container Cloud Service
* By: Callan Howell-Pavia (callan.howell-pavia@oracle.com)
*
* This implementation is designed to provide the a simple caching interface, in
* typical node style, using callbacks.
* Internally the cache uses byte arrays (octet streams), so focus is upon coercing
* the cached objects into appropriate formats.
*/
var request = require('./util/request-with-backoff.js');
//var request = require('request');
var MockCache = require('./mock_cache_handler.js');
var multivalue = require("./util/multivalue");
const WARN_NO_CACHE_HOST = "Internal Caching URL is not set. Falling back on using a local hashmap instead.";
const WARN_NO_CACHE_BINDING = "If this application is running on ACCS, ensure that you have correctly bound to a caching service.";
//Retrieve the cache url set by ACCS
var CCSHOST = process.env.CACHING_INTERNAL_CACHE_URL;
function Cache(cacheName){
//Fallback on Mock cache if it doesn't look like we are on ACCS
if(!process.env.CACHING_INTERNAL_CACHE_URL){
console.warn(WARN_NO_CACHE_HOST);
//HOSTNAME and PORT are set by default on ACCS, so we will use them to make a guess about whether
//the binding is simply not set.
if(process.env.HOSTNAME && process.env.PORT){
console.warn(WARN_NO_CACHE_BINDING);
}
return new MockCache(cacheName);
}
this._cacheName = encodeURIComponent(cacheName);
this._cacheURL = "http://" +CCSHOST +":8080/ccs/" +this._cacheName;
}
//Retreive an entry from the cache
Cache.prototype.get = function(key, objType, callback){
if(key == undefined || typeof key == 'function'){
throw new SyntaxError("key is a required parameter for get!");
}
if(typeof objType == 'function'){
callback = objType;
objType = null;
}
if(typeof key == 'number'){
key = String(key);
}
if(typeof key != 'string'){
throw new TypeError("Caching keys must be strings or numbers!");
}
if(objType && typeof objType != 'string'){
throw new TypeError("Object hints for getting a cache entry must be a valid javascript object type, e.g. 'string'");
}
//Retrieve the entry from the cache
var options = { url: this._cacheURL +"/" +encodeURIComponent(key),
method: 'GET'
};
request(options, function(err, response, body){
if(response && response.statusCode == 404){
//respond with a null object
callback(null, null);
return;
}
if(err){
callback(err);
return;
}
//Coerce the response into appropriate format
try{
var ret = _coerceObjectType(body, objType);
}catch(ex){
callback(ex, null);
return;
}
callback(null, ret);
});
}
//Add an entry to the cache
Cache.prototype.put = function(key, val, ttl, isBuffer, callback) {
if(key == undefined || val == undefined || typeof key == 'function' || typeof val == 'function'){
throw new SyntaxError("key and val are required parameters for put!");
}
if(typeof ttl == 'function'){
callback = ttl;
ttl = null;
isBuffer = false;
}
if(typeof ttl == 'boolean'){
if(typeof isBuffer == 'function'){
callback = isBuffer;
}
isBuffer = ttl;
ttl = null;
}
if(typeof isBuffer == 'function'){
callback = isBuffer;
isBuffer = false;
}
if(typeof key == 'number'){
key = String(key);
}
if(typeof key != 'string'){
throw new TypeError("Caching keys must be strings or numbers!");
}
if(ttl && typeof ttl != 'number'){
throw new TypeError("Time to live for cache entries must a positive number!");
}
//Assemble the call to add the entry to the cache
var options = { url: this._cacheURL +"/" +encodeURIComponent(key),
method: 'PUT',
headers: {
'Content-Type': 'application/octet-stream'
}
};
if(ttl && ttl > 0){
options.qs = {'ttl':ttl};
}
//Serialize the body if appropriate
if(typeof val == 'string' || Buffer.isBuffer(val)){
options.body = val;
}else{
options.body = JSON.stringify(val);
}
request(options, function(err, response, body){
if(err){
callback(err);
return;
}
callback(null);
});
};
//Add an entry to the cache if the key is available, otherwise reject.
Cache.prototype.putIfAbsent = function(key, val, ttl, isBuffer, callback) {
if(key == undefined || val == undefined || typeof key == 'function' || typeof val == 'function'){
throw new SyntaxError("key and val are required parameters for putIfAbsent!");
}
if(typeof ttl == 'function'){
callback = ttl;
ttl = null;
isBuffer = false;
}
if(typeof ttl == 'boolean'){
if(typeof isBuffer == 'function'){
callback = isBuffer;
}
isBuffer = ttl;
ttl = null;
}
if(typeof isBuffer == 'function'){
callback = isBuffer;
isBuffer = false;
}
if(typeof key == 'number'){
key = String(key);
}
if(typeof key != 'string'){
throw new TypeError("Caching keys must be strings or numbers!");
}
if(ttl && typeof ttl != 'number'){
throw new TypeError("Time to live for cache entries must a positive number!");
}
//Assemble the call to add the entry to the cache
//Return old is required, as in my testing
var options = { url: this._cacheURL +"/" +encodeURIComponent(key),
method: 'POST',
headers: {
'Content-Type': 'application/octet-stream',
'X-Method': 'putIfAbsent'
},
qs: {
'returnOld':'true'
}
};
if(ttl && ttl > 0){
options.qs.ttl = ttl;
}
//Serialize the body if appropriate
if(typeof val == 'string' || Buffer.isBuffer(val)){
options.body = val;
}else{
options.body = JSON.stringify(val);
}
request(options, function(err, response, body){
if(response && response.statusCode == 409){
callback(new Error('Did not insert entry, key already exists.'));
return;
}
if(err){
callback(err);
return;
}
callback(null);
});
};
Cache.prototype.replace = function(key, val, oldVal, ttl, callback) {
if(key == undefined || val == undefined || oldVal == undefined
|| typeof key == 'function' || typeof val == 'function' || typeof oldVal == 'function'){
throw new SyntaxError("key, val and oldVal are required parameters for replace!");
}
if(typeof ttl == 'function'){
callback = ttl;
ttl = null;
}
if(typeof key == 'number'){
key = String(key);
}
if(typeof key != 'string'){
throw new TypeError("Caching keys must be strings or numbers!");
}
if(ttl && typeof ttl != 'number'){
throw new TypeError("Time to live for cache entries must a positive number!");
}
//Assemble the call to add the entry to the cache
var options = { url: this._cacheURL +"/" +encodeURIComponent(key),
method: 'POST',
headers: {
'Content-Type': 'application/x-multivalue-octet-stream',
'X-Method': 'replaceValue'
}
};
if(ttl && ttl > 0){
options.qs = {'ttl':ttl};
}
//Assemble the required multi-value to pass
var mvBody = [];
//Serialize the body if appropriate
if(typeof oldVal == 'string' || Buffer.isBuffer(oldVal)){
mvBody.push(oldVal);
}else{
mvBody.push(JSON.stringify(oldVal));
}
if(typeof val == 'string' || Buffer.isBuffer(val)){
mvBody.push(val);
}else{
mvBody.push(JSON.stringify(val));
}
options.body = multivalue.encode(mvBody);
request(options, function(err, response, body){
if(response && response.statusCode == 409){
callback(new Error('Did not insert entry, cached value does not equal oldVal supplied.'));
return;
}
if(err){
callback(err);
return;
}
callback(null);
});
};
//Delete an entry from the cache
Cache.prototype.delete = function(key, callback) {
if(key == undefined || typeof key == 'function'){
throw new SyntaxError("key is a required parameter for delete!");
}
if(typeof key == 'number'){
key = String(key);
}
if(typeof key != 'string'){
throw new TypeError("Caching keys must be strings or numbers!");
}
//Assemble the call to delete the entry from the cache
var options = { url: this._cacheURL +"/" +encodeURIComponent(key),
method: 'DELETE'
};
request(options, function(err, response, body){
if(err){
callback(err);
return;
}
callback(null);
});
};
//Clear the whole cache
Cache.prototype.clear = function(callback) {
var options = { url: this._cacheURL,
method: 'DELETE'
};
request(options, function(err, response, body){
if(err){
callback(err);
return;
}
callback(null);
});
};
/*Get the cache stats
*Response object has the following fields:
*'cache' - cache name
*'count' - number of objects in the cache
*'size' - size of the cache (in bytes?)
*/
Cache.prototype.stats = function(callback){
var options = { url: this._cacheURL,
method: 'GET'
};
var self = this;
request(options, function(err, response, body){
if(err){
callback(err);
return;
}
var bodyJSON = JSON.parse(body);
var cacheInfo = { cache: self._cacheName,
count: bodyJSON.count,
size: bodyJSON.size
};
callback(null, cacheInfo);
});
};
/*
* Internal method for coercing the response back into the expected format.
* Takes an 'object hint' which defines the expected response format,
* but if one is not provided, makes a best-guess, based upon what the response
* looks like.
*/
function _coerceObjectType(obj, objHint){
if(objHint){
switch(objHint){
case 'string':
//Response that comes back is application/octet-stream, which we will return raw
return obj;
case 'buffer':
return Buffer.from(obj, 'utf8');
case 'number':
var num = Number(obj);
if(isNaN(num)){
throw new TypeError("'number was requested as a type, but result is NaN!");
}
return num;
case 'array':
try{
var pObj = JSON.parse(obj);
if(pObj[0] == undefined){
//Is an object, not an array, so we will construct an array of just this object
return new Array(pObj);
}
return pObj;
}catch(ex){
//If it fails, then it is likely a single string, lets coerce it to an array
return new Array(obj);
}
case 'boolean':
//Today's javascript fun-fact: 'false' is truthy!
if(obj == 'false'){
return false;
}
return Boolean(obj);
case 'object':
try{
var pObj = JSON.parse(obj);
return pObj;
}catch(ex){
//If it fails, then just throw back the object
return obj;
}
break;
}
}else{
return _bestGuessObject(obj);
}
}
function _bestGuessObject(obj){
//JSON.parse is a very powerful function...
try{
var pObj = JSON.parse(obj);
return pObj;
}catch(ex){
//If parsing as a JSON fails, then the object is normally a string or possibly some horrible blob object
//either way, the caller should work out what to do with it
return obj;
}
}
module.exports = Cache;
module.exports.MockCache = MockCache;