Skip to content

Commit

Permalink
Merge branch 'next' into next-storage-list
Browse files Browse the repository at this point in the history
  • Loading branch information
kvramyasri7 authored Aug 17, 2023
2 parents 9481d5d + a40aacb commit 829b4ca
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 167 deletions.
6 changes: 3 additions & 3 deletions packages/core/__tests__/Cache/StorageCache-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('StorageCache', () => {
defaultTTL: 3000000,
itemMaxSize: 1000,
keyPrefix: 'aws-amplify#$#',
storage: undefined,
storage: expect.any(Storage),
warningThreshold: 0.8,
});
});
Expand All @@ -130,9 +130,9 @@ describe('StorageCache', () => {
const spyon = jest.spyOn(Logger.prototype, 'warn');
const storage: StorageCache = new StorageCache(config);

const customizedConfig: CacheConfig = {
const customizedConfig = {
keyPrefix: 'abcc',
};
} as Omit<CacheConfig, 'keyPrefix'>;
const new_config = storage.configure(customizedConfig);

expect(spyon).toBeCalled();
Expand Down
65 changes: 31 additions & 34 deletions packages/core/src/Cache/AsyncStorageCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

import AsyncStorage from '@react-native-async-storage/async-storage';
import { Amplify } from '../Amplify';
import { ConsoleLogger as Logger } from '../Logger';
import { StorageCache } from './StorageCache';
import { defaultConfig, getCurrTime } from './Utils';
import { ICache } from './types';
import { CacheConfig, ICache } from './types';
import { getCurrSizeKey } from './Utils/CacheUtils';

const logger = new Logger('AsyncStorageCache');

Expand All @@ -19,14 +19,13 @@ export class AsyncStorageCache extends StorageCache implements ICache {
*
* @param {Object} config - the configuration of the cache
*/
constructor(config?) {
const cache_config = config
? Object.assign({}, defaultConfig, config)
: defaultConfig;
super(cache_config);
constructor(config?: CacheConfig) {
super(config);

this.getItem = this.getItem.bind(this);
this.setItem = this.setItem.bind(this);
this.removeItem = this.removeItem.bind(this);

logger.debug('Using AsyncStorageCache');
}

Expand All @@ -38,7 +37,7 @@ export class AsyncStorageCache extends StorageCache implements ICache {
async _decreaseCurSizeInBytes(amount) {
const curSize = await this.getCacheCurSize();
await AsyncStorage.setItem(
this.cacheCurSizeKey,
getCurrSizeKey(this.cacheConfig.keyPrefix),
(curSize - amount).toString()
);
}
Expand All @@ -51,7 +50,7 @@ export class AsyncStorageCache extends StorageCache implements ICache {
async _increaseCurSizeInBytes(amount) {
const curSize = await this.getCacheCurSize();
await AsyncStorage.setItem(
this.cacheCurSizeKey,
getCurrSizeKey(this.cacheConfig.keyPrefix),
(curSize + amount).toString()
);
}
Expand Down Expand Up @@ -139,9 +138,9 @@ export class AsyncStorageCache extends StorageCache implements ICache {
*/
async _sizeToPop(itemSize) {
const spaceItemNeed =
(await this.getCacheCurSize()) + itemSize - this.config.capacityInBytes;
(await this.getCacheCurSize()) + itemSize - this.cacheConfig.capacityInBytes;
const cacheThresholdSpace =
(1 - this.config.warningThreshold) * this.config.capacityInBytes;
(1 - this.cacheConfig.warningThreshold) * this.cacheConfig.capacityInBytes;
return spaceItemNeed > cacheThresholdSpace
? spaceItemNeed
: cacheThresholdSpace;
Expand All @@ -156,7 +155,7 @@ export class AsyncStorageCache extends StorageCache implements ICache {
*/
async _isCacheFull(itemSize) {
return (
itemSize + (await this.getCacheCurSize()) > this.config.capacityInBytes
itemSize + (await this.getCacheCurSize()) > this.cacheConfig.capacityInBytes
);
}

Expand All @@ -173,8 +172,8 @@ export class AsyncStorageCache extends StorageCache implements ICache {
for (let i = 0; i < keyInCache.length; i += 1) {
const key = keyInCache[i];
if (
key.indexOf(this.config.keyPrefix) === 0 &&
key !== this.cacheCurSizeKey
key.indexOf(this.cacheConfig.keyPrefix) === 0 &&
key !== getCurrSizeKey(this.cacheConfig.keyPrefix)
) {
if (await this._isExpired(key)) {
await this._removeItem(key);
Expand Down Expand Up @@ -250,11 +249,11 @@ export class AsyncStorageCache extends StorageCache implements ICache {
logger.debug(
`Set item: key is ${key}, value is ${value} with options: ${options}`
);
const prefixedKey = this.config.keyPrefix + key;
const prefixedKey = this.cacheConfig.keyPrefix + key;
// invalid keys
if (
prefixedKey === this.config.keyPrefix ||
prefixedKey === this.cacheCurSizeKey
prefixedKey === this.cacheConfig.keyPrefix ||
prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)
) {
logger.warn(`Invalid key: should not be empty or 'CurSize'`);
return;
Expand All @@ -269,11 +268,11 @@ export class AsyncStorageCache extends StorageCache implements ICache {
priority:
options && options.priority !== undefined
? options.priority
: this.config.defaultPriority,
: this.cacheConfig.defaultPriority,
expires:
options && options.expires !== undefined
? options.expires
: this.config.defaultTTL + getCurrTime(),
: this.cacheConfig.defaultTTL + getCurrTime(),
};

if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
Expand All @@ -286,7 +285,7 @@ export class AsyncStorageCache extends StorageCache implements ICache {
const item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);

// check wether this item is too big;
if (item.byteSize > this.config.itemMaxSize) {
if (item.byteSize > this.cacheConfig.itemMaxSize) {
logger.warn(
`Item with key: ${key} you are trying to put into is too big!`
);
Expand Down Expand Up @@ -333,11 +332,11 @@ export class AsyncStorageCache extends StorageCache implements ICache {
async getItem(key, options) {
logger.debug(`Get item: key is ${key} with options ${options}`);
let ret = null;
const prefixedKey = this.config.keyPrefix + key;
const prefixedKey = this.cacheConfig.keyPrefix + key;

if (
prefixedKey === this.config.keyPrefix ||
prefixedKey === this.cacheCurSizeKey
prefixedKey === this.cacheConfig.keyPrefix ||
prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)
) {
logger.warn(`Invalid key: should not be empty or 'CurSize'`);
return null;
Expand Down Expand Up @@ -380,11 +379,11 @@ export class AsyncStorageCache extends StorageCache implements ICache {
*/
async removeItem(key) {
logger.debug(`Remove item: key is ${key}`);
const prefixedKey = this.config.keyPrefix + key;
const prefixedKey = this.cacheConfig.keyPrefix + key;

if (
prefixedKey === this.config.keyPrefix ||
prefixedKey === this.cacheCurSizeKey
prefixedKey === this.cacheConfig.keyPrefix ||
prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)
) {
return;
}
Expand Down Expand Up @@ -412,7 +411,7 @@ export class AsyncStorageCache extends StorageCache implements ICache {

const keysToRemove = [];
for (let i = 0; i < keys.length; i += 1) {
if (keys[i].indexOf(this.config.keyPrefix) === 0) {
if (keys[i].indexOf(this.cacheConfig.keyPrefix) === 0) {
keysToRemove.push(keys[i]);
}
}
Expand All @@ -431,9 +430,9 @@ export class AsyncStorageCache extends StorageCache implements ICache {
* @return {Promise}
*/
async getCacheCurSize() {
let ret = await AsyncStorage.getItem(this.cacheCurSizeKey);
let ret = await AsyncStorage.getItem(getCurrSizeKey(this.cacheConfig.keyPrefix));
if (!ret) {
await AsyncStorage.setItem(this.cacheCurSizeKey, '0');
await AsyncStorage.setItem(getCurrSizeKey(this.cacheConfig.keyPrefix), '0');
ret = '0';
}
return Number(ret);
Expand All @@ -451,10 +450,10 @@ export class AsyncStorageCache extends StorageCache implements ICache {
const retKeys = [];
for (let i = 0; i < keys.length; i += 1) {
if (
keys[i].indexOf(this.config.keyPrefix) === 0 &&
keys[i] !== this.cacheCurSizeKey
keys[i].indexOf(this.cacheConfig.keyPrefix) === 0 &&
keys[i] !== getCurrSizeKey(this.cacheConfig.keyPrefix)
) {
retKeys.push(keys[i].substring(this.config.keyPrefix.length));
retKeys.push(keys[i].substring(this.cacheConfig.keyPrefix.length));
}
}
return retKeys;
Expand All @@ -480,5 +479,3 @@ export class AsyncStorageCache extends StorageCache implements ICache {

const instance: ICache = new AsyncStorageCache();
export { AsyncStorage, instance as Cache };

Amplify.register(instance);
Loading

0 comments on commit 829b4ca

Please sign in to comment.