1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | var Utils_1 = require("./Utils");
|
16 | var core_1 = require("@aws-amplify/core");
|
17 | var logger = new core_1.ConsoleLogger('StorageCache');
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var StorageCache = (function () {
|
23 | |
24 |
|
25 |
|
26 |
|
27 | function StorageCache(config) {
|
28 | this.config = Object.assign({}, config);
|
29 | this.cacheCurSizeKey = this.config.keyPrefix + 'CurSize';
|
30 | this.checkConfig();
|
31 | }
|
32 | StorageCache.prototype.getModuleName = function () {
|
33 | return 'Cache';
|
34 | };
|
35 | StorageCache.prototype.checkConfig = function () {
|
36 |
|
37 | if (!Utils_1.isInteger(this.config.capacityInBytes)) {
|
38 | logger.error('Invalid parameter: capacityInBytes. It should be an Integer. Setting back to default.');
|
39 | this.config.capacityInBytes = Utils_1.defaultConfig.capacityInBytes;
|
40 | }
|
41 | if (!Utils_1.isInteger(this.config.itemMaxSize)) {
|
42 | logger.error('Invalid parameter: itemMaxSize. It should be an Integer. Setting back to default.');
|
43 | this.config.itemMaxSize = Utils_1.defaultConfig.itemMaxSize;
|
44 | }
|
45 | if (!Utils_1.isInteger(this.config.defaultTTL)) {
|
46 | logger.error('Invalid parameter: defaultTTL. It should be an Integer. Setting back to default.');
|
47 | this.config.defaultTTL = Utils_1.defaultConfig.defaultTTL;
|
48 | }
|
49 | if (!Utils_1.isInteger(this.config.defaultPriority)) {
|
50 | logger.error('Invalid parameter: defaultPriority. It should be an Integer. Setting back to default.');
|
51 | this.config.defaultPriority = Utils_1.defaultConfig.defaultPriority;
|
52 | }
|
53 | if (this.config.itemMaxSize > this.config.capacityInBytes) {
|
54 | logger.error('Invalid parameter: itemMaxSize. It should be smaller than capacityInBytes. Setting back to default.');
|
55 | this.config.itemMaxSize = Utils_1.defaultConfig.itemMaxSize;
|
56 | }
|
57 | if (this.config.defaultPriority > 5 || this.config.defaultPriority < 1) {
|
58 | logger.error('Invalid parameter: defaultPriority. It should be between 1 and 5. Setting back to default.');
|
59 | this.config.defaultPriority = Utils_1.defaultConfig.defaultPriority;
|
60 | }
|
61 | if (Number(this.config.warningThreshold) > 1 ||
|
62 | Number(this.config.warningThreshold) < 0) {
|
63 | logger.error('Invalid parameter: warningThreshold. It should be between 0 and 1. Setting back to default.');
|
64 | this.config.warningThreshold = Utils_1.defaultConfig.warningThreshold;
|
65 | }
|
66 |
|
67 | var cacheLimit = 5 * 1024 * 1024;
|
68 | if (this.config.capacityInBytes > cacheLimit) {
|
69 | logger.error('Cache Capacity should be less than 5MB. Setting back to default. Setting back to default.');
|
70 | this.config.capacityInBytes = Utils_1.defaultConfig.capacityInBytes;
|
71 | }
|
72 | };
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | StorageCache.prototype.fillCacheItem = function (key, value, options) {
|
81 | var ret = {
|
82 | key: key,
|
83 | data: value,
|
84 | timestamp: Utils_1.getCurrTime(),
|
85 | visitedTime: Utils_1.getCurrTime(),
|
86 | priority: options.priority,
|
87 | expires: options.expires,
|
88 | type: typeof value,
|
89 | byteSize: 0,
|
90 | };
|
91 | ret.byteSize = Utils_1.getByteLength(JSON.stringify(ret));
|
92 |
|
93 | ret.byteSize = Utils_1.getByteLength(JSON.stringify(ret));
|
94 | return ret;
|
95 | };
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | StorageCache.prototype.configure = function (config) {
|
103 | if (!config) {
|
104 | return this.config;
|
105 | }
|
106 | if (config.keyPrefix) {
|
107 | logger.warn("Don't try to configure keyPrefix!");
|
108 | }
|
109 | this.config = Object.assign({}, this.config, config, config.Cache);
|
110 | this.checkConfig();
|
111 | return this.config;
|
112 | };
|
113 | return StorageCache;
|
114 | }());
|
115 | exports.StorageCache = StorageCache;
|
116 |
|
117 |
|
118 |
|
119 | exports.default = StorageCache;
|
120 |
|
\ | No newline at end of file |