UNPKG

4.74 kBPlain TextView 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 */
13
14import { getCurrTime, getByteLength, defaultConfig, isInteger } from './Utils';
15
16import { CacheConfig, CacheItem, CacheItemOptions } from './types';
17import { ConsoleLogger as Logger } from '@aws-amplify/core';
18
19const logger = new Logger('StorageCache');
20
21/**
22 * Initialization of the cache
23 *
24 */
25export class StorageCache {
26 protected cacheCurSizeKey: string;
27 protected config: CacheConfig;
28
29 /**
30 * Initialize the cache
31 * @param config - the configuration of the cache
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 // check configuration
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 // set 5MB limit
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 * produce a JSON object with meta-data and data value
108 * @param value - the value of the item
109 * @param options - optional, the specified meta-data
110 *
111 * @return - the item which has the meta-data and the value
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 // for accurate size
132 ret.byteSize = getByteLength(JSON.stringify(ret));
133 return ret;
134 }
135
136 /**
137 * set cache with customized configuration
138 * @param config - customized configuration
139 *
140 * @return - the current configuration
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 * @deprecated use named import
158 */
159export default StorageCache;