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