This repository has been archived by the owner on Jun 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
221 lines (176 loc) · 5.28 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
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
// ## dependencies
var log = require('debug')('lean-cache');
var events = require('events');
var validator = require('./lib/validator');
var Cleaner = require('./lib/cleaner');
// ## constants
var DEFAULT_OPTIONS = {
size: 100000, // 100k records max
ttl: (60 * 60), // 1 hour
interval: 600, // 10 minutes
strategy: 'fifo', // First in first out
load: function(id, callback){ // Where to get missing data
return callback('Not found - ' + id, null);
},
storage: 'memory' // alternative storage
};
// ## export
module.exports = function(argumentOptions){
// initialize
var defaultOpts = Object.assign({}, DEFAULT_OPTIONS);
var optionsToUse = null;
if( !argumentOptions ){
// just use the defaults
optionsToUse = defaultOpts;
log('init - opts - defaults...');
}
else{
log('init - opts - arg = %j', argumentOptions);
var validationMessage = validator.validateInputConstructor(argumentOptions);
if(validationMessage){
log('init - opts - validator = %j', validationMessage);
throw validationMessage;
}
optionsToUse = {};
for(var p in defaultOpts){
optionsToUse[p] = (argumentOptions[p]) ? argumentOptions[p] : defaultOpts[p];
}
if( argumentOptions.storage === 'node-cache' && !argumentOptions.strategy ){
optionsToUse.strategy = 'none';
}
}
log('init - opts - optionsToUse = %j', optionsToUse);
var _this = this;
this.statsHolder = {};
var StorageClass = null;
var StrategyClass = null;
var storage = null;
var emitter = new events.EventEmitter();
log('init - storage =', optionsToUse.storage );
StorageClass = require('./lib/storages/' + optionsToUse.storage);
storage = new StorageClass(optionsToUse.size, optionsToUse.ttl, optionsToUse.interval);
log('init - strategy =', optionsToUse.strategy, '| type =', typeof(optionsToUse.strategy) );
if( typeof(optionsToUse.strategy) === 'string' ){
var strategyNameValidate = validator.validateStrategyName(optionsToUse.strategy);
if( strategyNameValidate ){
log('init - strategy - validation failed = %j', strategyNameValidate );
throw strategyNameValidate;
}
var strategyFile = './lib/strategies/' + optionsToUse.strategy;
StrategyClass = require( strategyFile );
}
else {
var strategyFuncValidate = validator.validateStrategyFunc(optionsToUse.strategy);
if( strategyFuncValidate ){
log('init - strategy - validation failed = %j', strategyFuncValidate );
throw strategyFuncValidate;
}
StrategyClass = optionsToUse.strategy;
}
var strategyInstance = new StrategyClass(optionsToUse, storage);
var cleanRunner = new Cleaner(
optionsToUse.ttl, optionsToUse.interval, storage, strategyInstance, this.statsHolder, emitter
);
emitter.on('expiery', function(obj){
var cnt = _this.count();
log('on - expiery - key = %s | cnt = %s', obj.key, cnt);
if( cnt === 0 ){
cleanRunner.stop();
}
});
// ### methods
this.count = function(){
return storage.count;
};
this.tail = function(){
return storage.tail();
};
this.head = function(){
return storage.head();
};
this.keys = function(){
return storage.keys();
};
this.elements = function(){
return storage.elements();
};
this.set = function(key, value){
log('set - key =', key);
var obj = {
key: key,
added: new Date(),
useCount: 0,
value: value
};
var result = strategyInstance.set(key, obj);
cleanRunner.start();
return result;
};
this.getAsync = function(id, callback){
log('getAsync - key =', id);
var val = strategyInstance.get(id);
log('getAsync - found = %j', val);
if( val ){
return callback(null, val.value);
}
else if( !optionsToUse.load ){
return callback('load function undefined', null);
}
else {
//Load from remote
return optionsToUse.load(id, function(err, result){
log('getAsync - load.callback - key = %s | err = %j | result = %j', id, err, result);
if(err){
return callback(err, null);
}
else {
var set_ok = _this.set(id, result);
log('getAsync - load.callback - key =', id, '| set_ok =', set_ok);
return callback(null, result);
}
});
}
};
this.getPromise = function(id){
return new Promise(function(resolve, reject){
_this.getAsync(id, function(err, body){
if(err){
return reject(err);
}
return resolve(body);
});
});
};
this.get = function(id, callback){
if( typeof(callback) !== 'function' ){
console.error('lean-cache >> error - no callback provided - function get(id, callback){}');
callback = function(){};
}
return this.getAsync(id, callback);
};
this.del = function(id){
return storage.removeByKey(id);
};
this.stats = function(){
var statsObj = {
count: storage.count,
strategy: optionsToUse.strategy,
storage: optionsToUse.storage,
head: null,
headAdded: null,
tail: null,
tailAdded:null,
lastInterval: _this.statsHolder.lastInterval,
lastExpiredCount: _this.statsHolder.lastExpiredCount,
lastExpiredAdded: _this.statsHolder.lastExpiredAdded,
lastExpiredRemoved: _this.statsHolder.lastExpiredRemoved,
};
if( storage.count > 0 ){
statsObj.head = storage.list.head;
statsObj.headAdded = storage.table[storage.list.head].added;
statsObj.tail = storage.list.tail;
statsObj.tailAdded = storage.table[storage.list.tail].added;
}
return statsObj;
};
};