forked from n3t1ab7/mysql-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·654 lines (547 loc) · 17.8 KB
/
app.js
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
'use strict'
// Take a moment to rejoice the fact how this code looks now versus this:
// https://github.com/michaeldegroot/mysql-cache/blob/4739b184c5d397e901775cbf68f938168af8dadc/app.js
// Requires
const colors = require('colors')
const crypto = require('crypto')
const events = require('events')
const extend = require('extend')
const Promise = require('bluebird')
const CacheProvider = require('./lib/cacheProvider')
const moment = require('moment')
const checkNested = require('check-nested')
const install = require('install-package')
// Some constants
const poolPrefix = colors.cyan('Pool')
class MysqlCache {
constructor(config) {
// Promisify all functions, they can now be accessed by FunctionNameHereAsync()
Promise.promisifyAll(this, {
multiArgs: true,
})
// Merge default settings with user settings
this.config = extend({
TTL: 0,
verbose: false,
caching: true,
cacheProvider: 'lru',
connectionLimit: 1000,
supportBigNumbers: true,
hashing: 'farmhash64',
prettyError: true,
cacheProviderSetup: {
// Default memcached setting
serverLocation: '127.0.0.1:11211',
},
}, config)
// Setup class properties
this.event = new events.EventEmitter()
this.mysql = require('mysql2')
this.hits = 0
this.misses = 0
this.queries = 0
this.inserts = 0
this.poolConnections = 0
this.deletes = 0
this.selects = 0
this.updates = 0
this.cacheProvider = new CacheProvider(this, this.config)
this.cacheProviders = this.cacheProvider.getAll()
}
/**
* Prints text
*/
trace(text) {
if (this.config.hasOwnProperty('verbose')) {
if (this.config.verbose) {
console.log(colors.bold('MYSQL-CACHE') + ': ' + text)
return true
}
return false
}
}
/**
* Connect to the database
*/
connect(cb) {
// Create a mysql connection pool with the configured
this.pool = this.mysql.createPool(this.config)
// Create the connection
this.pool.getConnection((err, connection) => {
if (err) {
return cb(err)
}
this.trace(`${colors.bold(colors.green('Connected'))} to ${colors.bold('MySQL')} as ${colors.bold(this.config.user)}@${colors.bold(this.config.host)} with the ${colors.bold(this.config.cacheProvider)} cacheProvider`)
if (this.endPool(connection)) {
// Verbose output pool events of the mysql package
this.pool.on('acquire', connection => {
this.poolConnections = this.pool._allConnections.length
this.trace(`${poolPrefix}: recieved connection with id ${connection.threadId}`)
})
this.pool.on('connection', connection => {
this.poolConnections = this.pool._allConnections.length
this.trace(`${poolPrefix}: Connection established with id ${connection.threadId}`)
})
this.pool.on('enqueue', () => {
this.poolConnections = this.pool._allConnections.length
this.trace('${poolPrefix}: Waiting for available connection slot')
})
this.pool.on('release', connection => {
this.poolConnections = this.pool._allConnections.length
this.trace(`${poolPrefix}: Connection ${connection.threadId} released`)
})
this.event.emit('connected')
cb(null, true)
}
})
}
/**
* Disconnects from the database by killing the pool
*/
disconnect(cb) {
this.killPool(err => {
if (err) {
return cb(err)
}
this.trace(`${colors.bold(colors.red('Disconnected'))} from ${colors.bold('MySQL')}`)
return cb(null, true)
})
}
/**
* A placeholder callback for undefined callbacks
*/
defineCallback(cb) {
if (!cb) {
return () => {
return 'woot'
}
}
return cb
}
/**
* Uses or installs a package
*/
usePackage(moduleName, cb) {
try {
require.resolve(moduleName)
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
this.trace(`Module ${moduleName} not installed, installing...`)
return install(moduleName).then(result => {
cb(result.stderr, require(moduleName))
}).catch(e => {
cb(e, null)
})
} else {
throw new Error(e)
}
}
return cb(null, require(moduleName))
}
/**
* Flushes all cache
*/
flush(cb) {
cb = this.defineCallback(cb)
return this.cacheProvider.run({
action: 'flush',
}, err => {
if (err) {
return cb(err)
}
this.event.emit('flush')
this.trace('Cache Flushed')
return cb(null, true)
})
}
flushAll(cb) {
return this.flush(this.defineCallback(cb))
}
/**
* Returns some statistics about mysql-cache
*/
stats(object) {
if (object) {
return {
poolConnections: this.pool._allConnections.length,
hits: this.hits,
misses: this.misses,
total: this.queries,
selects: this.selects,
inserts: this.inserts,
updates: this.updates,
deletes: this.deletes,
}
} else {
this.trace('Open Pool Connections: ' + this.pool._allConnections.length)
this.trace('Cache Hits: ' + this.hits)
this.trace('Cache Misses: ' + this.misses)
this.trace('Total Queries: ' + this.queries)
this.trace('Total Select Statements: ' + this.selects)
this.trace('Total Insert Statements: ' + this.inserts)
this.trace('Total Update Statements: ' + this.updates)
this.trace('Total Remove Statements: ' + this.deletes)
}
}
/**
* A Request to query the database comes in
* @param {Object} sql
* @param {Object} params
* @param {Function} cb
* @param {Object} data
*/
query(sql, params, cb, data) {
this.queries++
let query
let zone = 'DEFAULT_MYSQL_CACHE_ZONE'
if (typeof params === 'function') {
data = cb
cb = params
params = []
query = sql
} else {
query = sql
}
if (typeof sql === 'object') {
query = sql.sql
params = []
if (sql.hasOwnProperty('params')) {
params = sql.params
}
}
if (sql.hasOwnProperty('zone')) {
zone = sql.zone
}
if (data) {
if (data.hasOwnProperty('zone')) {
zone = data.zone
}
}
// A query can be called without a callback
cb = this.defineCallback(cb)
// Determine the query type: SELECT, UPDATE, etc
const type = query.split(/\s/g)[0].toLowerCase()
// Let the mysql package forumulate the query object and send a event emit
query = this.mysql.format(query, params)
this.event.emit('query', query)
// Find out what to do next with this query
// This parameter object is used by multiple functions including: miss, hit and dbQuery
return this.defineCache({
sql,
query,
type,
params,
data,
zone,
}, cb)
}
/**
* Defines a cache object
*/
defineCache(obj, cb) {
this.trace(colors.bold(obj.type.toUpperCase()) + ' ' + colors.grey(colors.bold(obj.query)))
if (obj.type === 'insert') {
this.inserts++
}
if (obj.type === 'update') {
this.updates++
}
if (obj.type === 'delete') {
this.deletes++
}
if (obj.type === 'select') {
return this.createHash(obj.query, (err, hash) => {
if (err) {
return cb(err)
}
// Updates the hash property of this mysql cache object
obj.hash = hash
this.selects++
// Retrieve the cache key
return this.getKey(hash, (err, result) => {
if (err) {
return cb(err)
}
// Set the cache result
obj.result = result
// If there is a cacheProvider result we should return the cache HIT
if (obj.result) {
// Can refresh the cache object if specified
if (obj.sql.hasOwnProperty('refreshCache')) {
if (obj.sql.refreshCache === true) {
return this.miss(obj, cb)
}
}
return this.hit(obj, cb)
}
// Make the cache query MISS
return this.miss(obj, cb)
})
})
}
return this.dbQuery(obj, (err, result) => {
if (err) {
return cb(err)
}
// Set the database result
obj.result = result
// This is not cached
obj.isCache = false
return cb(null, this.mysqlObject(obj).result, this.mysqlObject(obj).cache)
})
}
/**
* Hits a query call
*/
hit(obj, cb) {
obj.isCache = true
this.event.emit('hit', obj)
this.trace(colors.yellow(obj.hash.slice(0, 15)) + ' ' + colors.green(colors.bold('HIT')))
this.hits++
return cb(null, this.mysqlObject(obj).result, this.mysqlObject(obj).cache)
}
/**
* Misses a query call
*/
miss(obj, cb) {
obj.isCache = false
let TTLSet = 0
this.trace(colors.yellow(obj.hash.slice(0, 15)) + ' ' + colors.red(colors.bold('MISS')))
this.misses++
// Will this query be cached?
let doCache = true
if (obj.sql.hasOwnProperty('cache')) {
if (obj.sql.cache === false) {
doCache = false
}
}
return this.dbQuery(obj, (err, result) => {
if (err) {
return cb(err)
}
// Sets the cache result
obj.result = result
this.event.emit('miss', obj, result)
// Figure out some parameters configurations for this query
TTLSet = this.config.TTL * 1000
if (obj.data) {
if (obj.data.hasOwnProperty('TTL')) {
TTLSet = obj.data.TTL * 1000
}
if (obj.data.hasOwnProperty('cache')) {
if (obj.data.cache === false) {
doCache = false
}
}
}
// If we don't want to cache then just return the result without creating a cache key
if (!this.config.caching || !doCache) {
return cb(null, this.mysqlObject(obj).result, this.mysqlObject(obj).cache)
}
// Create a cache key for future references
return this.createKey(obj.hash, result, TTLSet, obj.zone, (err, keyResult) => {
if (err) {
return cb(err)
}
return cb(null, this.mysqlObject(obj).result, this.mysqlObject(obj).cache)
})
})
}
/**
* Generates a object that mysqlCache exposes after a .query cb
*/
mysqlObject(obj) {
let newObj = {
cache: {},
}
if (checkNested(obj, 'cache.created') === false) {
newObj.cache.created = moment().unix()
}
newObj.result = obj.result
newObj.cache = {
isCache: obj.isCache,
hash: obj.hash,
sql: obj.query,
}
return newObj
}
/**
* Handles pool connection and queries the database
*/
dbQuery(obj, cb) {
return this.getPool((err, connection) => {
if (err) {
return cb(err)
}
this.event.emit('dbQuery', obj)
connection.query(obj.sql, obj.params, (err, rows) => {
if (err) {
return cb(err)
}
this.endPool(connection)
return cb(null, rows)
})
})
}
/**
* How a hash id is created from scratch
* @param {String} id
*/
createHash(id, cb) {
id = String(id)
id = id.replace(/ /g, '')
id = id.toLowerCase()
let hash = null
if (this.config.hashing === 'xxhash') {
return this.usePackage('xxhash', (err, module) => {
if (err) {
this.trace(`Warning when installing module 'xxhash': ${err}`)
}
return cb(null, module.hash(new Buffer(id), 0xCAFEBABE).toString())
})
}
if (this.config.hashing === 'farmhash32') {
return this.usePackage('farmhash', (err, module) => {
if (err) {
this.trace(`Warning when installing module 'farmhash': ${err}`)
}
return cb(null, module.hash32(id).toString())
})
}
if (this.config.hashing === 'farmhash64') {
return this.usePackage('farmhash', (err, module) => {
if (err) {
this.trace(`Warning when installing module 'farmhash': ${err}`)
}
return cb(null, module.hash64(id).toString())
})
}
try {
hash = crypto.createHash(this.config.hashing).update(id).digest('hex')
} catch (error) {
return cb('Undefined hash method: ' + this.config.hashing)
}
cb(null, hash)
}
/**
* Deletes a cache object by key
* @param {Object} id
* @param {Object} params
*/
delKey(id, params, cb) {
if (typeof params === 'function' && typeof id === 'object') {
cb = params
}
cb = this.defineCallback(cb)
if (typeof id === 'object') {
params = id['params']
id = id['sql']
}
return this.createHash(this.mysql.format(id, params), (err, hash) => {
if (err) {
return cb(err)
}
this.event.emit('delete', hash)
return this.cacheProvider.run({
action: 'remove',
hash,
}, (err, result) => {
cb(err, result)
})
})
}
/**
* Retrieves a cache object by key
* @param {String} hash
* @param {Function} cb
*/
getKey(hash, cb) {
this.event.emit('get', hash)
return this.cacheProvider.run({
action: 'get',
hash,
}, (err, result) => {
return cb(err, result)
})
}
/**
* Creates a cache object
* @param {String} hash
* @param {Object} val
* @param {Number} ttl
* @param {Function} cb
*/
createKey(hash, val, ttl, zone, cb) {
this.event.emit('create', hash, val, ttl)
return this.cacheProvider.run({
action: 'set',
hash,
val,
ttl,
zone,
}, (err, result) => {
return cb(err, result)
})
}
/**
* Clears a cache zone
* @param {String} Zone name
* @param {Function} cb
*/
clearZone(zone, cb) {
return this.cacheProvider.run({
action: 'clearzone',
zone,
}, err => {
return cb(err)
})
}
/**
* Create or get a pool connection
* @param {Function} cb
*/
getPool(cb) {
return this.pool.getConnection((err, connection) => {
if (err) {
return cb(err)
}
this.event.emit('getPool', connection)
this.poolConnections = this.pool._allConnections.length
return cb(null, connection)
})
}
/**
* Kill a pool connection
* @param {Object} connection
*/
endPool(connection) {
if (connection) {
connection.release()
this.event.emit('endPool', connection)
this.poolConnections = this.pool._allConnections.length
return true
}
return false
}
/**
* Kills the pool
* @param {Function} cb
*/
killPool(cb) {
cb = this.defineCallback(cb)
return this.pool.getConnection((err, connection) => {
if (err) {
return cb(err)
}
return this.pool.end(err => {
if (err) {
return cb(err)
}
this.trace(`${poolPrefix}: Pool is killed`)
this.event.emit('killPool')
return cb(null, true)
})
})
}
}
module.exports = MysqlCache