1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | import { getCurrTime, getByteLength, defaultConfig, isInteger } from './Utils';
|
15 |
|
16 | import { CacheConfig, CacheItem, CacheItemOptions } from './types';
|
17 | import { ConsoleLogger as Logger } from '@aws-amplify/core';
|
18 |
|
19 | const logger = new Logger('StorageCache');
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | export class StorageCache {
|
26 | protected cacheCurSizeKey: string;
|
27 | protected config: CacheConfig;
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
33 | constructor(config: CacheConfig) {
|
34 | this.config = Object.assign({}, config);
|
35 | this.cacheCurSizeKey = this.config.keyPrefix + 'CurSize';
|
36 | this.checkConfig();
|
37 | }
|
38 |
|
39 | public getModuleName() {
|
40 | return 'Cache';
|
41 | }
|
42 |
|
43 | private checkConfig(): void {
|
44 |
|
45 | if (!isInteger(this.config.capacityInBytes)) {
|
46 | logger.error(
|
47 | 'Invalid parameter: capacityInBytes. It should be an Integer. Setting back to default.'
|
48 | );
|
49 | this.config.capacityInBytes = defaultConfig.capacityInBytes;
|
50 | }
|
51 |
|
52 | if (!isInteger(this.config.itemMaxSize)) {
|
53 | logger.error(
|
54 | 'Invalid parameter: itemMaxSize. It should be an Integer. Setting back to default.'
|
55 | );
|
56 | this.config.itemMaxSize = defaultConfig.itemMaxSize;
|
57 | }
|
58 |
|
59 | if (!isInteger(this.config.defaultTTL)) {
|
60 | logger.error(
|
61 | 'Invalid parameter: defaultTTL. It should be an Integer. Setting back to default.'
|
62 | );
|
63 | this.config.defaultTTL = defaultConfig.defaultTTL;
|
64 | }
|
65 |
|
66 | if (!isInteger(this.config.defaultPriority)) {
|
67 | logger.error(
|
68 | 'Invalid parameter: defaultPriority. It should be an Integer. Setting back to default.'
|
69 | );
|
70 | this.config.defaultPriority = defaultConfig.defaultPriority;
|
71 | }
|
72 |
|
73 | if (this.config.itemMaxSize > this.config.capacityInBytes) {
|
74 | logger.error(
|
75 | 'Invalid parameter: itemMaxSize. It should be smaller than capacityInBytes. Setting back to default.'
|
76 | );
|
77 | this.config.itemMaxSize = defaultConfig.itemMaxSize;
|
78 | }
|
79 |
|
80 | if (this.config.defaultPriority > 5 || this.config.defaultPriority < 1) {
|
81 | logger.error(
|
82 | 'Invalid parameter: defaultPriority. It should be between 1 and 5. Setting back to default.'
|
83 | );
|
84 | this.config.defaultPriority = defaultConfig.defaultPriority;
|
85 | }
|
86 |
|
87 | if (
|
88 | Number(this.config.warningThreshold) > 1 ||
|
89 | Number(this.config.warningThreshold) < 0
|
90 | ) {
|
91 | logger.error(
|
92 | 'Invalid parameter: warningThreshold. It should be between 0 and 1. Setting back to default.'
|
93 | );
|
94 | this.config.warningThreshold = defaultConfig.warningThreshold;
|
95 | }
|
96 |
|
97 | const cacheLimit: number = 5 * 1024 * 1024;
|
98 | if (this.config.capacityInBytes > cacheLimit) {
|
99 | logger.error(
|
100 | 'Cache Capacity should be less than 5MB. Setting back to default. Setting back to default.'
|
101 | );
|
102 | this.config.capacityInBytes = defaultConfig.capacityInBytes;
|
103 | }
|
104 | }
|
105 |
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | protected fillCacheItem(
|
114 | key: string,
|
115 | value: object | number | string | boolean,
|
116 | options: CacheItemOptions
|
117 | ): CacheItem {
|
118 | const ret: CacheItem = {
|
119 | key,
|
120 | data: value,
|
121 | timestamp: getCurrTime(),
|
122 | visitedTime: getCurrTime(),
|
123 | priority: options.priority,
|
124 | expires: options.expires,
|
125 | type: typeof value,
|
126 | byteSize: 0,
|
127 | };
|
128 |
|
129 | ret.byteSize = getByteLength(JSON.stringify(ret));
|
130 |
|
131 |
|
132 | ret.byteSize = getByteLength(JSON.stringify(ret));
|
133 | return ret;
|
134 | }
|
135 |
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | public configure(config?: CacheConfig): CacheConfig {
|
143 | if (!config) {
|
144 | return this.config;
|
145 | }
|
146 | if (config.keyPrefix) {
|
147 | logger.warn(`Don't try to configure keyPrefix!`);
|
148 | }
|
149 |
|
150 | this.config = Object.assign({}, this.config, config, config.Cache);
|
151 | this.checkConfig();
|
152 | return this.config;
|
153 | }
|
154 | }
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | export default StorageCache;
|