1 | "use strict";
|
2 |
|
3 |
|
4 | Object.defineProperty(exports, "__esModule", { value: true });
|
5 | var tslib_1 = require("tslib");
|
6 | var core_1 = require("@aws-amplify/core");
|
7 | var Utils_1 = require("./Utils");
|
8 | var StorageCache_1 = require("./StorageCache");
|
9 | var logger = new core_1.ConsoleLogger('Cache');
|
10 |
|
11 |
|
12 |
|
13 | var BrowserStorageCacheClass = (function (_super) {
|
14 | tslib_1.__extends(BrowserStorageCacheClass, _super);
|
15 | |
16 |
|
17 |
|
18 |
|
19 | function BrowserStorageCacheClass(config) {
|
20 | var _this = this;
|
21 | var cacheConfig = config
|
22 | ? Object.assign({}, Utils_1.defaultConfig, config)
|
23 | : Utils_1.defaultConfig;
|
24 | _this = _super.call(this, cacheConfig) || this;
|
25 | _this.config.storage = cacheConfig.storage;
|
26 | _this.getItem = _this.getItem.bind(_this);
|
27 | _this.setItem = _this.setItem.bind(_this);
|
28 | _this.removeItem = _this.removeItem.bind(_this);
|
29 | return _this;
|
30 | }
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | BrowserStorageCacheClass.prototype._decreaseCurSizeInBytes = function (amount) {
|
38 | var curSize = this.getCacheCurSize();
|
39 | this.config.storage.setItem(this.cacheCurSizeKey, (curSize - amount).toString());
|
40 | };
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | BrowserStorageCacheClass.prototype._increaseCurSizeInBytes = function (amount) {
|
48 | var curSize = this.getCacheCurSize();
|
49 | this.config.storage.setItem(this.cacheCurSizeKey, (curSize + amount).toString());
|
50 | };
|
51 | |
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | BrowserStorageCacheClass.prototype._refreshItem = function (item, prefixedKey) {
|
61 | item.visitedTime = Utils_1.getCurrTime();
|
62 | this.config.storage.setItem(prefixedKey, JSON.stringify(item));
|
63 | return item;
|
64 | };
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | BrowserStorageCacheClass.prototype._isExpired = function (key) {
|
74 | var text = this.config.storage.getItem(key);
|
75 | var item = JSON.parse(text);
|
76 | if (Utils_1.getCurrTime() >= item.expires) {
|
77 | return true;
|
78 | }
|
79 | return false;
|
80 | };
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | BrowserStorageCacheClass.prototype._removeItem = function (prefixedKey, size) {
|
89 | var itemSize = size
|
90 | ? size
|
91 | : JSON.parse(this.config.storage.getItem(prefixedKey)).byteSize;
|
92 | this._decreaseCurSizeInBytes(itemSize);
|
93 |
|
94 | this.config.storage.removeItem(prefixedKey);
|
95 | };
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | BrowserStorageCacheClass.prototype._setItem = function (prefixedKey, item) {
|
105 |
|
106 | this._increaseCurSizeInBytes(item.byteSize);
|
107 | try {
|
108 | this.config.storage.setItem(prefixedKey, JSON.stringify(item));
|
109 | }
|
110 | catch (setItemErr) {
|
111 |
|
112 | this._decreaseCurSizeInBytes(item.byteSize);
|
113 | logger.error("Failed to set item " + setItemErr);
|
114 | }
|
115 | };
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | BrowserStorageCacheClass.prototype._sizeToPop = function (itemSize) {
|
125 | var spaceItemNeed = this.getCacheCurSize() + itemSize - this.config.capacityInBytes;
|
126 | var cacheThresholdSpace = (1 - this.config.warningThreshold) * this.config.capacityInBytes;
|
127 | return spaceItemNeed > cacheThresholdSpace
|
128 | ? spaceItemNeed
|
129 | : cacheThresholdSpace;
|
130 | };
|
131 | |
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | BrowserStorageCacheClass.prototype._isCacheFull = function (itemSize) {
|
140 | return itemSize + this.getCacheCurSize() > this.config.capacityInBytes;
|
141 | };
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | BrowserStorageCacheClass.prototype._findValidKeys = function () {
|
151 | var keys = [];
|
152 | var keyInCache = [];
|
153 |
|
154 | for (var i = 0; i < this.config.storage.length; i += 1) {
|
155 | keyInCache.push(this.config.storage.key(i));
|
156 | }
|
157 |
|
158 | for (var i = 0; i < keyInCache.length; i += 1) {
|
159 | var key = keyInCache[i];
|
160 | if (key.indexOf(this.config.keyPrefix) === 0 &&
|
161 | key !== this.cacheCurSizeKey) {
|
162 | if (this._isExpired(key)) {
|
163 | this._removeItem(key);
|
164 | }
|
165 | else {
|
166 | keys.push(key);
|
167 | }
|
168 | }
|
169 | }
|
170 | return keys;
|
171 | };
|
172 | |
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | BrowserStorageCacheClass.prototype._popOutItems = function (keys, sizeToPop) {
|
182 | var items = [];
|
183 | var remainedSize = sizeToPop;
|
184 |
|
185 | for (var i = 0; i < keys.length; i += 1) {
|
186 | var val = this.config.storage.getItem(keys[i]);
|
187 | if (val != null) {
|
188 | var item = JSON.parse(val);
|
189 | items.push(item);
|
190 | }
|
191 | }
|
192 |
|
193 |
|
194 | items.sort(function (a, b) {
|
195 | if (a.priority > b.priority) {
|
196 | return -1;
|
197 | }
|
198 | else if (a.priority < b.priority) {
|
199 | return 1;
|
200 | }
|
201 | else {
|
202 | if (a.visitedTime < b.visitedTime) {
|
203 | return -1;
|
204 | }
|
205 | else
|
206 | return 1;
|
207 | }
|
208 | });
|
209 | for (var i = 0; i < items.length; i += 1) {
|
210 |
|
211 | this._removeItem(items[i].key, items[i].byteSize);
|
212 | remainedSize -= items[i].byteSize;
|
213 | if (remainedSize <= 0) {
|
214 | return;
|
215 | }
|
216 | }
|
217 | };
|
218 | |
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | BrowserStorageCacheClass.prototype.setItem = function (key, value, options) {
|
235 | logger.log("Set item: key is " + key + ", value is " + value + " with options: " + options);
|
236 | var prefixedKey = this.config.keyPrefix + key;
|
237 |
|
238 | if (prefixedKey === this.config.keyPrefix ||
|
239 | prefixedKey === this.cacheCurSizeKey) {
|
240 | logger.warn("Invalid key: should not be empty or 'CurSize'");
|
241 | return;
|
242 | }
|
243 | if (typeof value === 'undefined') {
|
244 | logger.warn("The value of item should not be undefined!");
|
245 | return;
|
246 | }
|
247 | var cacheItemOptions = {
|
248 | priority: options && options.priority !== undefined
|
249 | ? options.priority
|
250 | : this.config.defaultPriority,
|
251 | expires: options && options.expires !== undefined
|
252 | ? options.expires
|
253 | : this.config.defaultTTL + Utils_1.getCurrTime(),
|
254 | };
|
255 | if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
|
256 | logger.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");
|
257 | return;
|
258 | }
|
259 | var item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
|
260 |
|
261 | if (item.byteSize > this.config.itemMaxSize) {
|
262 | logger.warn("Item with key: " + key + " you are trying to put into is too big!");
|
263 | return;
|
264 | }
|
265 | try {
|
266 |
|
267 | var val = this.config.storage.getItem(prefixedKey);
|
268 | if (val) {
|
269 | this._removeItem(prefixedKey, JSON.parse(val).byteSize);
|
270 | }
|
271 |
|
272 | if (this._isCacheFull(item.byteSize)) {
|
273 | var validKeys = this._findValidKeys();
|
274 |
|
275 | if (this._isCacheFull(item.byteSize)) {
|
276 | var sizeToPop = this._sizeToPop(item.byteSize);
|
277 | this._popOutItems(validKeys, sizeToPop);
|
278 | }
|
279 | }
|
280 |
|
281 |
|
282 | this._setItem(prefixedKey, item);
|
283 | }
|
284 | catch (e) {
|
285 | logger.warn("setItem failed! " + e);
|
286 | }
|
287 | };
|
288 | |
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 | BrowserStorageCacheClass.prototype.getItem = function (key, options) {
|
304 | logger.log("Get item: key is " + key + " with options " + options);
|
305 | var ret = null;
|
306 | var prefixedKey = this.config.keyPrefix + key;
|
307 | if (prefixedKey === this.config.keyPrefix ||
|
308 | prefixedKey === this.cacheCurSizeKey) {
|
309 | logger.warn("Invalid key: should not be empty or 'CurSize'");
|
310 | return null;
|
311 | }
|
312 | try {
|
313 | ret = this.config.storage.getItem(prefixedKey);
|
314 | if (ret != null) {
|
315 | if (this._isExpired(prefixedKey)) {
|
316 |
|
317 | this._removeItem(prefixedKey, JSON.parse(ret).byteSize);
|
318 | ret = null;
|
319 | }
|
320 | else {
|
321 |
|
322 | var item = JSON.parse(ret);
|
323 | item = this._refreshItem(item, prefixedKey);
|
324 | return item.data;
|
325 | }
|
326 | }
|
327 | if (options && options.callback !== undefined) {
|
328 | var val = options.callback();
|
329 | if (val !== null) {
|
330 | this.setItem(key, val, options);
|
331 | }
|
332 | return val;
|
333 | }
|
334 | return null;
|
335 | }
|
336 | catch (e) {
|
337 | logger.warn("getItem failed! " + e);
|
338 | return null;
|
339 | }
|
340 | };
|
341 | |
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 | BrowserStorageCacheClass.prototype.removeItem = function (key) {
|
348 | logger.log("Remove item: key is " + key);
|
349 | var prefixedKey = this.config.keyPrefix + key;
|
350 | if (prefixedKey === this.config.keyPrefix ||
|
351 | prefixedKey === this.cacheCurSizeKey) {
|
352 | return;
|
353 | }
|
354 | try {
|
355 | var val = this.config.storage.getItem(prefixedKey);
|
356 | if (val) {
|
357 | this._removeItem(prefixedKey, JSON.parse(val).byteSize);
|
358 | }
|
359 | }
|
360 | catch (e) {
|
361 | logger.warn("removeItem failed! " + e);
|
362 | }
|
363 | };
|
364 | |
365 |
|
366 |
|
367 |
|
368 |
|
369 | BrowserStorageCacheClass.prototype.clear = function () {
|
370 | logger.log("Clear Cache");
|
371 | var keysToRemove = [];
|
372 | for (var i = 0; i < this.config.storage.length; i += 1) {
|
373 | var key = this.config.storage.key(i);
|
374 | if (key.indexOf(this.config.keyPrefix) === 0) {
|
375 | keysToRemove.push(key);
|
376 | }
|
377 | }
|
378 | try {
|
379 | for (var i = 0; i < keysToRemove.length; i += 1) {
|
380 | this.config.storage.removeItem(keysToRemove[i]);
|
381 | }
|
382 | }
|
383 | catch (e) {
|
384 | logger.warn("clear failed! " + e);
|
385 | }
|
386 | };
|
387 | |
388 |
|
389 |
|
390 |
|
391 |
|
392 | BrowserStorageCacheClass.prototype.getAllKeys = function () {
|
393 | var keys = [];
|
394 | for (var i = 0; i < this.config.storage.length; i += 1) {
|
395 | var key = this.config.storage.key(i);
|
396 | if (key.indexOf(this.config.keyPrefix) === 0 &&
|
397 | key !== this.cacheCurSizeKey) {
|
398 | keys.push(key.substring(this.config.keyPrefix.length));
|
399 | }
|
400 | }
|
401 | return keys;
|
402 | };
|
403 | |
404 |
|
405 |
|
406 |
|
407 |
|
408 | BrowserStorageCacheClass.prototype.getCacheCurSize = function () {
|
409 | var ret = this.config.storage.getItem(this.cacheCurSizeKey);
|
410 | if (!ret) {
|
411 | this.config.storage.setItem(this.cacheCurSizeKey, '0');
|
412 | ret = '0';
|
413 | }
|
414 | return Number(ret);
|
415 | };
|
416 | |
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 | BrowserStorageCacheClass.prototype.createInstance = function (config) {
|
423 | if (!config.keyPrefix || config.keyPrefix === Utils_1.defaultConfig.keyPrefix) {
|
424 | logger.error('invalid keyPrefix, setting keyPrefix with timeStamp');
|
425 | config.keyPrefix = Utils_1.getCurrTime.toString();
|
426 | }
|
427 | return new BrowserStorageCacheClass(config);
|
428 | };
|
429 | return BrowserStorageCacheClass;
|
430 | }(StorageCache_1.StorageCache));
|
431 | exports.BrowserStorageCacheClass = BrowserStorageCacheClass;
|
432 | exports.BrowserStorageCache = new BrowserStorageCacheClass();
|
433 | core_1.Amplify.register(exports.BrowserStorageCache);
|
434 |
|
\ | No newline at end of file |