-
Notifications
You must be signed in to change notification settings - Fork 6
/
cacheCollection.js
110 lines (101 loc) · 2.79 KB
/
cacheCollection.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
/**
* cacheCollection constructor
* @constructor
* @param settings: {
* nameSpace: {string | ''},
* verbose: {boolean | false}
* }
* @param cacheModules: [
* {cache module object}
* ]
*/
function cacheCollection(settings, cacheModules){
var self = this;
/**
* Initialize cacheCollection given the provided constructor params
*/
function init(){
self.caches = [];
if(isEmpty(cacheModules)){
log(false, 'No cacheModules array provided--using the default configuration.');
getDefaultConfiguration();
}
else{
log(false, 'cacheModules array provided--using a custom configuration.');
getCustomConfiguration();
if(self.caches.length < 1){
throw new Exception('NoCacheException', 'No caches were succesfully initialized.');
}
}
}
/**
* Adds a nodeCacheModule instance to self.caches if no cacheModules array is provided
*/
function getDefaultConfiguration(){
var CModule = require('cache-service-cache-module');
var cacheModule = new CModule();
cacheModule = addSettings(cacheModule);
self.caches.push(cacheModule);
}
/**
* Adds caches that are not 'empty' from the cacheModules array to self.caches
*/
function getCustomConfiguration(){
for(var i = 0; i < cacheModules.length; i++){
var cacheModule = cacheModules[i];
if(isEmpty(cacheModule)){
log(true, 'Cache module at index ' + i + ' is \'empty\'.');
continue;
}
cacheModule = addSettings(cacheModule);
self.caches.push(cacheModule);
}
}
/**
* Adds cache-service config properties to each cache module
* @param {object} cacheModule
* @return {object} cacheModule
*/
function addSettings(cacheModule){
cacheModule.nameSpace = settings.nameSpace;
cacheModule.verbose = settings.verbose;
return cacheModule;
}
/**
* Checks if a value is "empty"
* @param {object | string | null | undefined} val
* @return {boolean}
*/
function isEmpty (val) {
return (val === undefined || val === false || val === null ||
(typeof val === 'object' && Object.keys(val).length === 0));
}
/**
* Instantates an exception to be thrown
* @param {string} name
* @param {string} message
* @return {exception}
*/
function Exception(name, message){
this.name = name;
this.message = message;
}
/**
* Logging utility function
* @param {boolean} isError
* @param {string} message
* @param {object} data
*/
function log(isError, message, data){
var indentifier = 'cacheService: ';
if(self.verbose || isError){
if(data) {
console.log(indentifier + message, data);
} else {
console.log(indentifier + message);
}
}
}
init();
}
module.exports = cacheCollection;