Source: Cache/StorageCache.js

  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. import {
  14. getCurrTime,
  15. getByteLength,
  16. defaultConfig
  17. } from './Utils';
  18. import { ConsoleLogger as Logger } from '../Common';
  19. const logger = new Logger('StorageCache');
  20. /**
  21. * Initialization of the cache
  22. *
  23. */
  24. class StorageCache {
  25. /**
  26. * Initialize the cache
  27. * @param config - the configuration of the cache
  28. */
  29. constructor(config) {
  30. this.config = Object.assign({}, config);
  31. this.cacheCurSizeKey = this.config.keyPrefix + 'CurSize';
  32. this.checkConfig();
  33. }
  34. /**
  35. * @private
  36. */
  37. checkConfig() {
  38. // check configuration
  39. if (!Number.isInteger(this.config.capacityInBytes)) {
  40. logger.error('Invalid parameter: capacityInBytes. It should be an Integer. Setting back to default.');
  41. this.config.capacityInBytes = defaultConfig.capacityInBytes;
  42. }
  43. if (!Number.isInteger(this.config.itemMaxSize)) {
  44. logger.error('Invalid parameter: itemMaxSize. It should be an Integer. Setting back to default.');
  45. this.config.itemMaxSize = defaultConfig.itemMaxSize;
  46. }
  47. if (!Number.isInteger(this.config.defaultTTL)) {
  48. logger.error('Invalid parameter: defaultTTL. It should be an Integer. Setting back to default.');
  49. this.config.defaultTTL = defaultConfig.defaultTTL;
  50. }
  51. if (!Number.isInteger(this.config.defaultPriority)) {
  52. logger.error('Invalid parameter: defaultPriority. It should be an Integer. Setting back to default.');
  53. this.config.defaultPriority = defaultConfig.defaultPriority;
  54. }
  55. if (this.config.itemMaxSize > this.config.capacityInBytes) {
  56. logger.error('Invalid parameter: itemMaxSize. It should be smaller than capacityInBytes. Setting back to default.');
  57. this.config.itemMaxSize = defaultConfig.itemMaxSize;
  58. }
  59. if (this.config.defaultPriority > 5 || this.config.defaultPriority < 1) {
  60. logger.error('Invalid parameter: defaultPriority. It should be between 1 and 5. Setting back to default.');
  61. this.config.defaultPriority = defaultConfig.defaultPriority;
  62. }
  63. if (Number(this.config.warningThreshold) > 1 || Number(this.config.warningThreshold) < 0) {
  64. logger.error('Invalid parameter: warningThreshold. It should be between 0 and 1. Setting back to default.');
  65. this.config.warningThreshold = defaultConfig.warningThreshold;
  66. }
  67. // set 5MB limit
  68. const cacheLimit = 5 * 1024 * 1024;
  69. if (this.config.capacityInBytes > cacheLimit) {
  70. logger.error('Cache Capacity should be less than 5MB. Setting back to default. Setting back to default.');
  71. this.config.capacityInBytes = defaultConfig.capacityInBytes;
  72. }
  73. }
  74. /**
  75. * produce a JSON object with meta-data and data value
  76. * @private
  77. * @param value - the value of the item
  78. * @param options - optional, the specified meta-data
  79. *
  80. * @return the item which has the meta-data and the value
  81. */
  82. fillCacheItem(key, value, options) {
  83. const ret = {
  84. key,
  85. data: JSON.stringify(value),
  86. timestamp: getCurrTime(),
  87. visitedTime: getCurrTime(),
  88. priority: options.priority,
  89. expires: options.expires,
  90. type: typeof value,
  91. byteSize: 0
  92. };
  93. ret.byteSize = getByteLength(JSON.stringify(ret));
  94. // for accurate size
  95. ret.byteSize = getByteLength(JSON.stringify(ret));
  96. return ret;
  97. }
  98. /**
  99. * set cache with customized configuration
  100. * @param {Object} [config] - The configuration of the cache
  101. * @return {Object} - The current configuration
  102. */
  103. configure(config) {
  104. if (!config) {
  105. return this.config;
  106. }
  107. if (config.keyPrefix) {
  108. logger.error(`Don't try to configure keyPrefix!`);
  109. }
  110. config.keyPrefix = this.config.keyPrefix;
  111. this.config = Object.assign({}, this.config, config);
  112. this.checkConfig();
  113. return this.config;
  114. }
  115. }
  116. export default StorageCache;