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