-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdata-cache.js
More file actions
236 lines (222 loc) · 7.41 KB
/
data-cache.js
File metadata and controls
236 lines (222 loc) · 7.41 KB
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
// MOST Web Framework 2.0 Codename Blueshift BSD-3-Clause license Copyright (c) 2017-2022, THEMOST LP All rights reserved
var {AbstractMethodError, ConfigurationStrategy} = require('@themost/common');
var NodeCache = require( 'node-cache' );
var CACHE_ABSOLUTE_EXPIRATION = 1200;
class DataCacheStrategy extends ConfigurationStrategy {
/**
*
* @param {import('@themost/common').ConfigurationBase} config
* @constructor
*
*/
constructor(config) {
super(config);
}
/**
* Sets a key value pair in cache.
* @abstract
* @param {string} key - A string that represents the key of the cached value
* @param {*} value - The value to be cached
* @param {number=} absoluteExpiration - An absolute expiration time in seconds. This parameter is optional.
* @returns {Promise|*}
*/
// eslint-disable-next-line no-unused-vars
add(key, value, absoluteExpiration) {
throw new AbstractMethodError();
}
/**
* Removes a cached value.
* @abstract
* @param {string} key - A string that represents the key of the cached value to be removed
* @returns {Promise|*}
*/
// eslint-disable-next-line no-unused-vars
remove(key) {
throw new AbstractMethodError();
}
/**
* Flush all cached data.
* @abstract
* @returns {Promise|*}
*/
clear() {
throw new AbstractMethodError();
}
// noinspection JSUnusedLocalSymbols
/**
* Gets a cached value defined by the given key.
* @param {string} key
* @returns {Promise|*}
*/
// eslint-disable-next-line no-unused-vars
get(key) {
throw new AbstractMethodError();
}
// noinspection JSUnusedLocalSymbols
/**
* Gets data from cache or executes the defined function and adds the result to the cache with the specified key
* @param {string|*} key - A string which represents the key of the cached data
* @param {Function} getFunc - A function to execute if data will not be found in cache
* @param {number=} absoluteExpiration - An absolute expiration time in seconds. This parameter is optional.
* @returns {Promise|*}
*/
// eslint-disable-next-line no-unused-vars
getOrDefault(key, getFunc, absoluteExpiration) {
throw new AbstractMethodError();
}
}
class DefaultDataCacheStrategy extends DataCacheStrategy {
/**
*
* @param {import('@themost/common').ConfigurationBase} config
* @constructor
*
*/
constructor(config) {
super(config);
var expiration = CACHE_ABSOLUTE_EXPIRATION;
var absoluteExpiration = config.getSourceAt('settings/cache/absoluteExpiration');
if (typeof absoluteExpiration === 'number' && absoluteExpiration > 0) {
expiration = absoluteExpiration;
}
this.rawCache = new NodeCache({
stdTTL:expiration
});
}
/**
* Sets a key value pair in cache.
* @param {string} key - A string that represents the key of the cached value
* @param {*} value - The value to be cached
* @param {number=} absoluteExpiration - An absolute expiration time in seconds. This parameter is optional.
* @returns {Promise|*}
*/
add(key, value, absoluteExpiration) {
var self = this;
return new Promise(function(resolve, reject) {
try {
void self.rawCache.set(key, value, absoluteExpiration, (err) => {
if (err) {
return reject(err);
}
return resolve();
});
} catch (err) {
return reject(err);
}
});
}
/**
* Gets a cached value defined by the given key.
* @param {string} key
* @returns {Promise|*}
*/
get(key) {
var self = this;
return new Promise(function(resolve, reject) {
try {
void self.rawCache.get(key, function(err, res) {
if (err) {
return reject(err);
}
if (Object.prototype.hasOwnProperty.call(res, key)) {
return resolve(res[key]);
}
return resolve();
});
} catch (err) {
return reject(err);
}
});
}
/**
* Removes a cached value.
* @param {string} key - A string that represents the key of the cached value to be removed
* @returns {Promise|*}
*/
remove(key) {
var self = this;
return new Promise(function(resolve, reject) {
try {
void self.rawCache.del(key, (err) => {
if (err) {
return reject(err);
}
return resolve();
});
} catch (err) {
return reject(err);
}
});
}
/**
* Flush all cached data.
* @returns {Promise|*}
*/
clear() {
var self = this;
return new Promise(function(resolve, reject) {
try {
void self.rawCache.flushAll((err) => {
if (err) {
return reject(err);
}
return resolve();
});
} catch (err) {
return reject(err);
}
return resolve();
});
}
finalize() {
var self = this;
return self.clear().then(function() {
if (self.rawCache.checkTimeout != null) {
clearTimeout(self.rawCache.checkTimeout);
return Promise.resolve();
}
});
}
/**
* Gets data from cache or executes the defined function and adds the result to the cache with the specified key
* @param {string|*} key - A string which represents the key of the cached data
* @param {Function} getFunc - A function to execute if data will not be found in cache
* @param {number=} absoluteExpiration - An absolute expiration time in seconds. This parameter is optional.
* @returns {Promise|*}
*/
getOrDefault(key, getFunc, absoluteExpiration) {
var self = this;
return new Promise(function(resolve, reject) {
//try to get from cache
try {
void self.rawCache.get(key, (err, res) => {
if (typeof res !== 'undefined') {
if (Object.prototype.hasOwnProperty.call(res, key)) {
return resolve(res[key]);
}
}
try {
void getFunc().then(function (res) {
void self.rawCache.set(key, res, absoluteExpiration, (err) => {
if (err) {
return reject(err);
}
return resolve(res);
});
}).catch(function(err) {
return reject(err);
})
} catch (err) {
return reject(err);
}
});
} catch (err) {
return reject(err);
}
});
}
}
module.exports = {
DataCacheStrategy,
DefaultDataCacheStrategy
}