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