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