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