-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.js
202 lines (183 loc) · 6.09 KB
/
cache.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
// @themost-framework 2.0 Codename Blueshift Copyright (c) 2017-2025, THEMOST LP All rights reserved
var NodeCache = require('node-cache');
var AbstractMethodError = require("@themost/common").AbstractMethodError;
var LangUtils = require('@themost/common').LangUtils;
var Args = require('@themost/common').Args;
var HttpApplicationService = require('./types').HttpApplicationService;
var CACHE_ABSOLUTE_EXPIRATION = 0;
/**
* @abstract
* @class
*/
class CacheStrategy extends HttpApplicationService {
constructor(app) {
super(app);
}
/**
* 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|*}
*/
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|*}
*/
remove(key) {
throw new AbstractMethodError();
}
/**
* Flush all cached data.
* @abstract
* @returns {Promise|*}
*/
clear() {
throw new AbstractMethodError();
}
/**
* Gets a cached value defined by the given key.
* @param {string} key
* @returns {Promise|*}
*/
get(key) {
throw new AbstractMethodError();
}
/**
* 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} fn - 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, fn, absoluteExpiration) {
throw new AbstractMethodError();
}
}
/**
* DefaultCacheStrategy class that extends CacheStrategy.
* @property @readonly {NodeCache} rawCache - Gets the raw cache object.
* This class provides a default implementation for caching strategies.
*/
class DefaultCacheStrategy extends CacheStrategy {
constructor(app) {
super(app);
//set absoluteExpiration (from application configuration)
var expiration = CACHE_ABSOLUTE_EXPIRATION;
var absoluteExpiration = LangUtils.parseInt(app.getConfiguration().getSourceAt('settings/cache/absoluteExpiration'));
if (absoluteExpiration>=0) {
expiration = absoluteExpiration;
}
Object.defineProperty(this, 'rawCache', {
enumerable: false,
configurable: true,
writable: false,
value: new NodeCache( {
stdTTL:expiration
})
});
}
/**
* 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|*}
*/
add(key, value, absoluteExpiration) {
var self = this;
return new Promise(function(resolve, reject) {
self.rawCache.set(key, value, absoluteExpiration, function(err) {
if (err) {
return reject(err);
}
return resolve();
});
});
}
/**
* Removes a cached value.
* @abstract
* @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) {
self.rawCache.del(key, function(err) {
if (err) {
return reject(err);
}
return resolve();
});
});
}
/**
* Flush all cached data.
* @abstract
* @returns {Promise|*}
*/
clear() {
this.rawCache.flushAll();
return Promise.resolve();
}
/**
* 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) {
void self.rawCache.get(key, function(err, result) {
if (err) {
return reject(err);
}
return resolve(result);
});
});
}
/**
* 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:Promise<any>} getValue - 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, getValue, absoluteExpiration) {
var self = this;
Args.check(typeof getValue === 'function','Invalid argument. Expected function.');
return self.get(key).then(function(res) {
if (typeof res === 'undefined') {
return getValue().then(function (res) {
if (typeof res === 'undefined') {
return Promise.resolve(res);
}
return self.add(key, res, absoluteExpiration).then(function () {
return Promise.resolve(res);
});
});
}
return Promise.resolve(res);
});
}
finalize() {
this.rawCache.close();
return Promise.resolve();
}
finalizeAsync() {
this.rawCache.close()
return Promise.resolve();
}
}
module.exports = {
CacheStrategy,
DefaultCacheStrategy
}