UNPKG

@aws-amplify/core

Version:
1 lines 3.8 kB
{"version":3,"file":"StorageCache.native.mjs","sources":["../../../src/Cache/StorageCache.native.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { loadAsyncStorage } from '@aws-amplify/react-native';\nimport { ConsoleLogger } from '../Logger';\nimport { defaultConfig } from './constants';\nimport { StorageCacheCommon } from './StorageCacheCommon';\nimport { getCurrentSizeKey, getCurrentTime } from './utils';\nconst logger = new ConsoleLogger('StorageCache');\nconst AsyncStorage = loadAsyncStorage();\n/*\n * Customized cache which based on the AsyncStorage 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 super({ config, keyValueStorage: AsyncStorage });\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 (const key of await AsyncStorage.getAllKeys()) {\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 async getAllStorageKeys() {\n try {\n return AsyncStorage.getAllKeys();\n }\n catch (e) {\n logger.warn(`getAllKeys failed! ${e}`);\n return [];\n }\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;AAMA,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,cAAc,CAAC;AAChD,MAAM,YAAY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