UNPKG

4.98 kBJavaScriptView Raw
1/*
2 * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5 * the License. A copy of the License is located at
6 *
7 * http://aws.amazon.com/apache2.0/
8 *
9 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11 * and limitations under the License.
12 */
13import { getCurrTime, getByteLength, defaultConfig, isInteger } from './Utils';
14import { ConsoleLogger as Logger } from '@aws-amplify/core';
15var logger = new Logger('StorageCache');
16/**
17 * Initialization of the cache
18 *
19 */
20var StorageCache = /** @class */ (function () {
21 /**
22 * Initialize the cache
23 * @param config - the configuration of the cache
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 // check configuration
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 // set 5MB limit
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 * produce a JSON object with meta-data and data value
73 * @param value - the value of the item
74 * @param options - optional, the specified meta-data
75 *
76 * @return - the item which has the meta-data and the value
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 // for accurate size
91 ret.byteSize = getByteLength(JSON.stringify(ret));
92 return ret;
93 };
94 /**
95 * set cache with customized configuration
96 * @param config - customized configuration
97 *
98 * @return - the current configuration
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}());
113export { StorageCache };
114/**
115 * @deprecated use named import
116 */
117export default StorageCache;
118//# sourceMappingURL=StorageCache.js.map
\No newline at end of file