UNPKG

@aws-amplify/core

Version:
1 lines 3.82 kB
{"version":3,"file":"StorageCache.mjs","sources":["../../../src/Cache/StorageCache.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { ConsoleLogger } from '../Logger';\nimport { KeyValueStorage } from '../storage/KeyValueStorage';\nimport { getLocalStorageWithFallback } from '../storage/utils';\nimport { defaultConfig } from './constants';\nimport { StorageCacheCommon } from './StorageCacheCommon';\nimport { getCurrentSizeKey, getCurrentTime } from './utils';\nconst logger = new ConsoleLogger('StorageCache');\n/**\n * Customized storage based on the SessionStorage or LocalStorage with LRU implemented\n */\nexport class StorageCache extends StorageCacheCommon {\n /**\n * initialize the cache\n * @param config - the configuration of the cache\n */\n constructor(config) {\n const storage = getLocalStorageWithFallback();\n super({ config, keyValueStorage: new KeyValueStorage(storage) });\n this.storage = storage;\n this.getItem = this.getItem.bind(this);\n this.setItem = this.setItem.bind(this);\n this.removeItem = this.removeItem.bind(this);\n }\n async getAllCacheKeys(options) {\n const { omitSizeKey } = options ?? {};\n const keys = [];\n for (let i = 0; i < this.storage.length; i++) {\n const key = this.storage.key(i);\n if (omitSizeKey && key === getCurrentSizeKey(this.config.keyPrefix)) {\n continue;\n }\n if (key?.startsWith(this.config.keyPrefix)) {\n keys.push(key.substring(this.config.keyPrefix.length));\n }\n }\n return keys;\n }\n /**\n * Return a new instance of cache with customized configuration.\n * @param {Object} config - the customized configuration\n * @return {Object} - the new instance of Cache\n */\n createInstance(config) {\n if (!config.keyPrefix || config.keyPrefix === defaultConfig.keyPrefix) {\n logger.error('invalid keyPrefix, setting keyPrefix with timeStamp');\n config.keyPrefix = getCurrentTime.toString();\n }\n return new StorageCache(config);\n }\n}\n"],"names":[],"mappings":";;;;;;;;AAAA;AACA;AAOA,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,cAAc,CAAC,CAAC;AACjD;AACA;AACA;AACO,MAAM,YAAY,SAAS,kBAAk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