@aws-amplify/core 5.8.2 → 6.0.1-console-preview.f63250a.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/lib/Amplify.d.ts +18 -17
- package/lib/Amplify.js +9 -0
- package/lib/AwsClients/CognitoIdentity/base.d.ts +3 -10
- package/lib/AwsClients/CognitoIdentity/getCredentialsForIdentity.d.ts +3 -17
- package/lib/AwsClients/CognitoIdentity/getCredentialsForIdentity.js +8 -8
- package/lib/AwsClients/CognitoIdentity/getId.d.ts +3 -17
- package/lib/AwsClients/Pinpoint/base.d.ts +2 -10
- package/lib/AwsClients/Pinpoint/getInAppMessages.d.ts +3 -17
- package/lib/AwsClients/Pinpoint/getInAppMessages.js +1 -1
- package/lib/AwsClients/Pinpoint/putEvents.d.ts +3 -17
- package/lib/AwsClients/Pinpoint/putEvents.js +6 -0
- package/lib/AwsClients/Pinpoint/updateEndpoint.d.ts +3 -17
- package/lib/AwsClients/Pinpoint/updateEndpoint.js +2 -2
- package/lib/Cache/AsyncStorageCache.d.ts +154 -0
- package/lib/Cache/AsyncStorageCache.js +744 -0
- package/lib/Cache/BrowserStorageCache.d.ts +166 -0
- package/lib/Cache/BrowserStorageCache.js +469 -0
- package/lib/Cache/InMemoryCache.d.ts +129 -0
- package/lib/Cache/InMemoryCache.js +349 -0
- package/lib/Cache/StorageCache.d.ts +39 -0
- package/lib/Cache/StorageCache.js +121 -0
- package/lib/Cache/Utils/CacheList.d.ts +89 -0
- package/lib/Cache/Utils/CacheList.js +196 -0
- package/lib/Cache/Utils/CacheUtils.d.ts +25 -0
- package/lib/Cache/Utils/CacheUtils.js +88 -0
- package/lib/Cache/Utils/index.d.ts +2 -0
- package/lib/Cache/Utils/index.js +16 -0
- package/lib/Cache/reactnative.d.ts +3 -0
- package/lib/Cache/reactnative.js +8 -0
- package/lib/Cache/types/Cache.d.ts +55 -0
- package/lib/Cache/types/Cache.js +4 -0
- package/lib/Cache/types/index.d.ts +1 -0
- package/lib/Cache/types/index.js +19 -0
- package/lib/ClientDevice/android.js +3 -2
- package/lib/ClientDevice/ios.d.ts +2 -2
- package/lib/ClientDevice/ios.js +3 -2
- package/lib/ClientDevice/reactnative.js +3 -2
- package/lib/Credentials.d.ts +1 -1
- package/lib/Credentials.js +2 -1
- package/lib/Errors.d.ts +19 -0
- package/lib/Errors.js +50 -1
- package/lib/Hub.js +1 -0
- package/lib/I18n/I18n.d.ts +29 -9
- package/lib/I18n/I18n.js +30 -5
- package/lib/I18n/index.d.ts +18 -5
- package/lib/I18n/index.js +35 -5
- package/lib/I18n/types.d.ts +4 -1
- package/lib/I18n/types.js +3 -0
- package/lib/JS.d.ts +6 -6
- package/lib/Logger/ConsoleLogger.d.ts +11 -11
- package/lib/OAuthHelper/GoogleOAuth.js +2 -1
- package/lib/Platform/detectFramework.js +2 -1
- package/lib/Platform/detection/Next.d.ts +1 -1
- package/lib/Platform/detection/React.js +1 -0
- package/lib/Platform/version.d.ts +1 -1
- package/lib/Platform/version.js +1 -1
- package/lib/Providers/AWSCloudWatchProvider.d.ts +2 -2
- package/lib/Providers/AWSCloudWatchProvider.js +35 -12
- package/lib/RNComponents/index.d.ts +1 -1
- package/lib/RNComponents/reactnative.js +3 -2
- package/lib/ServiceWorker/ServiceWorker.d.ts +4 -4
- package/lib/ServiceWorker/ServiceWorker.js +37 -11
- package/lib/Signer.d.ts +14 -1
- package/lib/StorageHelper/cookieStorage.d.ts +13 -0
- package/lib/StorageHelper/cookieStorage.js +167 -0
- package/lib/StorageHelper/inMemoryStorage.d.ts +10 -0
- package/lib/StorageHelper/inMemoryStorage.js +82 -0
- package/lib/StorageHelper/index.d.ts +7 -3
- package/lib/StorageHelper/index.js +9 -1
- package/lib/StorageHelper/localStorage.d.ts +32 -0
- package/lib/StorageHelper/localStorage.js +114 -0
- package/lib/StorageHelper/reactnative.d.ts +71 -1
- package/lib/StorageHelper/reactnative.js +154 -10
- package/lib/StorageHelper/sessionStorage.d.ts +32 -0
- package/lib/StorageHelper/sessionStorage.js +114 -0
- package/lib/UniversalStorage/index.d.ts +2 -2
- package/lib/Util/BackgroundProcessManager.d.ts +2 -2
- package/lib/Util/BackgroundProcessManager.js +1 -0
- package/lib/Util/DateUtils.d.ts +12 -1
- package/lib/Util/Reachability.js +7 -5
- package/lib/Util/Reachability.native.d.ts +3 -2
- package/lib/Util/StringUtils.js +3 -10
- package/lib/Util/errors/AssertError.d.ts +2 -0
- package/lib/Util/errors/AssertError.js +11 -0
- package/lib/clients/middleware/retry/defaultRetryDecider.d.ts +1 -1
- package/lib/clients/middleware/retry/defaultRetryDecider.js +17 -13
- package/lib/clients/middleware/retry/isClockSkewError.js +1 -1
- package/lib/clients/middleware/retry/middleware.d.ts +1 -1
- package/lib/clients/middleware/retry/middleware.js +13 -13
- package/lib/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.js +1 -1
- package/lib/clients/serde/responseInfo.js +1 -1
- package/lib/clients/types/aws.d.ts +4 -0
- package/lib/clients/types/index.d.ts +1 -1
- package/lib/constants.d.ts +7 -0
- package/lib/constants.js +9 -1
- package/lib/index.d.ts +18 -4
- package/lib/index.js +32 -5
- package/lib/parseAWSExports.d.ts +1 -1
- package/lib/singleton/Auth/index.d.ts +30 -0
- package/lib/singleton/Auth/index.js +133 -0
- package/lib/singleton/Auth/types.d.ts +95 -0
- package/lib/singleton/Auth/types.js +6 -0
- package/lib/singleton/Auth/utils/index.d.ts +5 -0
- package/lib/singleton/Auth/utils/index.js +52 -0
- package/lib/singleton/Storage/types.d.ts +14 -0
- package/lib/singleton/Storage/types.js +4 -0
- package/lib/singleton/index.d.ts +46 -0
- package/lib/singleton/index.js +147 -0
- package/lib/singleton/types.d.ts +21 -0
- package/lib/singleton/types.js +4 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/types.d.ts +31 -1
- package/lib/types/types.js +2 -0
- package/lib-esm/Amplify.d.ts +18 -17
- package/lib-esm/Amplify.js +9 -0
- package/lib-esm/AwsClients/CognitoIdentity/base.d.ts +3 -10
- package/lib-esm/AwsClients/CognitoIdentity/getCredentialsForIdentity.d.ts +3 -17
- package/lib-esm/AwsClients/CognitoIdentity/getCredentialsForIdentity.js +8 -8
- package/lib-esm/AwsClients/CognitoIdentity/getId.d.ts +3 -17
- package/lib-esm/AwsClients/Pinpoint/base.d.ts +2 -10
- package/lib-esm/AwsClients/Pinpoint/getInAppMessages.d.ts +3 -17
- package/lib-esm/AwsClients/Pinpoint/getInAppMessages.js +1 -1
- package/lib-esm/AwsClients/Pinpoint/putEvents.d.ts +3 -17
- package/lib-esm/AwsClients/Pinpoint/putEvents.js +6 -0
- package/lib-esm/AwsClients/Pinpoint/updateEndpoint.d.ts +3 -17
- package/lib-esm/AwsClients/Pinpoint/updateEndpoint.js +2 -2
- package/lib-esm/Cache/AsyncStorageCache.d.ts +154 -0
- package/lib-esm/Cache/AsyncStorageCache.js +737 -0
- package/lib-esm/Cache/BrowserStorageCache.d.ts +166 -0
- package/lib-esm/Cache/BrowserStorageCache.js +466 -0
- package/lib-esm/Cache/InMemoryCache.d.ts +129 -0
- package/lib-esm/Cache/InMemoryCache.js +346 -0
- package/lib-esm/Cache/StorageCache.d.ts +39 -0
- package/lib-esm/Cache/StorageCache.js +118 -0
- package/lib-esm/Cache/Utils/CacheList.d.ts +89 -0
- package/lib-esm/Cache/Utils/CacheList.js +194 -0
- package/lib-esm/Cache/Utils/CacheUtils.d.ts +25 -0
- package/lib-esm/Cache/Utils/CacheUtils.js +81 -0
- package/lib-esm/Cache/Utils/index.d.ts +2 -0
- package/lib-esm/Cache/Utils/index.js +4 -0
- package/lib-esm/Cache/reactnative.d.ts +3 -0
- package/lib-esm/Cache/reactnative.js +6 -0
- package/lib-esm/Cache/types/Cache.d.ts +55 -0
- package/lib-esm/Cache/types/Cache.js +3 -0
- package/lib-esm/Cache/types/index.d.ts +1 -0
- package/lib-esm/Cache/types/index.js +3 -0
- package/lib-esm/ClientDevice/android.js +1 -0
- package/lib-esm/ClientDevice/ios.d.ts +2 -2
- package/lib-esm/ClientDevice/ios.js +1 -0
- package/lib-esm/ClientDevice/reactnative.js +1 -0
- package/lib-esm/Credentials.d.ts +1 -1
- package/lib-esm/Credentials.js +2 -1
- package/lib-esm/Errors.d.ts +19 -0
- package/lib-esm/Errors.js +49 -0
- package/lib-esm/Hub.js +1 -0
- package/lib-esm/I18n/I18n.d.ts +29 -9
- package/lib-esm/I18n/I18n.js +30 -5
- package/lib-esm/I18n/index.d.ts +18 -5
- package/lib-esm/I18n/index.js +35 -5
- package/lib-esm/I18n/types.d.ts +4 -1
- package/lib-esm/I18n/types.js +3 -0
- package/lib-esm/JS.d.ts +6 -6
- package/lib-esm/Logger/ConsoleLogger.d.ts +11 -11
- package/lib-esm/OAuthHelper/GoogleOAuth.js +2 -1
- package/lib-esm/Platform/detectFramework.js +2 -1
- package/lib-esm/Platform/detection/Next.d.ts +1 -1
- package/lib-esm/Platform/detection/React.js +1 -0
- package/lib-esm/Platform/version.d.ts +1 -1
- package/lib-esm/Platform/version.js +1 -1
- package/lib-esm/Providers/AWSCloudWatchProvider.d.ts +2 -2
- package/lib-esm/Providers/AWSCloudWatchProvider.js +33 -10
- package/lib-esm/RNComponents/index.d.ts +1 -1
- package/lib-esm/RNComponents/reactnative.js +1 -0
- package/lib-esm/ServiceWorker/ServiceWorker.d.ts +4 -4
- package/lib-esm/ServiceWorker/ServiceWorker.js +37 -11
- package/lib-esm/Signer.d.ts +14 -1
- package/lib-esm/StorageHelper/cookieStorage.d.ts +13 -0
- package/lib-esm/StorageHelper/cookieStorage.js +141 -0
- package/lib-esm/StorageHelper/inMemoryStorage.d.ts +10 -0
- package/lib-esm/StorageHelper/inMemoryStorage.js +79 -0
- package/lib-esm/StorageHelper/index.d.ts +7 -3
- package/lib-esm/StorageHelper/index.js +4 -0
- package/lib-esm/StorageHelper/localStorage.d.ts +32 -0
- package/lib-esm/StorageHelper/localStorage.js +111 -0
- package/lib-esm/StorageHelper/reactnative.d.ts +71 -1
- package/lib-esm/StorageHelper/reactnative.js +153 -9
- package/lib-esm/StorageHelper/sessionStorage.d.ts +32 -0
- package/lib-esm/StorageHelper/sessionStorage.js +111 -0
- package/lib-esm/UniversalStorage/index.d.ts +2 -2
- package/lib-esm/Util/BackgroundProcessManager.d.ts +2 -2
- package/lib-esm/Util/BackgroundProcessManager.js +1 -0
- package/lib-esm/Util/DateUtils.d.ts +12 -1
- package/lib-esm/Util/Reachability.js +5 -3
- package/lib-esm/Util/Reachability.native.d.ts +3 -2
- package/lib-esm/Util/StringUtils.js +3 -10
- package/lib-esm/Util/errors/AssertError.d.ts +2 -0
- package/lib-esm/Util/errors/AssertError.js +7 -0
- package/lib-esm/clients/middleware/retry/defaultRetryDecider.d.ts +1 -1
- package/lib-esm/clients/middleware/retry/defaultRetryDecider.js +17 -13
- package/lib-esm/clients/middleware/retry/isClockSkewError.js +1 -1
- package/lib-esm/clients/middleware/retry/middleware.d.ts +1 -1
- package/lib-esm/clients/middleware/retry/middleware.js +13 -13
- package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.js +1 -1
- package/lib-esm/clients/serde/responseInfo.js +1 -1
- package/lib-esm/clients/types/aws.d.ts +4 -0
- package/lib-esm/clients/types/index.d.ts +1 -1
- package/lib-esm/constants.d.ts +7 -0
- package/lib-esm/constants.js +8 -0
- package/lib-esm/index.d.ts +18 -4
- package/lib-esm/index.js +15 -3
- package/lib-esm/parseAWSExports.d.ts +1 -1
- package/lib-esm/singleton/Auth/index.d.ts +30 -0
- package/lib-esm/singleton/Auth/index.js +129 -0
- package/lib-esm/singleton/Auth/types.d.ts +95 -0
- package/lib-esm/singleton/Auth/types.js +5 -0
- package/lib-esm/singleton/Auth/utils/index.d.ts +5 -0
- package/lib-esm/singleton/Auth/utils/index.js +45 -0
- package/lib-esm/singleton/Storage/types.d.ts +14 -0
- package/lib-esm/singleton/Storage/types.js +3 -0
- package/lib-esm/singleton/index.d.ts +46 -0
- package/lib-esm/singleton/index.js +143 -0
- package/lib-esm/singleton/types.d.ts +21 -0
- package/lib-esm/singleton/types.js +3 -0
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types/types.d.ts +31 -1
- package/lib-esm/types/types.js +2 -0
- package/package.json +38 -15
- package/src/Amplify.ts +9 -3
- package/src/AwsClients/CognitoIdentity/base.ts +2 -1
- package/src/AwsClients/CognitoIdentity/getCredentialsForIdentity.ts +13 -6
- package/src/AwsClients/Pinpoint/base.ts +6 -2
- package/src/AwsClients/Pinpoint/getInAppMessages.ts +1 -1
- package/src/AwsClients/Pinpoint/putEvents.ts +6 -0
- package/src/AwsClients/Pinpoint/updateEndpoint.ts +2 -2
- package/src/Cache/AsyncStorageCache.ts +500 -0
- package/src/Cache/BrowserStorageCache.ts +517 -0
- package/src/Cache/CHANGELOG.md +1227 -0
- package/src/Cache/InMemoryCache.ts +354 -0
- package/src/Cache/StorageCache.ts +157 -0
- package/src/Cache/Utils/CacheList.ts +198 -0
- package/src/Cache/Utils/CacheUtils.ts +92 -0
- package/src/Cache/Utils/index.ts +11 -0
- package/src/Cache/reactnative.ts +9 -0
- package/src/Cache/types/Cache.ts +75 -0
- package/src/Cache/types/index.ts +4 -0
- package/src/ClientDevice/android.ts +1 -1
- package/src/ClientDevice/ios.ts +2 -2
- package/src/ClientDevice/reactnative.ts +1 -1
- package/src/Credentials.ts +19 -17
- package/src/Errors.ts +41 -0
- package/src/Hub.ts +1 -0
- package/src/I18n/I18n.ts +40 -12
- package/src/I18n/index.ts +45 -12
- package/src/I18n/types.ts +3 -0
- package/src/JS.ts +24 -17
- package/src/Logger/ConsoleLogger.ts +10 -10
- package/src/OAuthHelper/FacebookOAuth.ts +2 -2
- package/src/OAuthHelper/GoogleOAuth.ts +9 -6
- package/src/Platform/detectFramework.ts +1 -1
- package/src/Platform/detection/React.ts +5 -3
- package/src/Platform/version.ts +1 -1
- package/src/Providers/AWSCloudWatchProvider.ts +39 -14
- package/src/RNComponents/index.ts +1 -1
- package/src/RNComponents/reactnative.ts +1 -1
- package/src/ServiceWorker/ServiceWorker.ts +58 -18
- package/src/Signer.ts +22 -3
- package/src/StorageHelper/cookieStorage.ts +99 -0
- package/src/StorageHelper/inMemoryStorage.ts +32 -0
- package/src/StorageHelper/index.ts +6 -1
- package/src/StorageHelper/localStorage.ts +60 -0
- package/src/StorageHelper/reactnative.ts +94 -14
- package/src/StorageHelper/sessionStorage.ts +60 -0
- package/src/Util/BackgroundProcessManager.ts +33 -9
- package/src/Util/DateUtils.ts +14 -5
- package/src/Util/Reachability.native.ts +3 -2
- package/src/Util/Reachability.ts +9 -7
- package/src/Util/Retry.ts +2 -1
- package/src/Util/StringUtils.ts +3 -10
- package/src/Util/errors/AssertError.ts +11 -0
- package/src/clients/handlers/fetch.ts +2 -2
- package/src/clients/middleware/retry/defaultRetryDecider.ts +12 -6
- package/src/clients/middleware/retry/isClockSkewError.ts +1 -1
- package/src/clients/middleware/retry/middleware.ts +9 -9
- package/src/clients/middleware/signing/middleware.ts +1 -1
- package/src/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.ts +1 -1
- package/src/clients/serde/responseInfo.ts +2 -1
- package/src/clients/types/aws.ts +10 -0
- package/src/clients/types/index.ts +1 -0
- package/src/constants.ts +16 -0
- package/src/global.d.ts +11 -0
- package/src/index.ts +66 -4
- package/src/parseAWSExports.ts +1 -1
- package/src/singleton/Auth/index.ts +113 -0
- package/src/singleton/Auth/types.ts +129 -0
- package/src/singleton/Auth/utils/index.ts +68 -0
- package/src/singleton/Storage/types.ts +21 -0
- package/src/singleton/index.ts +131 -0
- package/src/singleton/types.ts +48 -0
- package/src/types/types.ts +38 -1
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { CacheList, defaultConfig, getCurrTime, CacheObject } from './Utils';
|
|
5
|
+
|
|
6
|
+
import { StorageCache } from './StorageCache';
|
|
7
|
+
import { ICache, CacheConfig, CacheItem, CacheItemOptions } from './types';
|
|
8
|
+
import { ConsoleLogger as Logger } from '../Logger';
|
|
9
|
+
import { asserts } from '../Util/errors/AssertError';
|
|
10
|
+
import { STORAGE_CACHE_EXCEPTION } from '../constants';
|
|
11
|
+
import { getCurrSizeKey } from './Utils/CacheUtils';
|
|
12
|
+
|
|
13
|
+
const logger = new Logger('InMemoryCache');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Customized in-memory cache with LRU implemented
|
|
17
|
+
* @member cacheObj - object which store items
|
|
18
|
+
* @member cacheList - list of keys in the cache with LRU
|
|
19
|
+
* @member curSizeInBytes - current size of the cache
|
|
20
|
+
* @member maxPriority - max of the priority
|
|
21
|
+
*/
|
|
22
|
+
export class InMemoryCacheClass extends StorageCache implements ICache {
|
|
23
|
+
private cacheList: CacheList[];
|
|
24
|
+
private curSizeInBytes: number;
|
|
25
|
+
private maxPriority: number;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* initialize the cache
|
|
29
|
+
*
|
|
30
|
+
* @param config - the configuration of the cache
|
|
31
|
+
*/
|
|
32
|
+
constructor(config?: CacheConfig) {
|
|
33
|
+
super(config);
|
|
34
|
+
|
|
35
|
+
this.cacheList = [];
|
|
36
|
+
this.curSizeInBytes = 0;
|
|
37
|
+
this.maxPriority = 5;
|
|
38
|
+
|
|
39
|
+
this.getItem = this.getItem.bind(this);
|
|
40
|
+
this.setItem = this.setItem.bind(this);
|
|
41
|
+
this.removeItem = this.removeItem.bind(this);
|
|
42
|
+
|
|
43
|
+
// initialize list for every priority
|
|
44
|
+
for (let i = 0; i < this.maxPriority; i += 1) {
|
|
45
|
+
this.cacheList[i] = new CacheList();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* decrease current size of the cache
|
|
51
|
+
*
|
|
52
|
+
* @param amount - the amount of the cache size which needs to be decreased
|
|
53
|
+
*/
|
|
54
|
+
private _decreaseCurSizeInBytes(amount: number): void {
|
|
55
|
+
this.curSizeInBytes -= amount;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* increase current size of the cache
|
|
60
|
+
*
|
|
61
|
+
* @param amount - the amount of the cache szie which need to be increased
|
|
62
|
+
*/
|
|
63
|
+
private _increaseCurSizeInBytes(amount: number): void {
|
|
64
|
+
this.curSizeInBytes += amount;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* check whether item is expired
|
|
69
|
+
*
|
|
70
|
+
* @param key - the key of the item
|
|
71
|
+
*
|
|
72
|
+
* @return true if the item is expired.
|
|
73
|
+
*/
|
|
74
|
+
private _isExpired(key: string): boolean {
|
|
75
|
+
const text: string | null = CacheObject.getItem(key);
|
|
76
|
+
|
|
77
|
+
asserts(text !== null, {
|
|
78
|
+
name: STORAGE_CACHE_EXCEPTION,
|
|
79
|
+
message: 'item from storage is null',
|
|
80
|
+
});
|
|
81
|
+
const item: CacheItem = JSON.parse(text);
|
|
82
|
+
if (getCurrTime() >= item.expires) {
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* delete item from cache
|
|
90
|
+
*
|
|
91
|
+
* @param prefixedKey - the key of the item
|
|
92
|
+
* @param listIdx - indicates which cache list the key belongs to
|
|
93
|
+
*/
|
|
94
|
+
private _removeItem(prefixedKey: string, listIdx: number): void {
|
|
95
|
+
// delete the key from the list
|
|
96
|
+
this.cacheList[listIdx].removeItem(prefixedKey);
|
|
97
|
+
// decrease the current size of the cache
|
|
98
|
+
const item = CacheObject.getItem(prefixedKey);
|
|
99
|
+
asserts(item !== null, {
|
|
100
|
+
name: STORAGE_CACHE_EXCEPTION,
|
|
101
|
+
message: 'item from storage is null',
|
|
102
|
+
});
|
|
103
|
+
this._decreaseCurSizeInBytes(JSON.parse(item).byteSize);
|
|
104
|
+
// finally remove the item from memory
|
|
105
|
+
CacheObject.removeItem(prefixedKey);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* put item into cache
|
|
110
|
+
*
|
|
111
|
+
* @param prefixedKey - the key of the item
|
|
112
|
+
* @param itemData - the value of the item
|
|
113
|
+
* @param itemSizeInBytes - the byte size of the item
|
|
114
|
+
* @param listIdx - indicates which cache list the key belongs to
|
|
115
|
+
*/
|
|
116
|
+
private _setItem(
|
|
117
|
+
prefixedKey: string,
|
|
118
|
+
item: CacheItem,
|
|
119
|
+
listIdx: number
|
|
120
|
+
): void {
|
|
121
|
+
// insert the key into the list
|
|
122
|
+
this.cacheList[listIdx].insertItem(prefixedKey);
|
|
123
|
+
// increase the current size of the cache
|
|
124
|
+
this._increaseCurSizeInBytes(item.byteSize);
|
|
125
|
+
// finally add the item into memory
|
|
126
|
+
CacheObject.setItem(prefixedKey, JSON.stringify(item));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* see whether cache is full
|
|
131
|
+
*
|
|
132
|
+
* @param itemSize
|
|
133
|
+
*
|
|
134
|
+
* @return true if cache is full
|
|
135
|
+
*/
|
|
136
|
+
private _isCacheFull(itemSize: number): boolean {
|
|
137
|
+
return this.curSizeInBytes + itemSize > this.cacheConfig.capacityInBytes;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* check whether the cache contains the key
|
|
142
|
+
*
|
|
143
|
+
* @param key
|
|
144
|
+
*/
|
|
145
|
+
private containsKey(key: string): number {
|
|
146
|
+
const prefixedKey: string = this.cacheConfig.keyPrefix + key;
|
|
147
|
+
for (let i = 0; i < this.maxPriority; i += 1) {
|
|
148
|
+
if (this.cacheList[i].containsKey(prefixedKey)) {
|
|
149
|
+
return i + 1;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return -1;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* * Set item into cache. You can put number, string, boolean or object.
|
|
157
|
+
* The cache will first check whether has the same key.
|
|
158
|
+
* If it has, it will delete the old item and then put the new item in
|
|
159
|
+
* The cache will pop out items if it is full
|
|
160
|
+
* You can specify the cache item options. The cache will abort and output a warning:
|
|
161
|
+
* If the key is invalid
|
|
162
|
+
* If the size of the item exceeds itemMaxSize.
|
|
163
|
+
* If the value is undefined
|
|
164
|
+
* If incorrect cache item configuration
|
|
165
|
+
* If error happened with browser storage
|
|
166
|
+
*
|
|
167
|
+
* @param key - the key of the item
|
|
168
|
+
* @param value - the value of the item
|
|
169
|
+
* @param options - optional, the specified meta-data
|
|
170
|
+
*
|
|
171
|
+
* @throws if the item is too big which exceeds the limit of single item size
|
|
172
|
+
* @throws if the key is invalid
|
|
173
|
+
*/
|
|
174
|
+
public setItem(
|
|
175
|
+
key: string,
|
|
176
|
+
value: object | string | number | boolean,
|
|
177
|
+
options?: CacheItemOptions
|
|
178
|
+
): void {
|
|
179
|
+
const prefixedKey: string = this.cacheConfig.keyPrefix + key;
|
|
180
|
+
// invalid keys
|
|
181
|
+
if (
|
|
182
|
+
prefixedKey === this.cacheConfig.keyPrefix ||
|
|
183
|
+
prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)
|
|
184
|
+
) {
|
|
185
|
+
logger.warn(`Invalid key: should not be empty or 'CurSize'`);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (typeof value === 'undefined') {
|
|
190
|
+
logger.warn(`The value of item should not be undefined!`);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const cacheItemOptions = {
|
|
195
|
+
priority:
|
|
196
|
+
options && options.priority !== undefined
|
|
197
|
+
? options.priority
|
|
198
|
+
: this.cacheConfig.defaultPriority,
|
|
199
|
+
expires:
|
|
200
|
+
options && options.expires !== undefined
|
|
201
|
+
? options.expires
|
|
202
|
+
: this.cacheConfig.defaultTTL + getCurrTime(),
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
|
|
206
|
+
logger.warn(
|
|
207
|
+
`Invalid parameter: priority due to out or range. It should be within 1 and 5.`
|
|
208
|
+
);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const item: CacheItem = this.fillCacheItem(
|
|
213
|
+
prefixedKey,
|
|
214
|
+
value,
|
|
215
|
+
cacheItemOptions
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
// check wether this item is too big;
|
|
219
|
+
if (item.byteSize > this.cacheConfig.itemMaxSize) {
|
|
220
|
+
logger.warn(
|
|
221
|
+
`Item with key: ${key} you are trying to put into is too big!`
|
|
222
|
+
);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// if key already in the cache, then delete it.
|
|
227
|
+
const presentKeyPrio: number = this.containsKey(key);
|
|
228
|
+
if (presentKeyPrio !== -1) {
|
|
229
|
+
this._removeItem(prefixedKey, presentKeyPrio - 1);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// pop out items in the cache when cache is full based on LRU
|
|
233
|
+
// first start from lowest priority cache list
|
|
234
|
+
let cacheListIdx = this.maxPriority - 1;
|
|
235
|
+
while (this._isCacheFull(item.byteSize) && cacheListIdx >= 0) {
|
|
236
|
+
if (!this.cacheList[cacheListIdx].isEmpty()) {
|
|
237
|
+
const popedItemKey = this.cacheList[cacheListIdx].getLastItem();
|
|
238
|
+
this._removeItem(popedItemKey, cacheListIdx);
|
|
239
|
+
} else {
|
|
240
|
+
cacheListIdx -= 1;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
this._setItem(prefixedKey, item, Number(item.priority) - 1);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Get item from cache. It will return null if item doesn’t exist or it has been expired.
|
|
249
|
+
* If you specified callback function in the options,
|
|
250
|
+
* then the function will be executed if no such item in the cache
|
|
251
|
+
* and finally put the return value into cache.
|
|
252
|
+
* Please make sure the callback function will return the value you want to put into the cache.
|
|
253
|
+
* The cache will abort output a warning:
|
|
254
|
+
* If the key is invalid
|
|
255
|
+
*
|
|
256
|
+
* @param key - the key of the item
|
|
257
|
+
* @param options - the options of callback function
|
|
258
|
+
*/
|
|
259
|
+
public getItem(key: string, options?: CacheItemOptions): any {
|
|
260
|
+
let ret: string | null = null;
|
|
261
|
+
const prefixedKey: string = this.cacheConfig.keyPrefix + key;
|
|
262
|
+
|
|
263
|
+
if (
|
|
264
|
+
prefixedKey === this.cacheConfig.keyPrefix ||
|
|
265
|
+
prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)
|
|
266
|
+
) {
|
|
267
|
+
logger.warn(`Invalid key: should not be empty or 'CurSize'`);
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// check whether it's in the cachelist
|
|
272
|
+
const presentKeyPrio: number = this.containsKey(key);
|
|
273
|
+
if (presentKeyPrio !== -1) {
|
|
274
|
+
if (this._isExpired(prefixedKey)) {
|
|
275
|
+
// if expired, remove that item and return null
|
|
276
|
+
this._removeItem(prefixedKey, presentKeyPrio - 1);
|
|
277
|
+
} else {
|
|
278
|
+
// if not expired, great, return the value and refresh it
|
|
279
|
+
ret = CacheObject.getItem(prefixedKey) ?? '';
|
|
280
|
+
const item: CacheItem = JSON.parse(ret);
|
|
281
|
+
this.cacheList[item.priority - 1].refresh(prefixedKey);
|
|
282
|
+
return item.data;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (options && options.callback !== undefined) {
|
|
287
|
+
const val: object | string | number | boolean = options.callback();
|
|
288
|
+
if (val !== null) {
|
|
289
|
+
this.setItem(key, val, options);
|
|
290
|
+
}
|
|
291
|
+
return val;
|
|
292
|
+
}
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* remove item from the cache
|
|
298
|
+
*
|
|
299
|
+
* @param key - the key of the item
|
|
300
|
+
*/
|
|
301
|
+
public removeItem(key: string): void {
|
|
302
|
+
const prefixedKey: string = this.cacheConfig.keyPrefix + key;
|
|
303
|
+
|
|
304
|
+
// check if the key is in the cache
|
|
305
|
+
const presentKeyPrio: number = this.containsKey(key);
|
|
306
|
+
if (presentKeyPrio !== -1) {
|
|
307
|
+
this._removeItem(prefixedKey, presentKeyPrio - 1);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* clear the entire cache
|
|
313
|
+
*/
|
|
314
|
+
public clear(): void {
|
|
315
|
+
for (let i = 0; i < this.maxPriority; i += 1) {
|
|
316
|
+
for (const key of this.cacheList[i].getKeys()) {
|
|
317
|
+
this._removeItem(key, i);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Return all the keys in the cache.
|
|
324
|
+
*/
|
|
325
|
+
public getAllKeys(): string[] {
|
|
326
|
+
const keys: string[] = [];
|
|
327
|
+
for (let i = 0; i < this.maxPriority; i += 1) {
|
|
328
|
+
for (const key of this.cacheList[i].getKeys()) {
|
|
329
|
+
keys.push(key.substring(this.cacheConfig.keyPrefix.length));
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return keys;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* return the current size of the cache
|
|
338
|
+
*
|
|
339
|
+
* @return the current size of the cache
|
|
340
|
+
*/
|
|
341
|
+
public getCacheCurSize(): number {
|
|
342
|
+
return this.curSizeInBytes;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Return a new instance of cache with customized configuration.
|
|
347
|
+
* @param config - the customized configuration
|
|
348
|
+
*/
|
|
349
|
+
public createInstance(config: CacheConfig): ICache {
|
|
350
|
+
return new InMemoryCacheClass(config);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
export const InMemoryCache: ICache = new InMemoryCacheClass();
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { getCurrTime, getByteLength, defaultConfig } from './Utils';
|
|
5
|
+
import { AmplifyV6 } from '../singleton';
|
|
6
|
+
import { CacheConfig, CacheItem, CacheItemOptions } from './types';
|
|
7
|
+
import { ConsoleLogger as Logger } from '../Logger';
|
|
8
|
+
|
|
9
|
+
const logger = new Logger('StorageCache');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Initialization of the cache
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
export class StorageCache {
|
|
16
|
+
// Contains any fields that have been customized for this Cache instance (i.e. without default values)
|
|
17
|
+
private instanceConfig?: CacheConfig;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Initialize the cache
|
|
21
|
+
*
|
|
22
|
+
* @param config - Custom configuration for this instance.
|
|
23
|
+
*/
|
|
24
|
+
constructor(config?: CacheConfig) {
|
|
25
|
+
if (config) {
|
|
26
|
+
// A configuration was specified for this specific instance
|
|
27
|
+
this.instanceConfig = config;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
this.sanitizeConfig();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public getModuleName() {
|
|
34
|
+
return 'Cache';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private sanitizeConfig(): void {
|
|
38
|
+
const tempInstanceConfig = this.instanceConfig || ({} as CacheConfig);
|
|
39
|
+
|
|
40
|
+
if (this.cacheConfig.itemMaxSize > this.cacheConfig.capacityInBytes) {
|
|
41
|
+
logger.error(
|
|
42
|
+
'Invalid parameter: itemMaxSize. It should be smaller than capacityInBytes. Setting back to default.'
|
|
43
|
+
);
|
|
44
|
+
tempInstanceConfig.itemMaxSize = defaultConfig.itemMaxSize;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (
|
|
48
|
+
this.cacheConfig.defaultPriority > 5 ||
|
|
49
|
+
this.cacheConfig.defaultPriority < 1
|
|
50
|
+
) {
|
|
51
|
+
logger.error(
|
|
52
|
+
'Invalid parameter: defaultPriority. It should be between 1 and 5. Setting back to default.'
|
|
53
|
+
);
|
|
54
|
+
tempInstanceConfig.defaultPriority = defaultConfig.defaultPriority;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (
|
|
58
|
+
Number(this.cacheConfig.warningThreshold) > 1 ||
|
|
59
|
+
Number(this.cacheConfig.warningThreshold) < 0
|
|
60
|
+
) {
|
|
61
|
+
logger.error(
|
|
62
|
+
'Invalid parameter: warningThreshold. It should be between 0 and 1. Setting back to default.'
|
|
63
|
+
);
|
|
64
|
+
tempInstanceConfig.warningThreshold = defaultConfig.warningThreshold;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Set 5MB limit
|
|
68
|
+
const cacheLimit: number = 5 * 1024 * 1024;
|
|
69
|
+
if (this.cacheConfig.capacityInBytes > cacheLimit) {
|
|
70
|
+
logger.error(
|
|
71
|
+
'Cache Capacity should be less than 5MB. Setting back to default. Setting back to default.'
|
|
72
|
+
);
|
|
73
|
+
tempInstanceConfig.capacityInBytes = defaultConfig.capacityInBytes;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Apply sanitized values to the instance config
|
|
77
|
+
if (Object.keys(tempInstanceConfig).length > 0) {
|
|
78
|
+
this.instanceConfig = tempInstanceConfig;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* produce a JSON object with meta-data and data value
|
|
84
|
+
* @param value - the value of the item
|
|
85
|
+
* @param options - optional, the specified meta-data
|
|
86
|
+
*
|
|
87
|
+
* @return - the item which has the meta-data and the value
|
|
88
|
+
*/
|
|
89
|
+
protected fillCacheItem(
|
|
90
|
+
key: string,
|
|
91
|
+
value: object | number | string | boolean,
|
|
92
|
+
options: CacheItemOptions
|
|
93
|
+
): CacheItem {
|
|
94
|
+
const ret: CacheItem = {
|
|
95
|
+
key,
|
|
96
|
+
data: value,
|
|
97
|
+
timestamp: getCurrTime(),
|
|
98
|
+
visitedTime: getCurrTime(),
|
|
99
|
+
priority: options.priority ?? 0,
|
|
100
|
+
expires: options.expires ?? 0,
|
|
101
|
+
type: typeof value,
|
|
102
|
+
byteSize: 0,
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
ret.byteSize = getByteLength(JSON.stringify(ret));
|
|
106
|
+
|
|
107
|
+
// for accurate size
|
|
108
|
+
ret.byteSize = getByteLength(JSON.stringify(ret));
|
|
109
|
+
return ret;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Set custom configuration for the cache instance.
|
|
114
|
+
*
|
|
115
|
+
* @param config - customized configuration (without keyPrefix, which can't be changed)
|
|
116
|
+
*
|
|
117
|
+
* @return - the current configuration
|
|
118
|
+
*/
|
|
119
|
+
public configure(config?: Omit<CacheConfig, 'keyPrefix'>): CacheConfig {
|
|
120
|
+
if (config) {
|
|
121
|
+
if ((config as CacheConfig).keyPrefix) {
|
|
122
|
+
logger.warn(
|
|
123
|
+
'keyPrefix can not be re-configured on an existing Cache instance.'
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
this.instanceConfig = this.instanceConfig
|
|
128
|
+
? Object.assign({}, this.instanceConfig, config)
|
|
129
|
+
: (config as CacheConfig);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
this.sanitizeConfig();
|
|
133
|
+
|
|
134
|
+
return this.cacheConfig;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Returns an appropriate configuration for the Cache instance. Will apply any custom configuration for this
|
|
139
|
+
* instance on top of the global configuration. Default configuration will be applied in all cases.
|
|
140
|
+
*
|
|
141
|
+
* @internal
|
|
142
|
+
*/
|
|
143
|
+
protected get cacheConfig(): CacheConfig {
|
|
144
|
+
const globalCacheConfig = AmplifyV6.getConfig().Cache || {};
|
|
145
|
+
|
|
146
|
+
if (this.instanceConfig) {
|
|
147
|
+
return Object.assign(
|
|
148
|
+
{},
|
|
149
|
+
defaultConfig,
|
|
150
|
+
globalCacheConfig,
|
|
151
|
+
this.instanceConfig
|
|
152
|
+
);
|
|
153
|
+
} else {
|
|
154
|
+
return Object.assign({}, defaultConfig, globalCacheConfig);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { asserts } from '../../Util/errors/AssertError';
|
|
5
|
+
import { CACHE_LIST_EXCEPTION } from '../../constants';
|
|
6
|
+
|
|
7
|
+
class DoubleLinkedNode {
|
|
8
|
+
key: string;
|
|
9
|
+
prevNode: DoubleLinkedNode | null;
|
|
10
|
+
nextNode: DoubleLinkedNode | null;
|
|
11
|
+
|
|
12
|
+
constructor(keyVal?: string) {
|
|
13
|
+
this.key = keyVal ? keyVal : '';
|
|
14
|
+
this.prevNode = null;
|
|
15
|
+
this.nextNode = null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* double linked list plus a hash table inside
|
|
21
|
+
* each key in the cache stored as a node in the list
|
|
22
|
+
* recently visited node will be rotated to the head
|
|
23
|
+
* so the Last Recently Visited node will be at the tail
|
|
24
|
+
*
|
|
25
|
+
* @member head - dummy head of the linked list
|
|
26
|
+
* @member tail - dummy tail of the linked list
|
|
27
|
+
* @member hashtable - the hashtable which maps cache key to list node
|
|
28
|
+
* @member length - length of the list
|
|
29
|
+
*/
|
|
30
|
+
export default class CacheList {
|
|
31
|
+
private head: DoubleLinkedNode;
|
|
32
|
+
private tail: DoubleLinkedNode;
|
|
33
|
+
private hashtable: Record<string, any>;
|
|
34
|
+
private length: number;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* initialization
|
|
38
|
+
*/
|
|
39
|
+
constructor() {
|
|
40
|
+
this.head = new DoubleLinkedNode();
|
|
41
|
+
this.tail = new DoubleLinkedNode();
|
|
42
|
+
this.hashtable = {};
|
|
43
|
+
this.length = 0;
|
|
44
|
+
|
|
45
|
+
this.head.nextNode = this.tail;
|
|
46
|
+
this.tail.prevNode = this.head;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* insert node to the head of the list
|
|
51
|
+
*
|
|
52
|
+
* @param node
|
|
53
|
+
*/
|
|
54
|
+
private insertNodeToHead(node: DoubleLinkedNode) {
|
|
55
|
+
const tmp: DoubleLinkedNode | null = this.head.nextNode;
|
|
56
|
+
this.head.nextNode = node;
|
|
57
|
+
node.nextNode = tmp;
|
|
58
|
+
node.prevNode = this.head;
|
|
59
|
+
asserts(tmp !== null, {
|
|
60
|
+
name: CACHE_LIST_EXCEPTION,
|
|
61
|
+
message: 'previous node is null',
|
|
62
|
+
});
|
|
63
|
+
tmp.prevNode = node;
|
|
64
|
+
|
|
65
|
+
this.length = this.length + 1;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* remove node
|
|
70
|
+
*
|
|
71
|
+
* @param node
|
|
72
|
+
*/
|
|
73
|
+
private removeNode(node: DoubleLinkedNode): void {
|
|
74
|
+
asserts(node.prevNode !== null, {
|
|
75
|
+
name: CACHE_LIST_EXCEPTION,
|
|
76
|
+
message: 'previous node is null',
|
|
77
|
+
});
|
|
78
|
+
asserts(node.nextNode !== null, {
|
|
79
|
+
name: CACHE_LIST_EXCEPTION,
|
|
80
|
+
message: 'nextNode node is null',
|
|
81
|
+
});
|
|
82
|
+
node.prevNode.nextNode = node.nextNode;
|
|
83
|
+
node.nextNode.prevNode = node.prevNode;
|
|
84
|
+
|
|
85
|
+
node.prevNode = null;
|
|
86
|
+
node.nextNode = null;
|
|
87
|
+
|
|
88
|
+
this.length = this.length - 1;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @return true if list is empty
|
|
93
|
+
*/
|
|
94
|
+
public isEmpty(): boolean {
|
|
95
|
+
return this.length === 0;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* refresh node so it is rotated to the head
|
|
100
|
+
*
|
|
101
|
+
* @param key - key of the node
|
|
102
|
+
*/
|
|
103
|
+
public refresh(key: string): void {
|
|
104
|
+
const node: DoubleLinkedNode = this.hashtable[key];
|
|
105
|
+
this.removeNode(node);
|
|
106
|
+
this.insertNodeToHead(node);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* insert new node to the head and add it in the hashtable
|
|
111
|
+
*
|
|
112
|
+
* @param key - the key of the node
|
|
113
|
+
*/
|
|
114
|
+
public insertItem(key: string): void {
|
|
115
|
+
const node: DoubleLinkedNode = new DoubleLinkedNode(key);
|
|
116
|
+
this.hashtable[key] = node;
|
|
117
|
+
this.insertNodeToHead(node);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @return the LAST Recently Visited key
|
|
122
|
+
*/
|
|
123
|
+
public getLastItem(): string {
|
|
124
|
+
asserts(this.tail.prevNode !== null, {
|
|
125
|
+
name: CACHE_LIST_EXCEPTION,
|
|
126
|
+
message: 'previous node is null',
|
|
127
|
+
});
|
|
128
|
+
return this.tail.prevNode.key;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* remove the cache key from the list and hashtable
|
|
133
|
+
* @param key - the key of the node
|
|
134
|
+
*/
|
|
135
|
+
public removeItem(key: string): void {
|
|
136
|
+
const removedItem: DoubleLinkedNode = this.hashtable[key];
|
|
137
|
+
this.removeNode(removedItem);
|
|
138
|
+
delete this.hashtable[key];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @return length of the list
|
|
143
|
+
*/
|
|
144
|
+
public getSize(): number {
|
|
145
|
+
return this.length;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* @return true if the key is in the hashtable
|
|
150
|
+
* @param key
|
|
151
|
+
*/
|
|
152
|
+
public containsKey(key: string): boolean {
|
|
153
|
+
return key in this.hashtable;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* clean up the list and hashtable
|
|
158
|
+
*/
|
|
159
|
+
public clearList(): void {
|
|
160
|
+
for (const key of Object.keys(this.hashtable)) {
|
|
161
|
+
if (this.hashtable.hasOwnProperty(key)) {
|
|
162
|
+
delete this.hashtable[key];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
this.head.nextNode = this.tail;
|
|
166
|
+
this.tail.prevNode = this.head;
|
|
167
|
+
this.length = 0;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* @return all keys in the hashtable
|
|
172
|
+
*/
|
|
173
|
+
public getKeys(): string[] {
|
|
174
|
+
return Object.keys(this.hashtable);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* mainly for test
|
|
179
|
+
*
|
|
180
|
+
* @param key
|
|
181
|
+
* @return true if key is the head node
|
|
182
|
+
*/
|
|
183
|
+
public isHeadNode(key: string): boolean {
|
|
184
|
+
const node = this.hashtable[key];
|
|
185
|
+
return node.prevNode === this.head;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* mainly for test
|
|
190
|
+
*
|
|
191
|
+
* @param key
|
|
192
|
+
* @return true if key is the tail node
|
|
193
|
+
*/
|
|
194
|
+
public isTailNode(key: string): boolean {
|
|
195
|
+
const node = this.hashtable[key];
|
|
196
|
+
return node.nextNode === this.tail;
|
|
197
|
+
}
|
|
198
|
+
}
|