@aws-amplify/cache 4.0.64-unstable.4 → 4.0.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/build.js +5 -0
- package/dist/aws-amplify-cache.js +1484 -0
- package/dist/aws-amplify-cache.js.map +1 -0
- package/dist/aws-amplify-cache.min.js +2 -0
- package/dist/aws-amplify-cache.min.js.map +1 -0
- package/index.js +7 -0
- package/lib/AsyncStorageCache.js +101 -40
- package/lib/AsyncStorageCache.js.map +1 -1
- package/lib/BrowserStorageCache.js +31 -6
- package/lib/BrowserStorageCache.js.map +1 -1
- package/lib/InMemoryCache.js +43 -6
- package/lib/InMemoryCache.js.map +1 -1
- package/lib/StorageCache.js +16 -2
- package/lib/StorageCache.js.map +1 -1
- package/lib/Utils/CacheList.js +24 -4
- package/lib/Utils/CacheList.js.map +1 -1
- package/lib/Utils/CacheUtils.js +12 -2
- package/lib/Utils/CacheUtils.js.map +1 -1
- package/lib/Utils/index.js +16 -8
- package/lib/Utils/index.js.map +1 -1
- package/lib/index.js +18 -2
- package/lib/index.js.map +1 -1
- package/lib/reactnative.js +18 -2
- package/lib/reactnative.js.map +1 -1
- package/lib/types/Cache.js +12 -2
- package/lib/types/Cache.js.map +1 -1
- package/lib/types/index.js +12 -2
- package/lib/types/index.js.map +1 -1
- package/lib-esm/AsyncStorageCache.d.ts +2 -1
- package/lib-esm/AsyncStorageCache.js +64 -6
- package/lib-esm/AsyncStorageCache.js.map +1 -1
- package/lib-esm/BrowserStorageCache.d.ts +4 -0
- package/lib-esm/BrowserStorageCache.js +30 -5
- package/lib-esm/BrowserStorageCache.js.map +1 -1
- package/lib-esm/InMemoryCache.d.ts +4 -0
- package/lib-esm/InMemoryCache.js +40 -3
- package/lib-esm/InMemoryCache.js.map +1 -1
- package/lib-esm/StorageCache.d.ts +4 -0
- package/lib-esm/StorageCache.js +16 -2
- package/lib-esm/StorageCache.js.map +1 -1
- package/lib-esm/Utils/CacheList.js +23 -3
- package/lib-esm/Utils/CacheList.js.map +1 -1
- package/lib-esm/Utils/CacheUtils.js +12 -2
- package/lib-esm/Utils/CacheUtils.js.map +1 -1
- package/lib-esm/Utils/index.d.ts +1 -1
- package/lib-esm/Utils/index.js +13 -3
- package/lib-esm/Utils/index.js.map +1 -1
- package/lib-esm/index.d.ts +4 -0
- package/lib-esm/index.js +18 -3
- package/lib-esm/index.js.map +1 -1
- package/lib-esm/reactnative.d.ts +5 -2
- package/lib-esm/reactnative.js +19 -5
- package/lib-esm/reactnative.js.map +1 -1
- package/lib-esm/types/Cache.js +12 -2
- package/lib-esm/types/Cache.js.map +1 -1
- package/lib-esm/types/index.js +12 -2
- package/lib-esm/types/index.js.map +1 -1
- package/package.json +12 -15
- package/src/AsyncStorageCache.ts +15 -6
- package/src/BrowserStorageCache.ts +18 -4
- package/src/InMemoryCache.ts +16 -2
- package/src/StorageCache.ts +17 -2
- package/src/Utils/CacheList.ts +12 -2
- package/src/Utils/CacheUtils.ts +12 -2
- package/src/Utils/index.ts +15 -11
- package/src/index.ts +20 -4
- package/src/reactnative.ts +19 -5
- package/src/types/Cache.ts +12 -2
- package/src/types/index.ts +12 -2
- package/webpack.config.dev.js +6 -0
- package/lib/.tsbuildinfo +0 -3
- package/lib/AsyncStorageCache.d.ts +0 -154
- package/lib/BrowserStorageCache.d.ts +0 -165
- package/lib/InMemoryCache.d.ts +0 -131
- package/lib/StorageCache.d.ts +0 -31
- package/lib/Utils/CacheList.d.ts +0 -89
- package/lib/Utils/CacheUtils.d.ts +0 -24
- package/lib/Utils/index.d.ts +0 -2
- package/lib/index.d.ts +0 -5
- package/lib/reactnative.d.ts +0 -3
- package/lib/types/Cache.d.ts +0 -55
- package/lib/types/index.d.ts +0 -1
- package/lib-esm/.tsbuildinfo +0 -3
|
@@ -0,0 +1,1484 @@
|
|
|
1
|
+
(function webpackUniversalModuleDefinition(root, factory) {
|
|
2
|
+
if(typeof exports === 'object' && typeof module === 'object')
|
|
3
|
+
module.exports = factory(require("aws_amplify_core"));
|
|
4
|
+
else if(typeof define === 'function' && define.amd)
|
|
5
|
+
define("aws_amplify_cache", ["aws_amplify_core"], factory);
|
|
6
|
+
else if(typeof exports === 'object')
|
|
7
|
+
exports["aws_amplify_cache"] = factory(require("aws_amplify_core"));
|
|
8
|
+
else
|
|
9
|
+
root["aws_amplify_cache"] = factory(root["aws_amplify_core"]);
|
|
10
|
+
})(this, function(__WEBPACK_EXTERNAL_MODULE__aws_amplify_core__) {
|
|
11
|
+
return /******/ (function(modules) { // webpackBootstrap
|
|
12
|
+
/******/ // The module cache
|
|
13
|
+
/******/ var installedModules = {};
|
|
14
|
+
/******/
|
|
15
|
+
/******/ // The require function
|
|
16
|
+
/******/ function __webpack_require__(moduleId) {
|
|
17
|
+
/******/
|
|
18
|
+
/******/ // Check if module is in cache
|
|
19
|
+
/******/ if(installedModules[moduleId]) {
|
|
20
|
+
/******/ return installedModules[moduleId].exports;
|
|
21
|
+
/******/ }
|
|
22
|
+
/******/ // Create a new module (and put it into the cache)
|
|
23
|
+
/******/ var module = installedModules[moduleId] = {
|
|
24
|
+
/******/ i: moduleId,
|
|
25
|
+
/******/ l: false,
|
|
26
|
+
/******/ exports: {}
|
|
27
|
+
/******/ };
|
|
28
|
+
/******/
|
|
29
|
+
/******/ // Execute the module function
|
|
30
|
+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
|
31
|
+
/******/
|
|
32
|
+
/******/ // Flag the module as loaded
|
|
33
|
+
/******/ module.l = true;
|
|
34
|
+
/******/
|
|
35
|
+
/******/ // Return the exports of the module
|
|
36
|
+
/******/ return module.exports;
|
|
37
|
+
/******/ }
|
|
38
|
+
/******/
|
|
39
|
+
/******/
|
|
40
|
+
/******/ // expose the modules object (__webpack_modules__)
|
|
41
|
+
/******/ __webpack_require__.m = modules;
|
|
42
|
+
/******/
|
|
43
|
+
/******/ // expose the module cache
|
|
44
|
+
/******/ __webpack_require__.c = installedModules;
|
|
45
|
+
/******/
|
|
46
|
+
/******/ // define getter function for harmony exports
|
|
47
|
+
/******/ __webpack_require__.d = function(exports, name, getter) {
|
|
48
|
+
/******/ if(!__webpack_require__.o(exports, name)) {
|
|
49
|
+
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
|
|
50
|
+
/******/ }
|
|
51
|
+
/******/ };
|
|
52
|
+
/******/
|
|
53
|
+
/******/ // define __esModule on exports
|
|
54
|
+
/******/ __webpack_require__.r = function(exports) {
|
|
55
|
+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
56
|
+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
57
|
+
/******/ }
|
|
58
|
+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
|
|
59
|
+
/******/ };
|
|
60
|
+
/******/
|
|
61
|
+
/******/ // create a fake namespace object
|
|
62
|
+
/******/ // mode & 1: value is a module id, require it
|
|
63
|
+
/******/ // mode & 2: merge all properties of value into the ns
|
|
64
|
+
/******/ // mode & 4: return value when already ns object
|
|
65
|
+
/******/ // mode & 8|1: behave like require
|
|
66
|
+
/******/ __webpack_require__.t = function(value, mode) {
|
|
67
|
+
/******/ if(mode & 1) value = __webpack_require__(value);
|
|
68
|
+
/******/ if(mode & 8) return value;
|
|
69
|
+
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
|
|
70
|
+
/******/ var ns = Object.create(null);
|
|
71
|
+
/******/ __webpack_require__.r(ns);
|
|
72
|
+
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
|
|
73
|
+
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
|
|
74
|
+
/******/ return ns;
|
|
75
|
+
/******/ };
|
|
76
|
+
/******/
|
|
77
|
+
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
|
78
|
+
/******/ __webpack_require__.n = function(module) {
|
|
79
|
+
/******/ var getter = module && module.__esModule ?
|
|
80
|
+
/******/ function getDefault() { return module['default']; } :
|
|
81
|
+
/******/ function getModuleExports() { return module; };
|
|
82
|
+
/******/ __webpack_require__.d(getter, 'a', getter);
|
|
83
|
+
/******/ return getter;
|
|
84
|
+
/******/ };
|
|
85
|
+
/******/
|
|
86
|
+
/******/ // Object.prototype.hasOwnProperty.call
|
|
87
|
+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
|
88
|
+
/******/
|
|
89
|
+
/******/ // __webpack_public_path__
|
|
90
|
+
/******/ __webpack_require__.p = "";
|
|
91
|
+
/******/
|
|
92
|
+
/******/
|
|
93
|
+
/******/ // Load entry module and return exports
|
|
94
|
+
/******/ return __webpack_require__(__webpack_require__.s = "./lib-esm/index.js");
|
|
95
|
+
/******/ })
|
|
96
|
+
/************************************************************************/
|
|
97
|
+
/******/ ({
|
|
98
|
+
|
|
99
|
+
/***/ "./lib-esm/BrowserStorageCache.js":
|
|
100
|
+
/*!****************************************!*\
|
|
101
|
+
!*** ./lib-esm/BrowserStorageCache.js ***!
|
|
102
|
+
\****************************************/
|
|
103
|
+
/*! exports provided: BrowserStorageCacheClass, BrowserStorageCache, default */
|
|
104
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
105
|
+
|
|
106
|
+
"use strict";
|
|
107
|
+
__webpack_require__.r(__webpack_exports__);
|
|
108
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "BrowserStorageCacheClass", function() { return BrowserStorageCacheClass; });
|
|
109
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "BrowserStorageCache", function() { return BrowserStorageCache; });
|
|
110
|
+
/* harmony import */ var _Utils__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./Utils */ "./lib-esm/Utils/index.js");
|
|
111
|
+
/* harmony import */ var _StorageCache__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./StorageCache */ "./lib-esm/StorageCache.js");
|
|
112
|
+
/* harmony import */ var _aws_amplify_core__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @aws-amplify/core */ "@aws-amplify/core");
|
|
113
|
+
/* harmony import */ var _aws_amplify_core__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_aws_amplify_core__WEBPACK_IMPORTED_MODULE_2__);
|
|
114
|
+
/*
|
|
115
|
+
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
116
|
+
*
|
|
117
|
+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
|
|
118
|
+
* the License. A copy of the License is located at
|
|
119
|
+
*
|
|
120
|
+
* http://aws.amazon.com/apache2.0/
|
|
121
|
+
*
|
|
122
|
+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
123
|
+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
|
|
124
|
+
* and limitations under the License.
|
|
125
|
+
*/
|
|
126
|
+
var __extends = undefined && undefined.__extends || function () {
|
|
127
|
+
var _extendStatics = function extendStatics(d, b) {
|
|
128
|
+
_extendStatics = Object.setPrototypeOf || {
|
|
129
|
+
__proto__: []
|
|
130
|
+
} instanceof Array && function (d, b) {
|
|
131
|
+
d.__proto__ = b;
|
|
132
|
+
} || function (d, b) {
|
|
133
|
+
for (var p in b) {
|
|
134
|
+
if (b.hasOwnProperty(p)) d[p] = b[p];
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
return _extendStatics(d, b);
|
|
138
|
+
};
|
|
139
|
+
return function (d, b) {
|
|
140
|
+
_extendStatics(d, b);
|
|
141
|
+
function __() {
|
|
142
|
+
this.constructor = d;
|
|
143
|
+
}
|
|
144
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
145
|
+
};
|
|
146
|
+
}();
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
var logger = new _aws_amplify_core__WEBPACK_IMPORTED_MODULE_2__["ConsoleLogger"]('Cache');
|
|
151
|
+
/**
|
|
152
|
+
* Customized storage based on the SessionStorage or LocalStorage with LRU implemented
|
|
153
|
+
*/
|
|
154
|
+
var BrowserStorageCacheClass = /** @class */function (_super) {
|
|
155
|
+
__extends(BrowserStorageCacheClass, _super);
|
|
156
|
+
/**
|
|
157
|
+
* initialize the cache
|
|
158
|
+
* @param config - the configuration of the cache
|
|
159
|
+
*/
|
|
160
|
+
function BrowserStorageCacheClass(config) {
|
|
161
|
+
var _this = this;
|
|
162
|
+
var cacheConfig = config ? Object.assign({}, _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"], config) : _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"];
|
|
163
|
+
_this = _super.call(this, cacheConfig) || this;
|
|
164
|
+
_this.config.storage = cacheConfig.storage;
|
|
165
|
+
_this.getItem = _this.getItem.bind(_this);
|
|
166
|
+
_this.setItem = _this.setItem.bind(_this);
|
|
167
|
+
_this.removeItem = _this.removeItem.bind(_this);
|
|
168
|
+
return _this;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* decrease current size of the cache
|
|
172
|
+
*
|
|
173
|
+
* @private
|
|
174
|
+
* @param amount - the amount of the cache size which needs to be decreased
|
|
175
|
+
*/
|
|
176
|
+
BrowserStorageCacheClass.prototype._decreaseCurSizeInBytes = function (amount) {
|
|
177
|
+
var curSize = this.getCacheCurSize();
|
|
178
|
+
this.config.storage.setItem(this.cacheCurSizeKey, (curSize - amount).toString());
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* increase current size of the cache
|
|
182
|
+
*
|
|
183
|
+
* @private
|
|
184
|
+
* @param amount - the amount of the cache szie which need to be increased
|
|
185
|
+
*/
|
|
186
|
+
BrowserStorageCacheClass.prototype._increaseCurSizeInBytes = function (amount) {
|
|
187
|
+
var curSize = this.getCacheCurSize();
|
|
188
|
+
this.config.storage.setItem(this.cacheCurSizeKey, (curSize + amount).toString());
|
|
189
|
+
};
|
|
190
|
+
/**
|
|
191
|
+
* update the visited time if item has been visited
|
|
192
|
+
*
|
|
193
|
+
* @private
|
|
194
|
+
* @param item - the item which need to be refreshed
|
|
195
|
+
* @param prefixedKey - the key of the item
|
|
196
|
+
*
|
|
197
|
+
* @return the refreshed item
|
|
198
|
+
*/
|
|
199
|
+
BrowserStorageCacheClass.prototype._refreshItem = function (item, prefixedKey) {
|
|
200
|
+
item.visitedTime = Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"])();
|
|
201
|
+
this.config.storage.setItem(prefixedKey, JSON.stringify(item));
|
|
202
|
+
return item;
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* check wether item is expired
|
|
206
|
+
*
|
|
207
|
+
* @private
|
|
208
|
+
* @param key - the key of the item
|
|
209
|
+
*
|
|
210
|
+
* @return true if the item is expired.
|
|
211
|
+
*/
|
|
212
|
+
BrowserStorageCacheClass.prototype._isExpired = function (key) {
|
|
213
|
+
var text = this.config.storage.getItem(key);
|
|
214
|
+
var item = JSON.parse(text);
|
|
215
|
+
if (Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"])() >= item.expires) {
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
return false;
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* delete item from cache
|
|
222
|
+
*
|
|
223
|
+
* @private
|
|
224
|
+
* @param prefixedKey - the key of the item
|
|
225
|
+
* @param size - optional, the byte size of the item
|
|
226
|
+
*/
|
|
227
|
+
BrowserStorageCacheClass.prototype._removeItem = function (prefixedKey, size) {
|
|
228
|
+
var itemSize = size ? size : JSON.parse(this.config.storage.getItem(prefixedKey)).byteSize;
|
|
229
|
+
this._decreaseCurSizeInBytes(itemSize);
|
|
230
|
+
// remove the cache item
|
|
231
|
+
this.config.storage.removeItem(prefixedKey);
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* put item into cache
|
|
235
|
+
*
|
|
236
|
+
* @private
|
|
237
|
+
* @param prefixedKey - the key of the item
|
|
238
|
+
* @param itemData - the value of the item
|
|
239
|
+
* @param itemSizeInBytes - the byte size of the item
|
|
240
|
+
*/
|
|
241
|
+
BrowserStorageCacheClass.prototype._setItem = function (prefixedKey, item) {
|
|
242
|
+
// update the cache size
|
|
243
|
+
this._increaseCurSizeInBytes(item.byteSize);
|
|
244
|
+
try {
|
|
245
|
+
this.config.storage.setItem(prefixedKey, JSON.stringify(item));
|
|
246
|
+
} catch (setItemErr) {
|
|
247
|
+
// if failed, we need to rollback the cache size
|
|
248
|
+
this._decreaseCurSizeInBytes(item.byteSize);
|
|
249
|
+
logger.error("Failed to set item " + setItemErr);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* total space needed when poping out items
|
|
254
|
+
*
|
|
255
|
+
* @private
|
|
256
|
+
* @param itemSize
|
|
257
|
+
*
|
|
258
|
+
* @return total space needed
|
|
259
|
+
*/
|
|
260
|
+
BrowserStorageCacheClass.prototype._sizeToPop = function (itemSize) {
|
|
261
|
+
var spaceItemNeed = this.getCacheCurSize() + itemSize - this.config.capacityInBytes;
|
|
262
|
+
var cacheThresholdSpace = (1 - this.config.warningThreshold) * this.config.capacityInBytes;
|
|
263
|
+
return spaceItemNeed > cacheThresholdSpace ? spaceItemNeed : cacheThresholdSpace;
|
|
264
|
+
};
|
|
265
|
+
/**
|
|
266
|
+
* see whether cache is full
|
|
267
|
+
*
|
|
268
|
+
* @private
|
|
269
|
+
* @param itemSize
|
|
270
|
+
*
|
|
271
|
+
* @return true if cache is full
|
|
272
|
+
*/
|
|
273
|
+
BrowserStorageCacheClass.prototype._isCacheFull = function (itemSize) {
|
|
274
|
+
return itemSize + this.getCacheCurSize() > this.config.capacityInBytes;
|
|
275
|
+
};
|
|
276
|
+
/**
|
|
277
|
+
* scan the storage and find out all the keys owned by this cache
|
|
278
|
+
* also clean the expired keys while scanning
|
|
279
|
+
*
|
|
280
|
+
* @private
|
|
281
|
+
*
|
|
282
|
+
* @return array of keys
|
|
283
|
+
*/
|
|
284
|
+
BrowserStorageCacheClass.prototype._findValidKeys = function () {
|
|
285
|
+
var keys = [];
|
|
286
|
+
var keyInCache = [];
|
|
287
|
+
// get all keys in Storage
|
|
288
|
+
for (var i = 0; i < this.config.storage.length; i += 1) {
|
|
289
|
+
keyInCache.push(this.config.storage.key(i));
|
|
290
|
+
}
|
|
291
|
+
// find those items which belong to our cache and also clean those expired items
|
|
292
|
+
for (var i = 0; i < keyInCache.length; i += 1) {
|
|
293
|
+
var key = keyInCache[i];
|
|
294
|
+
if (key.indexOf(this.config.keyPrefix) === 0 && key !== this.cacheCurSizeKey) {
|
|
295
|
+
if (this._isExpired(key)) {
|
|
296
|
+
this._removeItem(key);
|
|
297
|
+
} else {
|
|
298
|
+
keys.push(key);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return keys;
|
|
303
|
+
};
|
|
304
|
+
/**
|
|
305
|
+
* get all the items we have, sort them by their priority,
|
|
306
|
+
* if priority is same, sort them by their last visited time
|
|
307
|
+
* pop out items from the low priority (5 is the lowest)
|
|
308
|
+
*
|
|
309
|
+
* @private
|
|
310
|
+
* @param keys - all the keys in this cache
|
|
311
|
+
* @param sizeToPop - the total size of the items which needed to be poped out
|
|
312
|
+
*/
|
|
313
|
+
BrowserStorageCacheClass.prototype._popOutItems = function (keys, sizeToPop) {
|
|
314
|
+
var items = [];
|
|
315
|
+
var remainedSize = sizeToPop;
|
|
316
|
+
// get the items from Storage
|
|
317
|
+
for (var i = 0; i < keys.length; i += 1) {
|
|
318
|
+
var val = this.config.storage.getItem(keys[i]);
|
|
319
|
+
if (val != null) {
|
|
320
|
+
var item = JSON.parse(val);
|
|
321
|
+
items.push(item);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
// first compare priority
|
|
325
|
+
// then compare visited time
|
|
326
|
+
items.sort(function (a, b) {
|
|
327
|
+
if (a.priority > b.priority) {
|
|
328
|
+
return -1;
|
|
329
|
+
} else if (a.priority < b.priority) {
|
|
330
|
+
return 1;
|
|
331
|
+
} else {
|
|
332
|
+
if (a.visitedTime < b.visitedTime) {
|
|
333
|
+
return -1;
|
|
334
|
+
} else return 1;
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
for (var i = 0; i < items.length; i += 1) {
|
|
338
|
+
// pop out items until we have enough room for new item
|
|
339
|
+
this._removeItem(items[i].key, items[i].byteSize);
|
|
340
|
+
remainedSize -= items[i].byteSize;
|
|
341
|
+
if (remainedSize <= 0) {
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
/**
|
|
347
|
+
* Set item into cache. You can put number, string, boolean or object.
|
|
348
|
+
* The cache will first check whether has the same key.
|
|
349
|
+
* If it has, it will delete the old item and then put the new item in
|
|
350
|
+
* The cache will pop out items if it is full
|
|
351
|
+
* You can specify the cache item options. The cache will abort and output a warning:
|
|
352
|
+
* If the key is invalid
|
|
353
|
+
* If the size of the item exceeds itemMaxSize.
|
|
354
|
+
* If the value is undefined
|
|
355
|
+
* If incorrect cache item configuration
|
|
356
|
+
* If error happened with browser storage
|
|
357
|
+
*
|
|
358
|
+
* @param key - the key of the item
|
|
359
|
+
* @param value - the value of the item
|
|
360
|
+
* @param {Object} [options] - optional, the specified meta-data
|
|
361
|
+
*/
|
|
362
|
+
BrowserStorageCacheClass.prototype.setItem = function (key, value, options) {
|
|
363
|
+
logger.log("Set item: key is " + key + ", value is " + value + " with options: " + options);
|
|
364
|
+
var prefixedKey = this.config.keyPrefix + key;
|
|
365
|
+
// invalid keys
|
|
366
|
+
if (prefixedKey === this.config.keyPrefix || prefixedKey === this.cacheCurSizeKey) {
|
|
367
|
+
logger.warn("Invalid key: should not be empty or 'CurSize'");
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (typeof value === 'undefined') {
|
|
371
|
+
logger.warn("The value of item should not be undefined!");
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
var cacheItemOptions = {
|
|
375
|
+
priority: options && options.priority !== undefined ? options.priority : this.config.defaultPriority,
|
|
376
|
+
expires: options && options.expires !== undefined ? options.expires : this.config.defaultTTL + Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"])()
|
|
377
|
+
};
|
|
378
|
+
if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
|
|
379
|
+
logger.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
var item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
|
|
383
|
+
// check wether this item is too big;
|
|
384
|
+
if (item.byteSize > this.config.itemMaxSize) {
|
|
385
|
+
logger.warn("Item with key: " + key + " you are trying to put into is too big!");
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
try {
|
|
389
|
+
// first look into the storage, if it exists, delete it.
|
|
390
|
+
var val = this.config.storage.getItem(prefixedKey);
|
|
391
|
+
if (val) {
|
|
392
|
+
this._removeItem(prefixedKey, JSON.parse(val).byteSize);
|
|
393
|
+
}
|
|
394
|
+
// check whether the cache is full
|
|
395
|
+
if (this._isCacheFull(item.byteSize)) {
|
|
396
|
+
var validKeys = this._findValidKeys();
|
|
397
|
+
// check again and then pop out items
|
|
398
|
+
if (this._isCacheFull(item.byteSize)) {
|
|
399
|
+
var sizeToPop = this._sizeToPop(item.byteSize);
|
|
400
|
+
this._popOutItems(validKeys, sizeToPop);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
// put item in the cache
|
|
404
|
+
// may failed due to storage full
|
|
405
|
+
this._setItem(prefixedKey, item);
|
|
406
|
+
} catch (e) {
|
|
407
|
+
logger.warn("setItem failed! " + e);
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
/**
|
|
411
|
+
* Get item from cache. It will return null if item doesn’t exist or it has been expired.
|
|
412
|
+
* If you specified callback function in the options,
|
|
413
|
+
* then the function will be executed if no such item in the cache
|
|
414
|
+
* and finally put the return value into cache.
|
|
415
|
+
* Please make sure the callback function will return the value you want to put into the cache.
|
|
416
|
+
* The cache will abort output a warning:
|
|
417
|
+
* If the key is invalid
|
|
418
|
+
* If error happened with browser storage
|
|
419
|
+
*
|
|
420
|
+
* @param key - the key of the item
|
|
421
|
+
* @param {Object} [options] - the options of callback function
|
|
422
|
+
*
|
|
423
|
+
* @return - return the value of the item
|
|
424
|
+
*/
|
|
425
|
+
BrowserStorageCacheClass.prototype.getItem = function (key, options) {
|
|
426
|
+
logger.log("Get item: key is " + key + " with options " + options);
|
|
427
|
+
var ret = null;
|
|
428
|
+
var prefixedKey = this.config.keyPrefix + key;
|
|
429
|
+
if (prefixedKey === this.config.keyPrefix || prefixedKey === this.cacheCurSizeKey) {
|
|
430
|
+
logger.warn("Invalid key: should not be empty or 'CurSize'");
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
433
|
+
try {
|
|
434
|
+
ret = this.config.storage.getItem(prefixedKey);
|
|
435
|
+
if (ret != null) {
|
|
436
|
+
if (this._isExpired(prefixedKey)) {
|
|
437
|
+
// if expired, remove that item and return null
|
|
438
|
+
this._removeItem(prefixedKey, JSON.parse(ret).byteSize);
|
|
439
|
+
ret = null;
|
|
440
|
+
} else {
|
|
441
|
+
// if not expired, great, return the value and refresh it
|
|
442
|
+
var item = JSON.parse(ret);
|
|
443
|
+
item = this._refreshItem(item, prefixedKey);
|
|
444
|
+
return item.data;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
if (options && options.callback !== undefined) {
|
|
448
|
+
var val = options.callback();
|
|
449
|
+
if (val !== null) {
|
|
450
|
+
this.setItem(key, val, options);
|
|
451
|
+
}
|
|
452
|
+
return val;
|
|
453
|
+
}
|
|
454
|
+
return null;
|
|
455
|
+
} catch (e) {
|
|
456
|
+
logger.warn("getItem failed! " + e);
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
/**
|
|
461
|
+
* remove item from the cache
|
|
462
|
+
* The cache will abort output a warning:
|
|
463
|
+
* If error happened with browser storage
|
|
464
|
+
* @param key - the key of the item
|
|
465
|
+
*/
|
|
466
|
+
BrowserStorageCacheClass.prototype.removeItem = function (key) {
|
|
467
|
+
logger.log("Remove item: key is " + key);
|
|
468
|
+
var prefixedKey = this.config.keyPrefix + key;
|
|
469
|
+
if (prefixedKey === this.config.keyPrefix || prefixedKey === this.cacheCurSizeKey) {
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
try {
|
|
473
|
+
var val = this.config.storage.getItem(prefixedKey);
|
|
474
|
+
if (val) {
|
|
475
|
+
this._removeItem(prefixedKey, JSON.parse(val).byteSize);
|
|
476
|
+
}
|
|
477
|
+
} catch (e) {
|
|
478
|
+
logger.warn("removeItem failed! " + e);
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
/**
|
|
482
|
+
* clear the entire cache
|
|
483
|
+
* The cache will abort output a warning:
|
|
484
|
+
* If error happened with browser storage
|
|
485
|
+
*/
|
|
486
|
+
BrowserStorageCacheClass.prototype.clear = function () {
|
|
487
|
+
logger.log("Clear Cache");
|
|
488
|
+
var keysToRemove = [];
|
|
489
|
+
for (var i = 0; i < this.config.storage.length; i += 1) {
|
|
490
|
+
var key = this.config.storage.key(i);
|
|
491
|
+
if (key.indexOf(this.config.keyPrefix) === 0) {
|
|
492
|
+
keysToRemove.push(key);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
try {
|
|
496
|
+
for (var i = 0; i < keysToRemove.length; i += 1) {
|
|
497
|
+
this.config.storage.removeItem(keysToRemove[i]);
|
|
498
|
+
}
|
|
499
|
+
} catch (e) {
|
|
500
|
+
logger.warn("clear failed! " + e);
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
/**
|
|
504
|
+
* Return all the keys in the cache.
|
|
505
|
+
*
|
|
506
|
+
* @return - all keys in the cache
|
|
507
|
+
*/
|
|
508
|
+
BrowserStorageCacheClass.prototype.getAllKeys = function () {
|
|
509
|
+
var keys = [];
|
|
510
|
+
for (var i = 0; i < this.config.storage.length; i += 1) {
|
|
511
|
+
var key = this.config.storage.key(i);
|
|
512
|
+
if (key.indexOf(this.config.keyPrefix) === 0 && key !== this.cacheCurSizeKey) {
|
|
513
|
+
keys.push(key.substring(this.config.keyPrefix.length));
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return keys;
|
|
517
|
+
};
|
|
518
|
+
/**
|
|
519
|
+
* return the current size of the cache
|
|
520
|
+
*
|
|
521
|
+
* @return - current size of the cache
|
|
522
|
+
*/
|
|
523
|
+
BrowserStorageCacheClass.prototype.getCacheCurSize = function () {
|
|
524
|
+
var ret = this.config.storage.getItem(this.cacheCurSizeKey);
|
|
525
|
+
if (!ret) {
|
|
526
|
+
this.config.storage.setItem(this.cacheCurSizeKey, '0');
|
|
527
|
+
ret = '0';
|
|
528
|
+
}
|
|
529
|
+
return Number(ret);
|
|
530
|
+
};
|
|
531
|
+
/**
|
|
532
|
+
* Return a new instance of cache with customized configuration.
|
|
533
|
+
* @param config - the customized configuration
|
|
534
|
+
*
|
|
535
|
+
* @return - new instance of Cache
|
|
536
|
+
*/
|
|
537
|
+
BrowserStorageCacheClass.prototype.createInstance = function (config) {
|
|
538
|
+
if (!config.keyPrefix || config.keyPrefix === _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].keyPrefix) {
|
|
539
|
+
logger.error('invalid keyPrefix, setting keyPrefix with timeStamp');
|
|
540
|
+
config.keyPrefix = _Utils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"].toString();
|
|
541
|
+
}
|
|
542
|
+
return new BrowserStorageCacheClass(config);
|
|
543
|
+
};
|
|
544
|
+
return BrowserStorageCacheClass;
|
|
545
|
+
}(_StorageCache__WEBPACK_IMPORTED_MODULE_1__["StorageCache"]);
|
|
546
|
+
|
|
547
|
+
var BrowserStorageCache = new BrowserStorageCacheClass();
|
|
548
|
+
/**
|
|
549
|
+
* @deprecated use named import
|
|
550
|
+
*/
|
|
551
|
+
/* harmony default export */ __webpack_exports__["default"] = (BrowserStorageCache);
|
|
552
|
+
|
|
553
|
+
/***/ }),
|
|
554
|
+
|
|
555
|
+
/***/ "./lib-esm/InMemoryCache.js":
|
|
556
|
+
/*!**********************************!*\
|
|
557
|
+
!*** ./lib-esm/InMemoryCache.js ***!
|
|
558
|
+
\**********************************/
|
|
559
|
+
/*! exports provided: InMemoryCacheClass, InMemoryCache, default */
|
|
560
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
561
|
+
|
|
562
|
+
"use strict";
|
|
563
|
+
__webpack_require__.r(__webpack_exports__);
|
|
564
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "InMemoryCacheClass", function() { return InMemoryCacheClass; });
|
|
565
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "InMemoryCache", function() { return InMemoryCache; });
|
|
566
|
+
/* harmony import */ var _Utils__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./Utils */ "./lib-esm/Utils/index.js");
|
|
567
|
+
/* harmony import */ var _StorageCache__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./StorageCache */ "./lib-esm/StorageCache.js");
|
|
568
|
+
/* harmony import */ var _aws_amplify_core__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @aws-amplify/core */ "@aws-amplify/core");
|
|
569
|
+
/* harmony import */ var _aws_amplify_core__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_aws_amplify_core__WEBPACK_IMPORTED_MODULE_2__);
|
|
570
|
+
/*
|
|
571
|
+
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
572
|
+
*
|
|
573
|
+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
|
|
574
|
+
* the License. A copy of the License is located at
|
|
575
|
+
*
|
|
576
|
+
* http://aws.amazon.com/apache2.0/
|
|
577
|
+
*
|
|
578
|
+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
579
|
+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
|
|
580
|
+
* and limitations under the License.
|
|
581
|
+
*/
|
|
582
|
+
var __extends = undefined && undefined.__extends || function () {
|
|
583
|
+
var _extendStatics = function extendStatics(d, b) {
|
|
584
|
+
_extendStatics = Object.setPrototypeOf || {
|
|
585
|
+
__proto__: []
|
|
586
|
+
} instanceof Array && function (d, b) {
|
|
587
|
+
d.__proto__ = b;
|
|
588
|
+
} || function (d, b) {
|
|
589
|
+
for (var p in b) {
|
|
590
|
+
if (b.hasOwnProperty(p)) d[p] = b[p];
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
return _extendStatics(d, b);
|
|
594
|
+
};
|
|
595
|
+
return function (d, b) {
|
|
596
|
+
_extendStatics(d, b);
|
|
597
|
+
function __() {
|
|
598
|
+
this.constructor = d;
|
|
599
|
+
}
|
|
600
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
601
|
+
};
|
|
602
|
+
}();
|
|
603
|
+
var __values = undefined && undefined.__values || function (o) {
|
|
604
|
+
var s = typeof Symbol === "function" && Symbol.iterator,
|
|
605
|
+
m = s && o[s],
|
|
606
|
+
i = 0;
|
|
607
|
+
if (m) return m.call(o);
|
|
608
|
+
if (o && typeof o.length === "number") return {
|
|
609
|
+
next: function next() {
|
|
610
|
+
if (o && i >= o.length) o = void 0;
|
|
611
|
+
return {
|
|
612
|
+
value: o && o[i++],
|
|
613
|
+
done: !o
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
618
|
+
};
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
var logger = new _aws_amplify_core__WEBPACK_IMPORTED_MODULE_2__["ConsoleLogger"]('InMemoryCache');
|
|
623
|
+
/**
|
|
624
|
+
* Customized in-memory cache with LRU implemented
|
|
625
|
+
* @member cacheObj - object which store items
|
|
626
|
+
* @member cacheList - list of keys in the cache with LRU
|
|
627
|
+
* @member curSizeInBytes - current size of the cache
|
|
628
|
+
* @member maxPriority - max of the priority
|
|
629
|
+
* @member cacheSizeLimit - the limit of cache size
|
|
630
|
+
*/
|
|
631
|
+
var InMemoryCacheClass = /** @class */function (_super) {
|
|
632
|
+
__extends(InMemoryCacheClass, _super);
|
|
633
|
+
/**
|
|
634
|
+
* initialize the cache
|
|
635
|
+
*
|
|
636
|
+
* @param config - the configuration of the cache
|
|
637
|
+
*/
|
|
638
|
+
function InMemoryCacheClass(config) {
|
|
639
|
+
var _this = this;
|
|
640
|
+
var cacheConfig = config ? Object.assign({}, _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"], config) : _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"];
|
|
641
|
+
_this = _super.call(this, cacheConfig) || this;
|
|
642
|
+
logger.debug('now we start!');
|
|
643
|
+
_this.cacheList = [];
|
|
644
|
+
_this.curSizeInBytes = 0;
|
|
645
|
+
_this.maxPriority = 5;
|
|
646
|
+
_this.getItem = _this.getItem.bind(_this);
|
|
647
|
+
_this.setItem = _this.setItem.bind(_this);
|
|
648
|
+
_this.removeItem = _this.removeItem.bind(_this);
|
|
649
|
+
// initialize list for every priority
|
|
650
|
+
for (var i = 0; i < _this.maxPriority; i += 1) {
|
|
651
|
+
_this.cacheList[i] = new _Utils__WEBPACK_IMPORTED_MODULE_0__["CacheList"]();
|
|
652
|
+
}
|
|
653
|
+
return _this;
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* decrease current size of the cache
|
|
657
|
+
*
|
|
658
|
+
* @param amount - the amount of the cache size which needs to be decreased
|
|
659
|
+
*/
|
|
660
|
+
InMemoryCacheClass.prototype._decreaseCurSizeInBytes = function (amount) {
|
|
661
|
+
this.curSizeInBytes -= amount;
|
|
662
|
+
};
|
|
663
|
+
/**
|
|
664
|
+
* increase current size of the cache
|
|
665
|
+
*
|
|
666
|
+
* @param amount - the amount of the cache szie which need to be increased
|
|
667
|
+
*/
|
|
668
|
+
InMemoryCacheClass.prototype._increaseCurSizeInBytes = function (amount) {
|
|
669
|
+
this.curSizeInBytes += amount;
|
|
670
|
+
};
|
|
671
|
+
/**
|
|
672
|
+
* check whether item is expired
|
|
673
|
+
*
|
|
674
|
+
* @param key - the key of the item
|
|
675
|
+
*
|
|
676
|
+
* @return true if the item is expired.
|
|
677
|
+
*/
|
|
678
|
+
InMemoryCacheClass.prototype._isExpired = function (key) {
|
|
679
|
+
var text = _Utils__WEBPACK_IMPORTED_MODULE_0__["CacheObject"].getItem(key);
|
|
680
|
+
var item = JSON.parse(text);
|
|
681
|
+
if (Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"])() >= item.expires) {
|
|
682
|
+
return true;
|
|
683
|
+
}
|
|
684
|
+
return false;
|
|
685
|
+
};
|
|
686
|
+
/**
|
|
687
|
+
* delete item from cache
|
|
688
|
+
*
|
|
689
|
+
* @param prefixedKey - the key of the item
|
|
690
|
+
* @param listIdx - indicates which cache list the key belongs to
|
|
691
|
+
*/
|
|
692
|
+
InMemoryCacheClass.prototype._removeItem = function (prefixedKey, listIdx) {
|
|
693
|
+
// delete the key from the list
|
|
694
|
+
this.cacheList[listIdx].removeItem(prefixedKey);
|
|
695
|
+
// decrease the current size of the cache
|
|
696
|
+
this._decreaseCurSizeInBytes(JSON.parse(_Utils__WEBPACK_IMPORTED_MODULE_0__["CacheObject"].getItem(prefixedKey)).byteSize);
|
|
697
|
+
// finally remove the item from memory
|
|
698
|
+
_Utils__WEBPACK_IMPORTED_MODULE_0__["CacheObject"].removeItem(prefixedKey);
|
|
699
|
+
};
|
|
700
|
+
/**
|
|
701
|
+
* put item into cache
|
|
702
|
+
*
|
|
703
|
+
* @param prefixedKey - the key of the item
|
|
704
|
+
* @param itemData - the value of the item
|
|
705
|
+
* @param itemSizeInBytes - the byte size of the item
|
|
706
|
+
* @param listIdx - indicates which cache list the key belongs to
|
|
707
|
+
*/
|
|
708
|
+
InMemoryCacheClass.prototype._setItem = function (prefixedKey, item, listIdx) {
|
|
709
|
+
// insert the key into the list
|
|
710
|
+
this.cacheList[listIdx].insertItem(prefixedKey);
|
|
711
|
+
// increase the current size of the cache
|
|
712
|
+
this._increaseCurSizeInBytes(item.byteSize);
|
|
713
|
+
// finally add the item into memory
|
|
714
|
+
_Utils__WEBPACK_IMPORTED_MODULE_0__["CacheObject"].setItem(prefixedKey, JSON.stringify(item));
|
|
715
|
+
};
|
|
716
|
+
/**
|
|
717
|
+
* see whether cache is full
|
|
718
|
+
*
|
|
719
|
+
* @param itemSize
|
|
720
|
+
*
|
|
721
|
+
* @return true if cache is full
|
|
722
|
+
*/
|
|
723
|
+
InMemoryCacheClass.prototype._isCacheFull = function (itemSize) {
|
|
724
|
+
return this.curSizeInBytes + itemSize > this.config.capacityInBytes;
|
|
725
|
+
};
|
|
726
|
+
/**
|
|
727
|
+
* check whether the cache contains the key
|
|
728
|
+
*
|
|
729
|
+
* @param key
|
|
730
|
+
*/
|
|
731
|
+
InMemoryCacheClass.prototype.containsKey = function (key) {
|
|
732
|
+
var prefixedKey = this.config.keyPrefix + key;
|
|
733
|
+
for (var i = 0; i < this.maxPriority; i += 1) {
|
|
734
|
+
if (this.cacheList[i].containsKey(prefixedKey)) {
|
|
735
|
+
return i + 1;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
return -1;
|
|
739
|
+
};
|
|
740
|
+
/**
|
|
741
|
+
* * Set item into cache. You can put number, string, boolean or object.
|
|
742
|
+
* The cache will first check whether has the same key.
|
|
743
|
+
* If it has, it will delete the old item and then put the new item in
|
|
744
|
+
* The cache will pop out items if it is full
|
|
745
|
+
* You can specify the cache item options. The cache will abort and output a warning:
|
|
746
|
+
* If the key is invalid
|
|
747
|
+
* If the size of the item exceeds itemMaxSize.
|
|
748
|
+
* If the value is undefined
|
|
749
|
+
* If incorrect cache item configuration
|
|
750
|
+
* If error happened with browser storage
|
|
751
|
+
*
|
|
752
|
+
* @param key - the key of the item
|
|
753
|
+
* @param value - the value of the item
|
|
754
|
+
* @param options - optional, the specified meta-data
|
|
755
|
+
*
|
|
756
|
+
* @throws if the item is too big which exceeds the limit of single item size
|
|
757
|
+
* @throws if the key is invalid
|
|
758
|
+
*/
|
|
759
|
+
InMemoryCacheClass.prototype.setItem = function (key, value, options) {
|
|
760
|
+
var prefixedKey = this.config.keyPrefix + key;
|
|
761
|
+
// invalid keys
|
|
762
|
+
if (prefixedKey === this.config.keyPrefix || prefixedKey === this.cacheCurSizeKey) {
|
|
763
|
+
logger.warn("Invalid key: should not be empty or 'CurSize'");
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
if (typeof value === 'undefined') {
|
|
767
|
+
logger.warn("The value of item should not be undefined!");
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
var cacheItemOptions = {
|
|
771
|
+
priority: options && options.priority !== undefined ? options.priority : this.config.defaultPriority,
|
|
772
|
+
expires: options && options.expires !== undefined ? options.expires : this.config.defaultTTL + Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"])()
|
|
773
|
+
};
|
|
774
|
+
if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
|
|
775
|
+
logger.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");
|
|
776
|
+
return;
|
|
777
|
+
}
|
|
778
|
+
var item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
|
|
779
|
+
// check wether this item is too big;
|
|
780
|
+
if (item.byteSize > this.config.itemMaxSize) {
|
|
781
|
+
logger.warn("Item with key: " + key + " you are trying to put into is too big!");
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
// if key already in the cache, then delete it.
|
|
785
|
+
var presentKeyPrio = this.containsKey(key);
|
|
786
|
+
if (presentKeyPrio !== -1) {
|
|
787
|
+
this._removeItem(prefixedKey, presentKeyPrio - 1);
|
|
788
|
+
}
|
|
789
|
+
// pop out items in the cache when cache is full based on LRU
|
|
790
|
+
// first start from lowest priority cache list
|
|
791
|
+
var cacheListIdx = this.maxPriority - 1;
|
|
792
|
+
while (this._isCacheFull(item.byteSize) && cacheListIdx >= 0) {
|
|
793
|
+
if (!this.cacheList[cacheListIdx].isEmpty()) {
|
|
794
|
+
var popedItemKey = this.cacheList[cacheListIdx].getLastItem();
|
|
795
|
+
this._removeItem(popedItemKey, cacheListIdx);
|
|
796
|
+
} else {
|
|
797
|
+
cacheListIdx -= 1;
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
this._setItem(prefixedKey, item, Number(item.priority) - 1);
|
|
801
|
+
};
|
|
802
|
+
/**
|
|
803
|
+
* Get item from cache. It will return null if item doesn’t exist or it has been expired.
|
|
804
|
+
* If you specified callback function in the options,
|
|
805
|
+
* then the function will be executed if no such item in the cache
|
|
806
|
+
* and finally put the return value into cache.
|
|
807
|
+
* Please make sure the callback function will return the value you want to put into the cache.
|
|
808
|
+
* The cache will abort output a warning:
|
|
809
|
+
* If the key is invalid
|
|
810
|
+
*
|
|
811
|
+
* @param key - the key of the item
|
|
812
|
+
* @param options - the options of callback function
|
|
813
|
+
*/
|
|
814
|
+
InMemoryCacheClass.prototype.getItem = function (key, options) {
|
|
815
|
+
var ret = null;
|
|
816
|
+
var prefixedKey = this.config.keyPrefix + key;
|
|
817
|
+
if (prefixedKey === this.config.keyPrefix || prefixedKey === this.cacheCurSizeKey) {
|
|
818
|
+
logger.warn("Invalid key: should not be empty or 'CurSize'");
|
|
819
|
+
return null;
|
|
820
|
+
}
|
|
821
|
+
// check whether it's in the cachelist
|
|
822
|
+
var presentKeyPrio = this.containsKey(key);
|
|
823
|
+
if (presentKeyPrio !== -1) {
|
|
824
|
+
if (this._isExpired(prefixedKey)) {
|
|
825
|
+
// if expired, remove that item and return null
|
|
826
|
+
this._removeItem(prefixedKey, presentKeyPrio - 1);
|
|
827
|
+
} else {
|
|
828
|
+
// if not expired, great, return the value and refresh it
|
|
829
|
+
ret = _Utils__WEBPACK_IMPORTED_MODULE_0__["CacheObject"].getItem(prefixedKey);
|
|
830
|
+
var item = JSON.parse(ret);
|
|
831
|
+
this.cacheList[item.priority - 1].refresh(prefixedKey);
|
|
832
|
+
return item.data;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
if (options && options.callback !== undefined) {
|
|
836
|
+
var val = options.callback();
|
|
837
|
+
if (val !== null) {
|
|
838
|
+
this.setItem(key, val, options);
|
|
839
|
+
}
|
|
840
|
+
return val;
|
|
841
|
+
}
|
|
842
|
+
return null;
|
|
843
|
+
};
|
|
844
|
+
/**
|
|
845
|
+
* remove item from the cache
|
|
846
|
+
*
|
|
847
|
+
* @param key - the key of the item
|
|
848
|
+
*/
|
|
849
|
+
InMemoryCacheClass.prototype.removeItem = function (key) {
|
|
850
|
+
var prefixedKey = this.config.keyPrefix + key;
|
|
851
|
+
// check if the key is in the cache
|
|
852
|
+
var presentKeyPrio = this.containsKey(key);
|
|
853
|
+
if (presentKeyPrio !== -1) {
|
|
854
|
+
this._removeItem(prefixedKey, presentKeyPrio - 1);
|
|
855
|
+
}
|
|
856
|
+
};
|
|
857
|
+
/**
|
|
858
|
+
* clear the entire cache
|
|
859
|
+
*/
|
|
860
|
+
InMemoryCacheClass.prototype.clear = function () {
|
|
861
|
+
var e_1, _a;
|
|
862
|
+
for (var i = 0; i < this.maxPriority; i += 1) {
|
|
863
|
+
try {
|
|
864
|
+
for (var _b = (e_1 = void 0, __values(this.cacheList[i].getKeys())), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
865
|
+
var key = _c.value;
|
|
866
|
+
this._removeItem(key, i);
|
|
867
|
+
}
|
|
868
|
+
} catch (e_1_1) {
|
|
869
|
+
e_1 = {
|
|
870
|
+
error: e_1_1
|
|
871
|
+
};
|
|
872
|
+
} finally {
|
|
873
|
+
try {
|
|
874
|
+
if (_c && !_c.done && (_a = _b["return"])) _a.call(_b);
|
|
875
|
+
} finally {
|
|
876
|
+
if (e_1) throw e_1.error;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
};
|
|
881
|
+
/**
|
|
882
|
+
* Return all the keys in the cache.
|
|
883
|
+
*/
|
|
884
|
+
InMemoryCacheClass.prototype.getAllKeys = function () {
|
|
885
|
+
var e_2, _a;
|
|
886
|
+
var keys = [];
|
|
887
|
+
for (var i = 0; i < this.maxPriority; i += 1) {
|
|
888
|
+
try {
|
|
889
|
+
for (var _b = (e_2 = void 0, __values(this.cacheList[i].getKeys())), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
890
|
+
var key = _c.value;
|
|
891
|
+
keys.push(key.substring(this.config.keyPrefix.length));
|
|
892
|
+
}
|
|
893
|
+
} catch (e_2_1) {
|
|
894
|
+
e_2 = {
|
|
895
|
+
error: e_2_1
|
|
896
|
+
};
|
|
897
|
+
} finally {
|
|
898
|
+
try {
|
|
899
|
+
if (_c && !_c.done && (_a = _b["return"])) _a.call(_b);
|
|
900
|
+
} finally {
|
|
901
|
+
if (e_2) throw e_2.error;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
return keys;
|
|
906
|
+
};
|
|
907
|
+
/**
|
|
908
|
+
* return the current size of the cache
|
|
909
|
+
*
|
|
910
|
+
* @return the current size of the cache
|
|
911
|
+
*/
|
|
912
|
+
InMemoryCacheClass.prototype.getCacheCurSize = function () {
|
|
913
|
+
return this.curSizeInBytes;
|
|
914
|
+
};
|
|
915
|
+
/**
|
|
916
|
+
* Return a new instance of cache with customized configuration.
|
|
917
|
+
* @param config - the customized configuration
|
|
918
|
+
*/
|
|
919
|
+
InMemoryCacheClass.prototype.createInstance = function (config) {
|
|
920
|
+
return new InMemoryCacheClass(config);
|
|
921
|
+
};
|
|
922
|
+
return InMemoryCacheClass;
|
|
923
|
+
}(_StorageCache__WEBPACK_IMPORTED_MODULE_1__["StorageCache"]);
|
|
924
|
+
|
|
925
|
+
var InMemoryCache = new InMemoryCacheClass();
|
|
926
|
+
/**
|
|
927
|
+
* @deprecated use named import
|
|
928
|
+
*/
|
|
929
|
+
/* harmony default export */ __webpack_exports__["default"] = (InMemoryCache);
|
|
930
|
+
|
|
931
|
+
/***/ }),
|
|
932
|
+
|
|
933
|
+
/***/ "./lib-esm/StorageCache.js":
|
|
934
|
+
/*!*********************************!*\
|
|
935
|
+
!*** ./lib-esm/StorageCache.js ***!
|
|
936
|
+
\*********************************/
|
|
937
|
+
/*! exports provided: StorageCache, default */
|
|
938
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
939
|
+
|
|
940
|
+
"use strict";
|
|
941
|
+
__webpack_require__.r(__webpack_exports__);
|
|
942
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "StorageCache", function() { return StorageCache; });
|
|
943
|
+
/* harmony import */ var _Utils__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./Utils */ "./lib-esm/Utils/index.js");
|
|
944
|
+
/* harmony import */ var _aws_amplify_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @aws-amplify/core */ "@aws-amplify/core");
|
|
945
|
+
/* harmony import */ var _aws_amplify_core__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_aws_amplify_core__WEBPACK_IMPORTED_MODULE_1__);
|
|
946
|
+
function _typeof(obj) {
|
|
947
|
+
"@babel/helpers - typeof";
|
|
948
|
+
|
|
949
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
|
|
950
|
+
return typeof obj;
|
|
951
|
+
} : function (obj) {
|
|
952
|
+
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
|
953
|
+
}, _typeof(obj);
|
|
954
|
+
}
|
|
955
|
+
/*
|
|
956
|
+
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
957
|
+
*
|
|
958
|
+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
|
|
959
|
+
* the License. A copy of the License is located at
|
|
960
|
+
*
|
|
961
|
+
* http://aws.amazon.com/apache2.0/
|
|
962
|
+
*
|
|
963
|
+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
964
|
+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
|
|
965
|
+
* and limitations under the License.
|
|
966
|
+
*/
|
|
967
|
+
|
|
968
|
+
|
|
969
|
+
var logger = new _aws_amplify_core__WEBPACK_IMPORTED_MODULE_1__["ConsoleLogger"]('StorageCache');
|
|
970
|
+
/**
|
|
971
|
+
* Initialization of the cache
|
|
972
|
+
*
|
|
973
|
+
*/
|
|
974
|
+
var StorageCache = /** @class */function () {
|
|
975
|
+
/**
|
|
976
|
+
* Initialize the cache
|
|
977
|
+
* @param config - the configuration of the cache
|
|
978
|
+
*/
|
|
979
|
+
function StorageCache(config) {
|
|
980
|
+
this.config = Object.assign({}, config);
|
|
981
|
+
this.cacheCurSizeKey = this.config.keyPrefix + 'CurSize';
|
|
982
|
+
this.checkConfig();
|
|
983
|
+
}
|
|
984
|
+
StorageCache.prototype.getModuleName = function () {
|
|
985
|
+
return 'Cache';
|
|
986
|
+
};
|
|
987
|
+
StorageCache.prototype.checkConfig = function () {
|
|
988
|
+
// check configuration
|
|
989
|
+
if (!Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["isInteger"])(this.config.capacityInBytes)) {
|
|
990
|
+
logger.error('Invalid parameter: capacityInBytes. It should be an Integer. Setting back to default.');
|
|
991
|
+
this.config.capacityInBytes = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].capacityInBytes;
|
|
992
|
+
}
|
|
993
|
+
if (!Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["isInteger"])(this.config.itemMaxSize)) {
|
|
994
|
+
logger.error('Invalid parameter: itemMaxSize. It should be an Integer. Setting back to default.');
|
|
995
|
+
this.config.itemMaxSize = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].itemMaxSize;
|
|
996
|
+
}
|
|
997
|
+
if (!Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["isInteger"])(this.config.defaultTTL)) {
|
|
998
|
+
logger.error('Invalid parameter: defaultTTL. It should be an Integer. Setting back to default.');
|
|
999
|
+
this.config.defaultTTL = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].defaultTTL;
|
|
1000
|
+
}
|
|
1001
|
+
if (!Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["isInteger"])(this.config.defaultPriority)) {
|
|
1002
|
+
logger.error('Invalid parameter: defaultPriority. It should be an Integer. Setting back to default.');
|
|
1003
|
+
this.config.defaultPriority = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].defaultPriority;
|
|
1004
|
+
}
|
|
1005
|
+
if (this.config.itemMaxSize > this.config.capacityInBytes) {
|
|
1006
|
+
logger.error('Invalid parameter: itemMaxSize. It should be smaller than capacityInBytes. Setting back to default.');
|
|
1007
|
+
this.config.itemMaxSize = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].itemMaxSize;
|
|
1008
|
+
}
|
|
1009
|
+
if (this.config.defaultPriority > 5 || this.config.defaultPriority < 1) {
|
|
1010
|
+
logger.error('Invalid parameter: defaultPriority. It should be between 1 and 5. Setting back to default.');
|
|
1011
|
+
this.config.defaultPriority = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].defaultPriority;
|
|
1012
|
+
}
|
|
1013
|
+
if (Number(this.config.warningThreshold) > 1 || Number(this.config.warningThreshold) < 0) {
|
|
1014
|
+
logger.error('Invalid parameter: warningThreshold. It should be between 0 and 1. Setting back to default.');
|
|
1015
|
+
this.config.warningThreshold = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].warningThreshold;
|
|
1016
|
+
}
|
|
1017
|
+
// set 5MB limit
|
|
1018
|
+
var cacheLimit = 5 * 1024 * 1024;
|
|
1019
|
+
if (this.config.capacityInBytes > cacheLimit) {
|
|
1020
|
+
logger.error('Cache Capacity should be less than 5MB. Setting back to default. Setting back to default.');
|
|
1021
|
+
this.config.capacityInBytes = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].capacityInBytes;
|
|
1022
|
+
}
|
|
1023
|
+
};
|
|
1024
|
+
/**
|
|
1025
|
+
* produce a JSON object with meta-data and data value
|
|
1026
|
+
* @param value - the value of the item
|
|
1027
|
+
* @param options - optional, the specified meta-data
|
|
1028
|
+
*
|
|
1029
|
+
* @return - the item which has the meta-data and the value
|
|
1030
|
+
*/
|
|
1031
|
+
StorageCache.prototype.fillCacheItem = function (key, value, options) {
|
|
1032
|
+
var ret = {
|
|
1033
|
+
key: key,
|
|
1034
|
+
data: value,
|
|
1035
|
+
timestamp: Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"])(),
|
|
1036
|
+
visitedTime: Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"])(),
|
|
1037
|
+
priority: options.priority,
|
|
1038
|
+
expires: options.expires,
|
|
1039
|
+
type: _typeof(value),
|
|
1040
|
+
byteSize: 0
|
|
1041
|
+
};
|
|
1042
|
+
ret.byteSize = Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getByteLength"])(JSON.stringify(ret));
|
|
1043
|
+
// for accurate size
|
|
1044
|
+
ret.byteSize = Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getByteLength"])(JSON.stringify(ret));
|
|
1045
|
+
return ret;
|
|
1046
|
+
};
|
|
1047
|
+
/**
|
|
1048
|
+
* set cache with customized configuration
|
|
1049
|
+
* @param config - customized configuration
|
|
1050
|
+
*
|
|
1051
|
+
* @return - the current configuration
|
|
1052
|
+
*/
|
|
1053
|
+
StorageCache.prototype.configure = function (config) {
|
|
1054
|
+
if (!config) {
|
|
1055
|
+
return this.config;
|
|
1056
|
+
}
|
|
1057
|
+
if (config.keyPrefix) {
|
|
1058
|
+
logger.warn("Don't try to configure keyPrefix!");
|
|
1059
|
+
}
|
|
1060
|
+
this.config = Object.assign({}, this.config, config, config.Cache);
|
|
1061
|
+
this.checkConfig();
|
|
1062
|
+
return this.config;
|
|
1063
|
+
};
|
|
1064
|
+
return StorageCache;
|
|
1065
|
+
}();
|
|
1066
|
+
|
|
1067
|
+
/**
|
|
1068
|
+
* @deprecated use named import
|
|
1069
|
+
*/
|
|
1070
|
+
/* harmony default export */ __webpack_exports__["default"] = (StorageCache);
|
|
1071
|
+
|
|
1072
|
+
/***/ }),
|
|
1073
|
+
|
|
1074
|
+
/***/ "./lib-esm/Utils/CacheList.js":
|
|
1075
|
+
/*!************************************!*\
|
|
1076
|
+
!*** ./lib-esm/Utils/CacheList.js ***!
|
|
1077
|
+
\************************************/
|
|
1078
|
+
/*! exports provided: default */
|
|
1079
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1080
|
+
|
|
1081
|
+
"use strict";
|
|
1082
|
+
__webpack_require__.r(__webpack_exports__);
|
|
1083
|
+
/*
|
|
1084
|
+
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
1085
|
+
*
|
|
1086
|
+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
|
|
1087
|
+
* the License. A copy of the License is located at
|
|
1088
|
+
*
|
|
1089
|
+
* http://aws.amazon.com/apache2.0/
|
|
1090
|
+
*
|
|
1091
|
+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
1092
|
+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
|
|
1093
|
+
* and limitations under the License.
|
|
1094
|
+
*/
|
|
1095
|
+
var __values = undefined && undefined.__values || function (o) {
|
|
1096
|
+
var s = typeof Symbol === "function" && Symbol.iterator,
|
|
1097
|
+
m = s && o[s],
|
|
1098
|
+
i = 0;
|
|
1099
|
+
if (m) return m.call(o);
|
|
1100
|
+
if (o && typeof o.length === "number") return {
|
|
1101
|
+
next: function next() {
|
|
1102
|
+
if (o && i >= o.length) o = void 0;
|
|
1103
|
+
return {
|
|
1104
|
+
value: o && o[i++],
|
|
1105
|
+
done: !o
|
|
1106
|
+
};
|
|
1107
|
+
}
|
|
1108
|
+
};
|
|
1109
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
1110
|
+
};
|
|
1111
|
+
var DoubleLinkedNode = /** @class */function () {
|
|
1112
|
+
function DoubleLinkedNode(keyVal) {
|
|
1113
|
+
this.key = keyVal ? keyVal : '';
|
|
1114
|
+
this.prevNode = null;
|
|
1115
|
+
this.nextNode = null;
|
|
1116
|
+
}
|
|
1117
|
+
return DoubleLinkedNode;
|
|
1118
|
+
}();
|
|
1119
|
+
/**
|
|
1120
|
+
* double linked list plus a hash table inside
|
|
1121
|
+
* each key in the cache stored as a node in the list
|
|
1122
|
+
* recently visited node will be rotated to the head
|
|
1123
|
+
* so the Last Recently Visited node will be at the tail
|
|
1124
|
+
*
|
|
1125
|
+
* @member head - dummy head of the linked list
|
|
1126
|
+
* @member tail - dummy tail of the linked list
|
|
1127
|
+
* @member hashtable - the hashtable which maps cache key to list node
|
|
1128
|
+
* @member length - length of the list
|
|
1129
|
+
*/
|
|
1130
|
+
var CacheList = /** @class */function () {
|
|
1131
|
+
/**
|
|
1132
|
+
* initialization
|
|
1133
|
+
*/
|
|
1134
|
+
function CacheList() {
|
|
1135
|
+
this.head = new DoubleLinkedNode();
|
|
1136
|
+
this.tail = new DoubleLinkedNode();
|
|
1137
|
+
this.hashtable = {};
|
|
1138
|
+
this.length = 0;
|
|
1139
|
+
this.head.nextNode = this.tail;
|
|
1140
|
+
this.tail.prevNode = this.head;
|
|
1141
|
+
}
|
|
1142
|
+
/**
|
|
1143
|
+
* insert node to the head of the list
|
|
1144
|
+
*
|
|
1145
|
+
* @param node
|
|
1146
|
+
*/
|
|
1147
|
+
CacheList.prototype.insertNodeToHead = function (node) {
|
|
1148
|
+
var tmp = this.head.nextNode;
|
|
1149
|
+
this.head.nextNode = node;
|
|
1150
|
+
node.nextNode = tmp;
|
|
1151
|
+
node.prevNode = this.head;
|
|
1152
|
+
tmp.prevNode = node;
|
|
1153
|
+
this.length = this.length + 1;
|
|
1154
|
+
};
|
|
1155
|
+
/**
|
|
1156
|
+
* remove node
|
|
1157
|
+
*
|
|
1158
|
+
* @param node
|
|
1159
|
+
*/
|
|
1160
|
+
CacheList.prototype.removeNode = function (node) {
|
|
1161
|
+
node.prevNode.nextNode = node.nextNode;
|
|
1162
|
+
node.nextNode.prevNode = node.prevNode;
|
|
1163
|
+
node.prevNode = null;
|
|
1164
|
+
node.nextNode = null;
|
|
1165
|
+
this.length = this.length - 1;
|
|
1166
|
+
};
|
|
1167
|
+
/**
|
|
1168
|
+
* @return true if list is empty
|
|
1169
|
+
*/
|
|
1170
|
+
CacheList.prototype.isEmpty = function () {
|
|
1171
|
+
return this.length === 0;
|
|
1172
|
+
};
|
|
1173
|
+
/**
|
|
1174
|
+
* refresh node so it is rotated to the head
|
|
1175
|
+
*
|
|
1176
|
+
* @param key - key of the node
|
|
1177
|
+
*/
|
|
1178
|
+
CacheList.prototype.refresh = function (key) {
|
|
1179
|
+
var node = this.hashtable[key];
|
|
1180
|
+
this.removeNode(node);
|
|
1181
|
+
this.insertNodeToHead(node);
|
|
1182
|
+
};
|
|
1183
|
+
/**
|
|
1184
|
+
* insert new node to the head and add it in the hashtable
|
|
1185
|
+
*
|
|
1186
|
+
* @param key - the key of the node
|
|
1187
|
+
*/
|
|
1188
|
+
CacheList.prototype.insertItem = function (key) {
|
|
1189
|
+
var node = new DoubleLinkedNode(key);
|
|
1190
|
+
this.hashtable[key] = node;
|
|
1191
|
+
this.insertNodeToHead(node);
|
|
1192
|
+
};
|
|
1193
|
+
/**
|
|
1194
|
+
* @return the LAST Recently Visited key
|
|
1195
|
+
*/
|
|
1196
|
+
CacheList.prototype.getLastItem = function () {
|
|
1197
|
+
return this.tail.prevNode.key;
|
|
1198
|
+
};
|
|
1199
|
+
/**
|
|
1200
|
+
* remove the cache key from the list and hashtable
|
|
1201
|
+
* @param key - the key of the node
|
|
1202
|
+
*/
|
|
1203
|
+
CacheList.prototype.removeItem = function (key) {
|
|
1204
|
+
var removedItem = this.hashtable[key];
|
|
1205
|
+
this.removeNode(removedItem);
|
|
1206
|
+
delete this.hashtable[key];
|
|
1207
|
+
};
|
|
1208
|
+
/**
|
|
1209
|
+
* @return length of the list
|
|
1210
|
+
*/
|
|
1211
|
+
CacheList.prototype.getSize = function () {
|
|
1212
|
+
return this.length;
|
|
1213
|
+
};
|
|
1214
|
+
/**
|
|
1215
|
+
* @return true if the key is in the hashtable
|
|
1216
|
+
* @param key
|
|
1217
|
+
*/
|
|
1218
|
+
CacheList.prototype.containsKey = function (key) {
|
|
1219
|
+
return key in this.hashtable;
|
|
1220
|
+
};
|
|
1221
|
+
/**
|
|
1222
|
+
* clean up the list and hashtable
|
|
1223
|
+
*/
|
|
1224
|
+
CacheList.prototype.clearList = function () {
|
|
1225
|
+
var e_1, _a;
|
|
1226
|
+
try {
|
|
1227
|
+
for (var _b = __values(Object.keys(this.hashtable)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
1228
|
+
var key = _c.value;
|
|
1229
|
+
if (this.hashtable.hasOwnProperty(key)) {
|
|
1230
|
+
delete this.hashtable[key];
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
} catch (e_1_1) {
|
|
1234
|
+
e_1 = {
|
|
1235
|
+
error: e_1_1
|
|
1236
|
+
};
|
|
1237
|
+
} finally {
|
|
1238
|
+
try {
|
|
1239
|
+
if (_c && !_c.done && (_a = _b["return"])) _a.call(_b);
|
|
1240
|
+
} finally {
|
|
1241
|
+
if (e_1) throw e_1.error;
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
this.head.nextNode = this.tail;
|
|
1245
|
+
this.tail.prevNode = this.head;
|
|
1246
|
+
this.length = 0;
|
|
1247
|
+
};
|
|
1248
|
+
/**
|
|
1249
|
+
* @return all keys in the hashtable
|
|
1250
|
+
*/
|
|
1251
|
+
CacheList.prototype.getKeys = function () {
|
|
1252
|
+
return Object.keys(this.hashtable);
|
|
1253
|
+
};
|
|
1254
|
+
/**
|
|
1255
|
+
* mainly for test
|
|
1256
|
+
*
|
|
1257
|
+
* @param key
|
|
1258
|
+
* @return true if key is the head node
|
|
1259
|
+
*/
|
|
1260
|
+
CacheList.prototype.isHeadNode = function (key) {
|
|
1261
|
+
var node = this.hashtable[key];
|
|
1262
|
+
return node.prevNode === this.head;
|
|
1263
|
+
};
|
|
1264
|
+
/**
|
|
1265
|
+
* mainly for test
|
|
1266
|
+
*
|
|
1267
|
+
* @param key
|
|
1268
|
+
* @return true if key is the tail node
|
|
1269
|
+
*/
|
|
1270
|
+
CacheList.prototype.isTailNode = function (key) {
|
|
1271
|
+
var node = this.hashtable[key];
|
|
1272
|
+
return node.nextNode === this.tail;
|
|
1273
|
+
};
|
|
1274
|
+
return CacheList;
|
|
1275
|
+
}();
|
|
1276
|
+
/* harmony default export */ __webpack_exports__["default"] = (CacheList);
|
|
1277
|
+
|
|
1278
|
+
/***/ }),
|
|
1279
|
+
|
|
1280
|
+
/***/ "./lib-esm/Utils/CacheUtils.js":
|
|
1281
|
+
/*!*************************************!*\
|
|
1282
|
+
!*** ./lib-esm/Utils/CacheUtils.js ***!
|
|
1283
|
+
\*************************************/
|
|
1284
|
+
/*! exports provided: defaultConfig, getByteLength, getCurrTime, isInteger, CacheObject */
|
|
1285
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1286
|
+
|
|
1287
|
+
"use strict";
|
|
1288
|
+
__webpack_require__.r(__webpack_exports__);
|
|
1289
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "defaultConfig", function() { return defaultConfig; });
|
|
1290
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "getByteLength", function() { return getByteLength; });
|
|
1291
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "getCurrTime", function() { return getCurrTime; });
|
|
1292
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "isInteger", function() { return isInteger; });
|
|
1293
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "CacheObject", function() { return CacheObject; });
|
|
1294
|
+
/* harmony import */ var _aws_amplify_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @aws-amplify/core */ "@aws-amplify/core");
|
|
1295
|
+
/* harmony import */ var _aws_amplify_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_aws_amplify_core__WEBPACK_IMPORTED_MODULE_0__);
|
|
1296
|
+
/*
|
|
1297
|
+
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
1298
|
+
*
|
|
1299
|
+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
|
|
1300
|
+
* the License. A copy of the License is located at
|
|
1301
|
+
*
|
|
1302
|
+
* http://aws.amazon.com/apache2.0/
|
|
1303
|
+
*
|
|
1304
|
+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
1305
|
+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
|
|
1306
|
+
* and limitations under the License.
|
|
1307
|
+
*/
|
|
1308
|
+
|
|
1309
|
+
/**
|
|
1310
|
+
* Default cache config
|
|
1311
|
+
*/
|
|
1312
|
+
var defaultConfig = {
|
|
1313
|
+
keyPrefix: 'aws-amplify-cache',
|
|
1314
|
+
capacityInBytes: 1048576,
|
|
1315
|
+
itemMaxSize: 210000,
|
|
1316
|
+
defaultTTL: 259200000,
|
|
1317
|
+
defaultPriority: 5,
|
|
1318
|
+
warningThreshold: 0.8,
|
|
1319
|
+
// the storage helper will check if localStorage exists,
|
|
1320
|
+
// if not, will use a in-memory object instead
|
|
1321
|
+
storage: new _aws_amplify_core__WEBPACK_IMPORTED_MODULE_0__["StorageHelper"]().getStorage()
|
|
1322
|
+
};
|
|
1323
|
+
/**
|
|
1324
|
+
* return the byte size of the string
|
|
1325
|
+
* @param str
|
|
1326
|
+
*/
|
|
1327
|
+
function getByteLength(str) {
|
|
1328
|
+
var ret = 0;
|
|
1329
|
+
ret = str.length;
|
|
1330
|
+
for (var i = str.length; i >= 0; i -= 1) {
|
|
1331
|
+
var charCode = str.charCodeAt(i);
|
|
1332
|
+
if (charCode > 0x7f && charCode <= 0x7ff) {
|
|
1333
|
+
ret += 1;
|
|
1334
|
+
} else if (charCode > 0x7ff && charCode <= 0xffff) {
|
|
1335
|
+
ret += 2;
|
|
1336
|
+
}
|
|
1337
|
+
// trail surrogate
|
|
1338
|
+
if (charCode >= 0xdc00 && charCode <= 0xdfff) {
|
|
1339
|
+
i -= 1;
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
return ret;
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* get current time
|
|
1346
|
+
*/
|
|
1347
|
+
function getCurrTime() {
|
|
1348
|
+
var currTime = new Date();
|
|
1349
|
+
return currTime.getTime();
|
|
1350
|
+
}
|
|
1351
|
+
/**
|
|
1352
|
+
* check if passed value is an integer
|
|
1353
|
+
*/
|
|
1354
|
+
function isInteger(value) {
|
|
1355
|
+
if (Number.isInteger) {
|
|
1356
|
+
return Number.isInteger(value);
|
|
1357
|
+
}
|
|
1358
|
+
return _isInteger(value);
|
|
1359
|
+
}
|
|
1360
|
+
function _isInteger(value) {
|
|
1361
|
+
return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
|
|
1362
|
+
}
|
|
1363
|
+
/**
|
|
1364
|
+
* provide an object as the in-memory cache
|
|
1365
|
+
*/
|
|
1366
|
+
var store = {};
|
|
1367
|
+
var CacheObject = /** @class */function () {
|
|
1368
|
+
function CacheObject() {}
|
|
1369
|
+
CacheObject.clear = function () {
|
|
1370
|
+
store = {};
|
|
1371
|
+
};
|
|
1372
|
+
CacheObject.getItem = function (key) {
|
|
1373
|
+
return store[key] || null;
|
|
1374
|
+
};
|
|
1375
|
+
CacheObject.setItem = function (key, value) {
|
|
1376
|
+
store[key] = value;
|
|
1377
|
+
};
|
|
1378
|
+
CacheObject.removeItem = function (key) {
|
|
1379
|
+
delete store[key];
|
|
1380
|
+
};
|
|
1381
|
+
return CacheObject;
|
|
1382
|
+
}();
|
|
1383
|
+
|
|
1384
|
+
|
|
1385
|
+
/***/ }),
|
|
1386
|
+
|
|
1387
|
+
/***/ "./lib-esm/Utils/index.js":
|
|
1388
|
+
/*!********************************!*\
|
|
1389
|
+
!*** ./lib-esm/Utils/index.js ***!
|
|
1390
|
+
\********************************/
|
|
1391
|
+
/*! exports provided: defaultConfig, getByteLength, getCurrTime, isInteger, CacheObject, CacheList */
|
|
1392
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1393
|
+
|
|
1394
|
+
"use strict";
|
|
1395
|
+
__webpack_require__.r(__webpack_exports__);
|
|
1396
|
+
/* harmony import */ var _CacheUtils__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./CacheUtils */ "./lib-esm/Utils/CacheUtils.js");
|
|
1397
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "defaultConfig", function() { return _CacheUtils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"]; });
|
|
1398
|
+
|
|
1399
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "getByteLength", function() { return _CacheUtils__WEBPACK_IMPORTED_MODULE_0__["getByteLength"]; });
|
|
1400
|
+
|
|
1401
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "getCurrTime", function() { return _CacheUtils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"]; });
|
|
1402
|
+
|
|
1403
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "isInteger", function() { return _CacheUtils__WEBPACK_IMPORTED_MODULE_0__["isInteger"]; });
|
|
1404
|
+
|
|
1405
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "CacheObject", function() { return _CacheUtils__WEBPACK_IMPORTED_MODULE_0__["CacheObject"]; });
|
|
1406
|
+
|
|
1407
|
+
/* harmony import */ var _CacheList__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./CacheList */ "./lib-esm/Utils/CacheList.js");
|
|
1408
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "CacheList", function() { return _CacheList__WEBPACK_IMPORTED_MODULE_1__["default"]; });
|
|
1409
|
+
|
|
1410
|
+
/*
|
|
1411
|
+
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
1412
|
+
*
|
|
1413
|
+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
|
|
1414
|
+
* the License. A copy of the License is located at
|
|
1415
|
+
*
|
|
1416
|
+
* http://aws.amazon.com/apache2.0/
|
|
1417
|
+
*
|
|
1418
|
+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
1419
|
+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
|
|
1420
|
+
* and limitations under the License.
|
|
1421
|
+
*/
|
|
1422
|
+
|
|
1423
|
+
|
|
1424
|
+
|
|
1425
|
+
/***/ }),
|
|
1426
|
+
|
|
1427
|
+
/***/ "./lib-esm/index.js":
|
|
1428
|
+
/*!**************************!*\
|
|
1429
|
+
!*** ./lib-esm/index.js ***!
|
|
1430
|
+
\**************************/
|
|
1431
|
+
/*! exports provided: BrowserStorageCache, InMemoryCache, Cache, default */
|
|
1432
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1433
|
+
|
|
1434
|
+
"use strict";
|
|
1435
|
+
__webpack_require__.r(__webpack_exports__);
|
|
1436
|
+
/* harmony import */ var _aws_amplify_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @aws-amplify/core */ "@aws-amplify/core");
|
|
1437
|
+
/* harmony import */ var _aws_amplify_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_aws_amplify_core__WEBPACK_IMPORTED_MODULE_0__);
|
|
1438
|
+
/* harmony import */ var _BrowserStorageCache__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./BrowserStorageCache */ "./lib-esm/BrowserStorageCache.js");
|
|
1439
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "BrowserStorageCache", function() { return _BrowserStorageCache__WEBPACK_IMPORTED_MODULE_1__["BrowserStorageCache"]; });
|
|
1440
|
+
|
|
1441
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "Cache", function() { return _BrowserStorageCache__WEBPACK_IMPORTED_MODULE_1__["BrowserStorageCache"]; });
|
|
1442
|
+
|
|
1443
|
+
/* harmony import */ var _InMemoryCache__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./InMemoryCache */ "./lib-esm/InMemoryCache.js");
|
|
1444
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "InMemoryCache", function() { return _InMemoryCache__WEBPACK_IMPORTED_MODULE_2__["InMemoryCache"]; });
|
|
1445
|
+
|
|
1446
|
+
/*
|
|
1447
|
+
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
1448
|
+
*
|
|
1449
|
+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
|
|
1450
|
+
* the License. A copy of the License is located at
|
|
1451
|
+
*
|
|
1452
|
+
* http://aws.amazon.com/apache2.0/
|
|
1453
|
+
*
|
|
1454
|
+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
1455
|
+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
|
|
1456
|
+
* and limitations under the License.
|
|
1457
|
+
*/
|
|
1458
|
+
|
|
1459
|
+
|
|
1460
|
+
|
|
1461
|
+
|
|
1462
|
+
|
|
1463
|
+
/**
|
|
1464
|
+
* @deprecated use named import
|
|
1465
|
+
*/
|
|
1466
|
+
/* harmony default export */ __webpack_exports__["default"] = (_BrowserStorageCache__WEBPACK_IMPORTED_MODULE_1__["BrowserStorageCache"]);
|
|
1467
|
+
_aws_amplify_core__WEBPACK_IMPORTED_MODULE_0__["Amplify"].register(_BrowserStorageCache__WEBPACK_IMPORTED_MODULE_1__["BrowserStorageCache"]);
|
|
1468
|
+
|
|
1469
|
+
/***/ }),
|
|
1470
|
+
|
|
1471
|
+
/***/ "@aws-amplify/core":
|
|
1472
|
+
/*!***********************************!*\
|
|
1473
|
+
!*** external "aws_amplify_core" ***!
|
|
1474
|
+
\***********************************/
|
|
1475
|
+
/*! no static exports found */
|
|
1476
|
+
/***/ (function(module, exports) {
|
|
1477
|
+
|
|
1478
|
+
module.exports = __WEBPACK_EXTERNAL_MODULE__aws_amplify_core__;
|
|
1479
|
+
|
|
1480
|
+
/***/ })
|
|
1481
|
+
|
|
1482
|
+
/******/ });
|
|
1483
|
+
});
|
|
1484
|
+
//# sourceMappingURL=aws-amplify-cache.js.map
|