UNPKG

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