1 | "use strict";
|
2 |
|
3 |
|
4 | Object.defineProperty(exports, "__esModule", { value: true });
|
5 | var tslib_1 = require("tslib");
|
6 | var Utils_1 = require("./Utils");
|
7 | var StorageCache_1 = require("./StorageCache");
|
8 | var core_1 = require("@aws-amplify/core");
|
9 | var logger = new core_1.ConsoleLogger('InMemoryCache');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var InMemoryCacheClass = (function (_super) {
|
19 | tslib_1.__extends(InMemoryCacheClass, _super);
|
20 | |
21 |
|
22 |
|
23 |
|
24 |
|
25 | function InMemoryCacheClass(config) {
|
26 | var _this = this;
|
27 | var cacheConfig = config
|
28 | ? Object.assign({}, Utils_1.defaultConfig, config)
|
29 | : Utils_1.defaultConfig;
|
30 | _this = _super.call(this, cacheConfig) || this;
|
31 | logger.debug('now we start!');
|
32 | _this.cacheList = [];
|
33 | _this.curSizeInBytes = 0;
|
34 | _this.maxPriority = 5;
|
35 | _this.getItem = _this.getItem.bind(_this);
|
36 | _this.setItem = _this.setItem.bind(_this);
|
37 | _this.removeItem = _this.removeItem.bind(_this);
|
38 |
|
39 | for (var i = 0; i < _this.maxPriority; i += 1) {
|
40 | _this.cacheList[i] = new Utils_1.CacheList();
|
41 | }
|
42 | return _this;
|
43 | }
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 | InMemoryCacheClass.prototype._decreaseCurSizeInBytes = function (amount) {
|
50 | this.curSizeInBytes -= amount;
|
51 | };
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 | InMemoryCacheClass.prototype._increaseCurSizeInBytes = function (amount) {
|
58 | this.curSizeInBytes += amount;
|
59 | };
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | InMemoryCacheClass.prototype._isExpired = function (key) {
|
68 | var text = Utils_1.CacheObject.getItem(key);
|
69 | var item = JSON.parse(text);
|
70 | if (Utils_1.getCurrTime() >= item.expires) {
|
71 | return true;
|
72 | }
|
73 | return false;
|
74 | };
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | InMemoryCacheClass.prototype._removeItem = function (prefixedKey, listIdx) {
|
82 |
|
83 | this.cacheList[listIdx].removeItem(prefixedKey);
|
84 |
|
85 | this._decreaseCurSizeInBytes(JSON.parse(Utils_1.CacheObject.getItem(prefixedKey)).byteSize);
|
86 |
|
87 | Utils_1.CacheObject.removeItem(prefixedKey);
|
88 | };
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | InMemoryCacheClass.prototype._setItem = function (prefixedKey, item, listIdx) {
|
98 |
|
99 | this.cacheList[listIdx].insertItem(prefixedKey);
|
100 |
|
101 | this._increaseCurSizeInBytes(item.byteSize);
|
102 |
|
103 | Utils_1.CacheObject.setItem(prefixedKey, JSON.stringify(item));
|
104 | };
|
105 | |
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | InMemoryCacheClass.prototype._isCacheFull = function (itemSize) {
|
113 | return this.curSizeInBytes + itemSize > this.config.capacityInBytes;
|
114 | };
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 | InMemoryCacheClass.prototype.containsKey = function (key) {
|
121 | var prefixedKey = this.config.keyPrefix + key;
|
122 | for (var i = 0; i < this.maxPriority; i += 1) {
|
123 | if (this.cacheList[i].containsKey(prefixedKey)) {
|
124 | return i + 1;
|
125 | }
|
126 | }
|
127 | return -1;
|
128 | };
|
129 | |
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | InMemoryCacheClass.prototype.setItem = function (key, value, options) {
|
149 | var prefixedKey = this.config.keyPrefix + key;
|
150 |
|
151 | if (prefixedKey === this.config.keyPrefix ||
|
152 | prefixedKey === this.cacheCurSizeKey) {
|
153 | logger.warn("Invalid key: should not be empty or 'CurSize'");
|
154 | return;
|
155 | }
|
156 | if (typeof value === 'undefined') {
|
157 | logger.warn("The value of item should not be undefined!");
|
158 | return;
|
159 | }
|
160 | var cacheItemOptions = {
|
161 | priority: options && options.priority !== undefined
|
162 | ? options.priority
|
163 | : this.config.defaultPriority,
|
164 | expires: options && options.expires !== undefined
|
165 | ? options.expires
|
166 | : this.config.defaultTTL + Utils_1.getCurrTime(),
|
167 | };
|
168 | if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
|
169 | logger.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");
|
170 | return;
|
171 | }
|
172 | var item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
|
173 |
|
174 | if (item.byteSize > this.config.itemMaxSize) {
|
175 | logger.warn("Item with key: " + key + " you are trying to put into is too big!");
|
176 | return;
|
177 | }
|
178 |
|
179 | var presentKeyPrio = this.containsKey(key);
|
180 | if (presentKeyPrio !== -1) {
|
181 | this._removeItem(prefixedKey, presentKeyPrio - 1);
|
182 | }
|
183 |
|
184 |
|
185 | var cacheListIdx = this.maxPriority - 1;
|
186 | while (this._isCacheFull(item.byteSize) && cacheListIdx >= 0) {
|
187 | if (!this.cacheList[cacheListIdx].isEmpty()) {
|
188 | var popedItemKey = this.cacheList[cacheListIdx].getLastItem();
|
189 | this._removeItem(popedItemKey, cacheListIdx);
|
190 | }
|
191 | else {
|
192 | cacheListIdx -= 1;
|
193 | }
|
194 | }
|
195 | this._setItem(prefixedKey, item, Number(item.priority) - 1);
|
196 | };
|
197 | |
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | InMemoryCacheClass.prototype.getItem = function (key, options) {
|
210 | var ret = null;
|
211 | var prefixedKey = this.config.keyPrefix + key;
|
212 | if (prefixedKey === this.config.keyPrefix ||
|
213 | prefixedKey === this.cacheCurSizeKey) {
|
214 | logger.warn("Invalid key: should not be empty or 'CurSize'");
|
215 | return null;
|
216 | }
|
217 |
|
218 | var presentKeyPrio = this.containsKey(key);
|
219 | if (presentKeyPrio !== -1) {
|
220 | if (this._isExpired(prefixedKey)) {
|
221 |
|
222 | this._removeItem(prefixedKey, presentKeyPrio - 1);
|
223 | }
|
224 | else {
|
225 |
|
226 | ret = Utils_1.CacheObject.getItem(prefixedKey);
|
227 | var item = JSON.parse(ret);
|
228 | this.cacheList[item.priority - 1].refresh(prefixedKey);
|
229 | return item.data;
|
230 | }
|
231 | }
|
232 | if (options && options.callback !== undefined) {
|
233 | var val = options.callback();
|
234 | if (val !== null) {
|
235 | this.setItem(key, val, options);
|
236 | }
|
237 | return val;
|
238 | }
|
239 | return null;
|
240 | };
|
241 | |
242 |
|
243 |
|
244 |
|
245 |
|
246 | InMemoryCacheClass.prototype.removeItem = function (key) {
|
247 | var prefixedKey = this.config.keyPrefix + key;
|
248 |
|
249 | var presentKeyPrio = this.containsKey(key);
|
250 | if (presentKeyPrio !== -1) {
|
251 | this._removeItem(prefixedKey, presentKeyPrio - 1);
|
252 | }
|
253 | };
|
254 | |
255 |
|
256 |
|
257 | InMemoryCacheClass.prototype.clear = function () {
|
258 | var e_1, _a;
|
259 | for (var i = 0; i < this.maxPriority; i += 1) {
|
260 | try {
|
261 | for (var _b = (e_1 = void 0, tslib_1.__values(this.cacheList[i].getKeys())), _c = _b.next(); !_c.done; _c = _b.next()) {
|
262 | var key = _c.value;
|
263 | this._removeItem(key, i);
|
264 | }
|
265 | }
|
266 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
267 | finally {
|
268 | try {
|
269 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
270 | }
|
271 | finally { if (e_1) throw e_1.error; }
|
272 | }
|
273 | }
|
274 | };
|
275 | |
276 |
|
277 |
|
278 | InMemoryCacheClass.prototype.getAllKeys = function () {
|
279 | var e_2, _a;
|
280 | var keys = [];
|
281 | for (var i = 0; i < this.maxPriority; i += 1) {
|
282 | try {
|
283 | for (var _b = (e_2 = void 0, tslib_1.__values(this.cacheList[i].getKeys())), _c = _b.next(); !_c.done; _c = _b.next()) {
|
284 | var key = _c.value;
|
285 | keys.push(key.substring(this.config.keyPrefix.length));
|
286 | }
|
287 | }
|
288 | catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
289 | finally {
|
290 | try {
|
291 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
292 | }
|
293 | finally { if (e_2) throw e_2.error; }
|
294 | }
|
295 | }
|
296 | return keys;
|
297 | };
|
298 | |
299 |
|
300 |
|
301 |
|
302 |
|
303 | InMemoryCacheClass.prototype.getCacheCurSize = function () {
|
304 | return this.curSizeInBytes;
|
305 | };
|
306 | |
307 |
|
308 |
|
309 |
|
310 | InMemoryCacheClass.prototype.createInstance = function (config) {
|
311 | return new InMemoryCacheClass(config);
|
312 | };
|
313 | return InMemoryCacheClass;
|
314 | }(StorageCache_1.StorageCache));
|
315 | exports.InMemoryCacheClass = InMemoryCacheClass;
|
316 | exports.InMemoryCache = new InMemoryCacheClass();
|
317 |
|
\ | No newline at end of file |