-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
zoic.ts
457 lines (381 loc) · 14.2 KB
/
zoic.ts
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import { decode as base64decode, encode as base64encode } from 'https://deno.land/[email protected]/encoding/base64.ts'
import { Context } from 'https://deno.land/x/[email protected]/mod.ts'
import { connect, Redis } from 'https://deno.land/x/[email protected]/mod.ts'
import { oakCors } from 'https://deno.land/x/[email protected]/mod.ts'
import { options, cacheValue } from './src/types.ts'
import PerfMetrics from './src/performanceMetrics.ts'
import LRU from './src/lru.ts'
import LFU from './src/lfu.ts'
/**
* Class to initalize new instance of cache.
* Takes options to define if cache eviction policy, expiration time for cache itmes, and if response should be returned on cache hit.
*
* ### Example
*
* ```ts
*
* import { Zoic } from "https://deno.land/x/zoic/zoic.ts"
*
* const cache = new Zoic({
* cache: 'LRU',
* expire: '2h, 5m, 3s',
* capacity: 200
* });
*
* router.get('/dbRead', cache.use, controller.dbRead, ctx => {
* ctx.response.body = ctx.state.somethingFromDb;});
*
* ```
*
* ### Wtih Redis
* Note: with Reids options "expire" and "capacity" do not apply.
* ```ts
*
* const cache = new Zoic({
* cache:'Redis',
* port: 6379
* })
*
* ```
*
* @param option (cache options)
* @returns LRU | Redis (new cache)
*/
export class Zoic {
capacity: number;
expire: number;
metrics: PerfMetrics;
respondOnHit: boolean;
cache: Promise < LRU | LFU | Redis >;
constructor (options?: options) {
if (options?.capacity !== undefined && options.capacity <= 0){
throw new Error('Cache capacity must exceed 0 entires.');
}
this.capacity = options?.capacity || Infinity;
this.expire = this.#parseExpTime(options?.expire);
this.metrics = new PerfMetrics();
this.respondOnHit = this.#setRespondOnHit(options);
this.cache = this.#initCacheType(
this.expire,
this.metrics,
options?.cache?.toUpperCase(),
options?.port,
options?.hostname
);
this.use = this.use.bind(this);
this.put = this.put.bind(this);
this.clear = this.clear.bind(this);
this.getMetrics = this.getMetrics.bind(this);
this.endPerformanceMark = this.endPerformanceMark.bind(this);
}
/**
* Sets cache eviction policty. Defaults to LRU.
* @param expire
* @param cache
* @returns LRU | Redis
*/
async #initCacheType (expire: number, metrics: PerfMetrics, cache?: string, redisPort?: number, hostname?: string) {
// The client will enter the specific cache function they want as a string, which is passed as an arg here.
if (!cache || cache === 'LRU'){
this.metrics.cacheType = 'LRU';
return new LRU(expire, metrics, this.capacity);
} else if (cache === 'LFU'){
this.metrics.cacheType = 'LFU';
return new LFU(expire, metrics, this.capacity);
} else if (cache === 'REDIS'){
if (!redisPort) {
throw new Error('Redis requires port number passed in as an options property.');
}
const redis = await connect({
hostname: hostname || '127.0.0.1',
port: redisPort
});
this.metrics.cacheType = 'Redis';
return redis;
}
throw new TypeError('Invalid cache type.');
}
/**
* Parses expire option into time in seconds.
* @param numberString
* @returns number
*/
#parseExpTime (numberString?: string | number | undefined) {
if (numberString === undefined) return Infinity;
let seconds;
if (typeof numberString === 'string'){
seconds = numberString.trim().split(',').reduce((arr, el) => {
if (el[el.length - 1] === 'd') return arr += parseInt(el.slice(0, -1)) * 86400;
if (el[el.length - 1] === 'h') return arr += parseInt(el.slice(0, -1)) * 3600;
if (el[el.length - 1] === 'm') return arr += parseInt(el.slice(0, -1)) * 60;
if (el[el.length - 1] === 's') return arr += parseInt(el.slice(0, -1));
throw new TypeError(
'Cache expiration time must be string formatted as a numerical value followed by \'d\', \'h\', \'m\', or \'s\', or a number representing time in seconds.'
)
}, 0);
} else {
seconds = numberString;
}
if (seconds > 31536000 || seconds <= 0 ) throw new TypeError('Cache expiration time out of range.');
return seconds;
}
/**
* Sets respond on hit value, defaults to true.
* @param options
* @returns
*/
#setRespondOnHit (options?: options) {
if (options?.respondOnHit === undefined) return true;
return options.respondOnHit;
}
/**
* typecheck for Redis cache
* @param cache
* @returns
*/
redisTypeCheck (cache: LRU | LFU | Redis): cache is Redis {
return (cache as Redis).isConnected !== undefined;
}
/**
* Marks end of latency test for cache hit or miss, and updates read or write processed
* @param queryRes
*/
endPerformanceMark (queryRes: 'hit' | 'miss') {
performance.mark('endingMark');
this.metrics.updateLatency(
performance.measure('latency_timer', 'startingMark', 'endingMark').duration,
queryRes
);
queryRes === 'hit'
? this.metrics.readProcessed()
: this.metrics.writeProcessed();
}
/**
* Primary caching middleware method on user end.
* Resposible for querying cache and either returning results to client/attaching results to ctx.state.zoic (depending on user options)
* or, in the case of a miss, signalling to make response cachable.
* @param ctx
* @param next
* @returns Promise | void
*/
async use (ctx: Context, next: () => Promise<unknown>) {
try {
const cache = await this.cache;
//starting mark for cache hit/miss latency performance test.
performance.mark('startingMark');
//defines key via api endpoint
const key: string = ctx.request.url.pathname + ctx.request.url.search;
//query cache
const cacheQueryResults = await cache.get(key);
//check if cache miss
if (!cacheQueryResults) {
// If declared cache size is less than current cache size, we increment the count of entries.
if (this.metrics.numberOfEntries < this.capacity) this.metrics.addEntry();
//makes response cacheable via patch
this.#cacheResponse(ctx);
return next();
}
//if cache is Redis parse base64string, decoding body header and status
if (this.redisTypeCheck(cache) && typeof cacheQueryResults === 'string') {
const parsedResults = cacheQueryResults.split('\n');
const { headers, status } = JSON.parse(atob(parsedResults[0]));
const body = base64decode(parsedResults[1]);
if (this.respondOnHit) {
ctx.response.body = body;
ctx.response.status = status;
Object.keys(headers).forEach(key => {
ctx.response.headers.set(key, headers[key]);
});
this.endPerformanceMark('hit');
return;
}
//attach query results to ctx.state.zoic if not respondOnHit
ctx.state.zoicResponse = {
body: body,
headers: headers,
status: status
};
this.endPerformanceMark('hit');
return next();
}
//if in-memory cache...
if (!this.redisTypeCheck(cache) && typeof cacheQueryResults !== 'string'){
const { body, headers, status } = cacheQueryResults;
if (this.respondOnHit) {
ctx.response.body = body;
ctx.response.status = status;
Object.keys(headers).forEach(key => {
ctx.response.headers.set(key, headers[key]);
});
this.endPerformanceMark('hit');
return;
}
///attach query results to ctx.state.zoic if not respondOnHit
ctx.state.zoicResponse = {
body: JSON.parse(new TextDecoder().decode(body)),
headers: headers,
status: status
};
this.endPerformanceMark('hit');
return next();
}
throw new Error('Cache query failed');
} catch (err) {
ctx.response.status = 400;
ctx.response.body = 'Error in Zoic.use. Check server logs for details.';
console.log(`Error in Zoic.use: ${err}`);
}
}
/**
* Makes response store to cache at the end of middleware chain in the case of a cache miss.
* This is done by patching 'toDomResponse' to send results to cache before returning to client.
* @param ctx
* @returns void
*/
async #cacheResponse (ctx: Context) {
try {
const cache = await this.cache;
const redisTypeCheck = this.redisTypeCheck;
const endPerformanceMark = this.endPerformanceMark;
const toDomResponsePrePatch = ctx.response.toDomResponse;
//patch toDomResponse to cache response body before returning results to client
ctx.response.toDomResponse = async function() {
//defines key via api endpoint and adds response body to cache
const key: string = ctx.request.url.pathname + ctx.request.url.search;
//extract native http response from toDomResponse to get correct headers and readable body
const nativeResponse = await toDomResponsePrePatch.apply(this);
//redis cache stores body as a base64 string encoded from a buffer
if (redisTypeCheck(cache)) {
//make response body string, and then stringify response object for storage in redis
const body = await nativeResponse.clone().arrayBuffer();
const headerAndStatus = {
headers: Object.fromEntries(nativeResponse.headers.entries()),
status: nativeResponse.status
};
cache.set(
key,
`${ btoa(JSON.stringify(headerAndStatus)) }\n${ base64encode(new Uint8Array(body)) }`
);
}
//if in-memory store as native js...
if (!redisTypeCheck(cache)) {
//make response body unit8array and read size for metrics
const arrBuffer = await nativeResponse.clone().arrayBuffer();
const responseToCache: cacheValue = {
body: new Uint8Array(arrBuffer),
headers: Object.fromEntries(nativeResponse.headers.entries()),
status: nativeResponse.status
};
//count bytes for perf metrics
const headerBytes = Object.entries(responseToCache.headers)
.reduce((acc: number, headerArr: Array<string>) => {
return acc += (headerArr[0].length * 2) + (headerArr[1].length * 2);
}, 0);
//34 represents size of obj keys + status code.
const resByteLength = (key.length * 2) + responseToCache.body.byteLength + headerBytes + 34;
cache.put(key, responseToCache, resByteLength);
}
//ending mark for a cache miss latency performance test.
endPerformanceMark('miss');
return new Promise (resolve => {
resolve(nativeResponse);
});
}
return;
} catch (err) {
ctx.response.status = 400;
ctx.response.body = 'Error in Zoic.#cacheResponse. Check server logs for details.';
console.log(`Error in Zoic.#cacheResponse: ${err}`);
}
}
/**
* Manually clears all current cache entries.
*/
async clear (ctx: Context, next: () => Promise<unknown>) {
try {
const cache = await this.cache;
this.redisTypeCheck(cache)
? cache.flushdb()
: cache.clear();
this.metrics.clearEntires();
return next();
} catch (err) {
ctx.response.status = 400;
ctx.response.body = 'Error in Zoic.clear. Check server logs for details.';
console.log(`Error in Zoic.clear: ${err}`);
}
}
/**
* Retrives cache metrics. Designed for use with Chrome extension.
* @param ctx
*/
getMetrics (ctx: Context) {
try {
//wrap functionality of sending metrics inside of oakCors to enable route specific cors by passing in as 'next'.
const enableRouteCors = oakCors();
return enableRouteCors(ctx, async () => {
const cache = await this.cache;
const {
cacheType,
memoryUsed,
numberOfEntries,
readsProcessed,
writesProcessed,
missLatencyTotal,
hitLatencyTotal
} = this.metrics;
ctx.response.headers.set('Access-Control-Allow-Origin', '*');
//fetch stats from redis client if needed.
if (this.redisTypeCheck(cache)) {
const redisInfo = await cache.info();
const redisSize = await cache.dbsize();
const infoArr: string[] = redisInfo.split('\r\n');
ctx.response.body = {
cache_type: cacheType,
number_of_entries: redisSize,
memory_used: infoArr?.find((line: string) => line.match(/used_memory/))?.split(':')[1],
reads_processed: infoArr?.find((line: string) => line.match(/keyspace_hits/))?.split(':')[1],
writes_processed: infoArr?.find((line: string) => line.match(/keyspace_misses/))?.split(':')[1],
average_hit_latency: hitLatencyTotal / readsProcessed,
average_miss_latency: missLatencyTotal / writesProcessed
}
return;
}
//set in-memory stats
ctx.response.body = {
cache_type: cacheType,
memory_used: memoryUsed,
number_of_entries: numberOfEntries,
reads_processed: readsProcessed,
writes_processed: writesProcessed,
average_hit_latency: hitLatencyTotal / readsProcessed,
average_miss_latency: missLatencyTotal / writesProcessed
}
return;
})
} catch (err) {
ctx.response.status = 400;
ctx.response.body = 'Error in Zoic.getMetrics. Check server logs for details.';
console.log(`Error in Zoic.getMetrics: ${err}`);
}
}
/**
* Manually sets response to cache.
* @param ctx
* @param next
* @returns
*/
put (ctx: Context, next: () => Promise<unknown>) {
try {
performance.mark('startingMark');
if (this.metrics.numberOfEntries < this.capacity) this.metrics.addEntry();
this.#cacheResponse(ctx);
return next();
} catch (err) {
ctx.response.status = 400;
ctx.response.body = 'Error in Zoic.put. Check server logs for details.';
console.log(`Error in Zoic.put: ${err}`);
}
}
}
export default Zoic;