UNPKG

4.6 kBJavaScriptView Raw
1"use strict";
2// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3// SPDX-License-Identifier: Apache-2.0
4Object.defineProperty(exports, "__esModule", { value: true });
5var Utils_1 = require("./Utils");
6var core_1 = require("@aws-amplify/core");
7var logger = new core_1.ConsoleLogger('StorageCache');
8/**
9 * Initialization of the cache
10 *
11 */
12var StorageCache = /** @class */ (function () {
13 /**
14 * Initialize the cache
15 * @param config - the configuration of the cache
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 // check configuration
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 // set 5MB limit
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 * produce a JSON object with meta-data and data value
65 * @param value - the value of the item
66 * @param options - optional, the specified meta-data
67 *
68 * @return - the item which has the meta-data and the value
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 // for accurate size
83 ret.byteSize = Utils_1.getByteLength(JSON.stringify(ret));
84 return ret;
85 };
86 /**
87 * set cache with customized configuration
88 * @param config - customized configuration
89 *
90 * @return - the current configuration
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}());
105exports.StorageCache = StorageCache;
106//# sourceMappingURL=StorageCache.js.map
\No newline at end of file