-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
129 lines (104 loc) · 3.55 KB
/
index.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
/**
* Index File for @truesparrow/cache
*/
const Base = require('@truesparrow/base');
const rootPrefix = '.',
version = require(rootPrefix + '/package.json').version,
coreConstant = require(rootPrefix + '/config/coreConstant');
const InstanceComposer = Base.InstanceComposer;
require(rootPrefix + '/services/CacheInstance');
const Cache = function(configStrategy) {
const oThis = this;
if (!configStrategy) {
throw 'Mandatory argument configStrategy missing';
}
const instanceComposer = new InstanceComposer(configStrategy);
oThis.ic = function() {
return instanceComposer;
};
};
Cache.prototype = {
version: version
};
Object.defineProperty(Cache.prototype, 'cacheInstance', {
get: function() {
const oThis = this;
return oThis.ic().getInstanceFor(coreConstant.icNameSpace, 'getCacheInstance');
}
});
const instanceMap = {};
/**
* Creates the key for the instanceMap.
*
* @returns {string}
*
*/
const getInstanceKey = function(configStrategy) {
if (!configStrategy.hasOwnProperty('engine')) {
throw 'CACHING_ENGINE parameter is missing.';
}
if (configStrategy.engine === undefined) {
throw 'CACHING_ENGINE parameter is empty.';
}
// Grab the required details from the configStrategy.
const cacheEngine = configStrategy.engine.toString();
let isConsistentBehaviour = configStrategy.consistentBehavior;
// Sanitize isConsistentBehaviour
isConsistentBehaviour = isConsistentBehaviour === undefined ? true : isConsistentBehaviour != '0';
// Stores the endpoint for key generation of instanceMap.
let endpointDetails = null;
// Generate endpointDetails for key generation of instanceMap.
if (cacheEngine == 'redis') {
const redisMandatoryParams = ['host', 'port', 'password', 'enableTsl'];
// Check if all the mandatory connection parameters for Redis are available or not.
for (let i = 0; i < redisMandatoryParams.length; i++) {
if (!configStrategy.hasOwnProperty(redisMandatoryParams[i])) {
throw 'Redis - mandatory connection parameters missing.';
}
if (configStrategy[redisMandatoryParams[i]] === undefined) {
throw 'Redis - connection parameters are empty.';
}
}
endpointDetails =
configStrategy.host.toLowerCase() +
'-' +
configStrategy.port.toString() +
'-' +
configStrategy.enableTsl.toString();
} else if (cacheEngine == 'memcached') {
if (!configStrategy.hasOwnProperty('servers')) {
throw 'Memcached - mandatory connection parameters missing.';
}
if (configStrategy.servers === undefined) {
throw 'MEMCACHE_SERVERS(configStrategy.servers) parameter is empty. ';
}
endpointDetails = configStrategy.servers.join(',').toLowerCase();
} else {
endpointDetails = configStrategy.namespace || '';
}
return cacheEngine + '-' + isConsistentBehaviour.toString() + '-' + endpointDetails;
};
let Factory = function() {};
Factory.prototype = {
/**
* Fetches a cache instance if available in instanceMap. If instance is not available in
* instanceMap, it calls createCacheInstance() to create a new cache instance.
*
* @returns {cacheInstance}
*
*/
getInstance: function(configStrategy) {
let instanceKey = getInstanceKey(configStrategy);
let _instance = instanceMap[instanceKey];
if (!_instance) {
_instance = new Cache(configStrategy);
instanceMap[instanceKey] = _instance;
}
return _instance;
}
};
const factory = new Factory();
Cache.getInstance = function() {
return factory.getInstance.apply(factory, arguments);
};
module.exports = Cache;