@aws-amplify/core 5.8.2-unstable.c3ec8a5.0 → 6.0.1-console-preview.7919075.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 +160 -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 +156 -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 +144 -0
- package/src/singleton/types.ts +48 -0
- package/src/types/types.ts +38 -1
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { StorageCache } from './StorageCache';
|
|
2
|
+
import { ICache, CacheConfig, CacheItemOptions } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Customized in-memory cache with LRU implemented
|
|
5
|
+
* @member cacheObj - object which store items
|
|
6
|
+
* @member cacheList - list of keys in the cache with LRU
|
|
7
|
+
* @member curSizeInBytes - current size of the cache
|
|
8
|
+
* @member maxPriority - max of the priority
|
|
9
|
+
*/
|
|
10
|
+
export declare class InMemoryCacheClass extends StorageCache implements ICache {
|
|
11
|
+
private cacheList;
|
|
12
|
+
private curSizeInBytes;
|
|
13
|
+
private maxPriority;
|
|
14
|
+
/**
|
|
15
|
+
* initialize the cache
|
|
16
|
+
*
|
|
17
|
+
* @param config - the configuration of the cache
|
|
18
|
+
*/
|
|
19
|
+
constructor(config?: CacheConfig);
|
|
20
|
+
/**
|
|
21
|
+
* decrease current size of the cache
|
|
22
|
+
*
|
|
23
|
+
* @param amount - the amount of the cache size which needs to be decreased
|
|
24
|
+
*/
|
|
25
|
+
private _decreaseCurSizeInBytes;
|
|
26
|
+
/**
|
|
27
|
+
* increase current size of the cache
|
|
28
|
+
*
|
|
29
|
+
* @param amount - the amount of the cache szie which need to be increased
|
|
30
|
+
*/
|
|
31
|
+
private _increaseCurSizeInBytes;
|
|
32
|
+
/**
|
|
33
|
+
* check whether item is expired
|
|
34
|
+
*
|
|
35
|
+
* @param key - the key of the item
|
|
36
|
+
*
|
|
37
|
+
* @return true if the item is expired.
|
|
38
|
+
*/
|
|
39
|
+
private _isExpired;
|
|
40
|
+
/**
|
|
41
|
+
* delete item from cache
|
|
42
|
+
*
|
|
43
|
+
* @param prefixedKey - the key of the item
|
|
44
|
+
* @param listIdx - indicates which cache list the key belongs to
|
|
45
|
+
*/
|
|
46
|
+
private _removeItem;
|
|
47
|
+
/**
|
|
48
|
+
* put item into cache
|
|
49
|
+
*
|
|
50
|
+
* @param prefixedKey - the key of the item
|
|
51
|
+
* @param itemData - the value of the item
|
|
52
|
+
* @param itemSizeInBytes - the byte size of the item
|
|
53
|
+
* @param listIdx - indicates which cache list the key belongs to
|
|
54
|
+
*/
|
|
55
|
+
private _setItem;
|
|
56
|
+
/**
|
|
57
|
+
* see whether cache is full
|
|
58
|
+
*
|
|
59
|
+
* @param itemSize
|
|
60
|
+
*
|
|
61
|
+
* @return true if cache is full
|
|
62
|
+
*/
|
|
63
|
+
private _isCacheFull;
|
|
64
|
+
/**
|
|
65
|
+
* check whether the cache contains the key
|
|
66
|
+
*
|
|
67
|
+
* @param key
|
|
68
|
+
*/
|
|
69
|
+
private containsKey;
|
|
70
|
+
/**
|
|
71
|
+
* * Set item into cache. You can put number, string, boolean or object.
|
|
72
|
+
* The cache will first check whether has the same key.
|
|
73
|
+
* If it has, it will delete the old item and then put the new item in
|
|
74
|
+
* The cache will pop out items if it is full
|
|
75
|
+
* You can specify the cache item options. The cache will abort and output a warning:
|
|
76
|
+
* If the key is invalid
|
|
77
|
+
* If the size of the item exceeds itemMaxSize.
|
|
78
|
+
* If the value is undefined
|
|
79
|
+
* If incorrect cache item configuration
|
|
80
|
+
* If error happened with browser storage
|
|
81
|
+
*
|
|
82
|
+
* @param key - the key of the item
|
|
83
|
+
* @param value - the value of the item
|
|
84
|
+
* @param options - optional, the specified meta-data
|
|
85
|
+
*
|
|
86
|
+
* @throws if the item is too big which exceeds the limit of single item size
|
|
87
|
+
* @throws if the key is invalid
|
|
88
|
+
*/
|
|
89
|
+
setItem(key: string, value: object | string | number | boolean, options?: CacheItemOptions): void;
|
|
90
|
+
/**
|
|
91
|
+
* Get item from cache. It will return null if item doesn’t exist or it has been expired.
|
|
92
|
+
* If you specified callback function in the options,
|
|
93
|
+
* then the function will be executed if no such item in the cache
|
|
94
|
+
* and finally put the return value into cache.
|
|
95
|
+
* Please make sure the callback function will return the value you want to put into the cache.
|
|
96
|
+
* The cache will abort output a warning:
|
|
97
|
+
* If the key is invalid
|
|
98
|
+
*
|
|
99
|
+
* @param key - the key of the item
|
|
100
|
+
* @param options - the options of callback function
|
|
101
|
+
*/
|
|
102
|
+
getItem(key: string, options?: CacheItemOptions): any;
|
|
103
|
+
/**
|
|
104
|
+
* remove item from the cache
|
|
105
|
+
*
|
|
106
|
+
* @param key - the key of the item
|
|
107
|
+
*/
|
|
108
|
+
removeItem(key: string): void;
|
|
109
|
+
/**
|
|
110
|
+
* clear the entire cache
|
|
111
|
+
*/
|
|
112
|
+
clear(): void;
|
|
113
|
+
/**
|
|
114
|
+
* Return all the keys in the cache.
|
|
115
|
+
*/
|
|
116
|
+
getAllKeys(): string[];
|
|
117
|
+
/**
|
|
118
|
+
* return the current size of the cache
|
|
119
|
+
*
|
|
120
|
+
* @return the current size of the cache
|
|
121
|
+
*/
|
|
122
|
+
getCacheCurSize(): number;
|
|
123
|
+
/**
|
|
124
|
+
* Return a new instance of cache with customized configuration.
|
|
125
|
+
* @param config - the customized configuration
|
|
126
|
+
*/
|
|
127
|
+
createInstance(config: CacheConfig): ICache;
|
|
128
|
+
}
|
|
129
|
+
export declare const InMemoryCache: ICache;
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
var __extends = (this && this.__extends) || (function () {
|
|
5
|
+
var extendStatics = function (d, b) {
|
|
6
|
+
extendStatics = Object.setPrototypeOf ||
|
|
7
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
8
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
9
|
+
return extendStatics(d, b);
|
|
10
|
+
};
|
|
11
|
+
return function (d, b) {
|
|
12
|
+
if (typeof b !== "function" && b !== null)
|
|
13
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
14
|
+
extendStatics(d, b);
|
|
15
|
+
function __() { this.constructor = d; }
|
|
16
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
17
|
+
};
|
|
18
|
+
})();
|
|
19
|
+
var __values = (this && this.__values) || function(o) {
|
|
20
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
21
|
+
if (m) return m.call(o);
|
|
22
|
+
if (o && typeof o.length === "number") return {
|
|
23
|
+
next: function () {
|
|
24
|
+
if (o && i >= o.length) o = void 0;
|
|
25
|
+
return { value: o && o[i++], done: !o };
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.InMemoryCache = exports.InMemoryCacheClass = void 0;
|
|
32
|
+
var Utils_1 = require("./Utils");
|
|
33
|
+
var StorageCache_1 = require("./StorageCache");
|
|
34
|
+
var Logger_1 = require("../Logger");
|
|
35
|
+
var AssertError_1 = require("../Util/errors/AssertError");
|
|
36
|
+
var constants_1 = require("../constants");
|
|
37
|
+
var CacheUtils_1 = require("./Utils/CacheUtils");
|
|
38
|
+
var logger = new Logger_1.ConsoleLogger('InMemoryCache');
|
|
39
|
+
/**
|
|
40
|
+
* Customized in-memory cache with LRU implemented
|
|
41
|
+
* @member cacheObj - object which store items
|
|
42
|
+
* @member cacheList - list of keys in the cache with LRU
|
|
43
|
+
* @member curSizeInBytes - current size of the cache
|
|
44
|
+
* @member maxPriority - max of the priority
|
|
45
|
+
*/
|
|
46
|
+
var InMemoryCacheClass = /** @class */ (function (_super) {
|
|
47
|
+
__extends(InMemoryCacheClass, _super);
|
|
48
|
+
/**
|
|
49
|
+
* initialize the cache
|
|
50
|
+
*
|
|
51
|
+
* @param config - the configuration of the cache
|
|
52
|
+
*/
|
|
53
|
+
function InMemoryCacheClass(config) {
|
|
54
|
+
var _this = _super.call(this, config) || this;
|
|
55
|
+
_this.cacheList = [];
|
|
56
|
+
_this.curSizeInBytes = 0;
|
|
57
|
+
_this.maxPriority = 5;
|
|
58
|
+
_this.getItem = _this.getItem.bind(_this);
|
|
59
|
+
_this.setItem = _this.setItem.bind(_this);
|
|
60
|
+
_this.removeItem = _this.removeItem.bind(_this);
|
|
61
|
+
// initialize list for every priority
|
|
62
|
+
for (var i = 0; i < _this.maxPriority; i += 1) {
|
|
63
|
+
_this.cacheList[i] = new Utils_1.CacheList();
|
|
64
|
+
}
|
|
65
|
+
return _this;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* decrease current size of the cache
|
|
69
|
+
*
|
|
70
|
+
* @param amount - the amount of the cache size which needs to be decreased
|
|
71
|
+
*/
|
|
72
|
+
InMemoryCacheClass.prototype._decreaseCurSizeInBytes = function (amount) {
|
|
73
|
+
this.curSizeInBytes -= amount;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* increase current size of the cache
|
|
77
|
+
*
|
|
78
|
+
* @param amount - the amount of the cache szie which need to be increased
|
|
79
|
+
*/
|
|
80
|
+
InMemoryCacheClass.prototype._increaseCurSizeInBytes = function (amount) {
|
|
81
|
+
this.curSizeInBytes += amount;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* check whether item is expired
|
|
85
|
+
*
|
|
86
|
+
* @param key - the key of the item
|
|
87
|
+
*
|
|
88
|
+
* @return true if the item is expired.
|
|
89
|
+
*/
|
|
90
|
+
InMemoryCacheClass.prototype._isExpired = function (key) {
|
|
91
|
+
var text = Utils_1.CacheObject.getItem(key);
|
|
92
|
+
(0, AssertError_1.asserts)(text !== null, {
|
|
93
|
+
name: constants_1.STORAGE_CACHE_EXCEPTION,
|
|
94
|
+
message: 'item from storage is null',
|
|
95
|
+
});
|
|
96
|
+
var item = JSON.parse(text);
|
|
97
|
+
if ((0, Utils_1.getCurrTime)() >= item.expires) {
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* delete item from cache
|
|
104
|
+
*
|
|
105
|
+
* @param prefixedKey - the key of the item
|
|
106
|
+
* @param listIdx - indicates which cache list the key belongs to
|
|
107
|
+
*/
|
|
108
|
+
InMemoryCacheClass.prototype._removeItem = function (prefixedKey, listIdx) {
|
|
109
|
+
// delete the key from the list
|
|
110
|
+
this.cacheList[listIdx].removeItem(prefixedKey);
|
|
111
|
+
// decrease the current size of the cache
|
|
112
|
+
var item = Utils_1.CacheObject.getItem(prefixedKey);
|
|
113
|
+
(0, AssertError_1.asserts)(item !== null, {
|
|
114
|
+
name: constants_1.STORAGE_CACHE_EXCEPTION,
|
|
115
|
+
message: 'item from storage is null',
|
|
116
|
+
});
|
|
117
|
+
this._decreaseCurSizeInBytes(JSON.parse(item).byteSize);
|
|
118
|
+
// finally remove the item from memory
|
|
119
|
+
Utils_1.CacheObject.removeItem(prefixedKey);
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* put item into cache
|
|
123
|
+
*
|
|
124
|
+
* @param prefixedKey - the key of the item
|
|
125
|
+
* @param itemData - the value of the item
|
|
126
|
+
* @param itemSizeInBytes - the byte size of the item
|
|
127
|
+
* @param listIdx - indicates which cache list the key belongs to
|
|
128
|
+
*/
|
|
129
|
+
InMemoryCacheClass.prototype._setItem = function (prefixedKey, item, listIdx) {
|
|
130
|
+
// insert the key into the list
|
|
131
|
+
this.cacheList[listIdx].insertItem(prefixedKey);
|
|
132
|
+
// increase the current size of the cache
|
|
133
|
+
this._increaseCurSizeInBytes(item.byteSize);
|
|
134
|
+
// finally add the item into memory
|
|
135
|
+
Utils_1.CacheObject.setItem(prefixedKey, JSON.stringify(item));
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* see whether cache is full
|
|
139
|
+
*
|
|
140
|
+
* @param itemSize
|
|
141
|
+
*
|
|
142
|
+
* @return true if cache is full
|
|
143
|
+
*/
|
|
144
|
+
InMemoryCacheClass.prototype._isCacheFull = function (itemSize) {
|
|
145
|
+
return this.curSizeInBytes + itemSize > this.cacheConfig.capacityInBytes;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* check whether the cache contains the key
|
|
149
|
+
*
|
|
150
|
+
* @param key
|
|
151
|
+
*/
|
|
152
|
+
InMemoryCacheClass.prototype.containsKey = function (key) {
|
|
153
|
+
var prefixedKey = this.cacheConfig.keyPrefix + key;
|
|
154
|
+
for (var i = 0; i < this.maxPriority; i += 1) {
|
|
155
|
+
if (this.cacheList[i].containsKey(prefixedKey)) {
|
|
156
|
+
return i + 1;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return -1;
|
|
160
|
+
};
|
|
161
|
+
/**
|
|
162
|
+
* * Set item into cache. You can put number, string, boolean or object.
|
|
163
|
+
* The cache will first check whether has the same key.
|
|
164
|
+
* If it has, it will delete the old item and then put the new item in
|
|
165
|
+
* The cache will pop out items if it is full
|
|
166
|
+
* You can specify the cache item options. The cache will abort and output a warning:
|
|
167
|
+
* If the key is invalid
|
|
168
|
+
* If the size of the item exceeds itemMaxSize.
|
|
169
|
+
* If the value is undefined
|
|
170
|
+
* If incorrect cache item configuration
|
|
171
|
+
* If error happened with browser storage
|
|
172
|
+
*
|
|
173
|
+
* @param key - the key of the item
|
|
174
|
+
* @param value - the value of the item
|
|
175
|
+
* @param options - optional, the specified meta-data
|
|
176
|
+
*
|
|
177
|
+
* @throws if the item is too big which exceeds the limit of single item size
|
|
178
|
+
* @throws if the key is invalid
|
|
179
|
+
*/
|
|
180
|
+
InMemoryCacheClass.prototype.setItem = function (key, value, options) {
|
|
181
|
+
var prefixedKey = this.cacheConfig.keyPrefix + key;
|
|
182
|
+
// invalid keys
|
|
183
|
+
if (prefixedKey === this.cacheConfig.keyPrefix ||
|
|
184
|
+
prefixedKey === (0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix)) {
|
|
185
|
+
logger.warn("Invalid key: should not be empty or 'CurSize'");
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (typeof value === 'undefined') {
|
|
189
|
+
logger.warn("The value of item should not be undefined!");
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
var cacheItemOptions = {
|
|
193
|
+
priority: options && options.priority !== undefined
|
|
194
|
+
? options.priority
|
|
195
|
+
: this.cacheConfig.defaultPriority,
|
|
196
|
+
expires: options && options.expires !== undefined
|
|
197
|
+
? options.expires
|
|
198
|
+
: this.cacheConfig.defaultTTL + (0, Utils_1.getCurrTime)(),
|
|
199
|
+
};
|
|
200
|
+
if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
|
|
201
|
+
logger.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
var item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
|
|
205
|
+
// check wether this item is too big;
|
|
206
|
+
if (item.byteSize > this.cacheConfig.itemMaxSize) {
|
|
207
|
+
logger.warn("Item with key: ".concat(key, " you are trying to put into is too big!"));
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
// if key already in the cache, then delete it.
|
|
211
|
+
var presentKeyPrio = this.containsKey(key);
|
|
212
|
+
if (presentKeyPrio !== -1) {
|
|
213
|
+
this._removeItem(prefixedKey, presentKeyPrio - 1);
|
|
214
|
+
}
|
|
215
|
+
// pop out items in the cache when cache is full based on LRU
|
|
216
|
+
// first start from lowest priority cache list
|
|
217
|
+
var cacheListIdx = this.maxPriority - 1;
|
|
218
|
+
while (this._isCacheFull(item.byteSize) && cacheListIdx >= 0) {
|
|
219
|
+
if (!this.cacheList[cacheListIdx].isEmpty()) {
|
|
220
|
+
var popedItemKey = this.cacheList[cacheListIdx].getLastItem();
|
|
221
|
+
this._removeItem(popedItemKey, cacheListIdx);
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
cacheListIdx -= 1;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
this._setItem(prefixedKey, item, Number(item.priority) - 1);
|
|
228
|
+
};
|
|
229
|
+
/**
|
|
230
|
+
* Get item from cache. It will return null if item doesn’t exist or it has been expired.
|
|
231
|
+
* If you specified callback function in the options,
|
|
232
|
+
* then the function will be executed if no such item in the cache
|
|
233
|
+
* and finally put the return value into cache.
|
|
234
|
+
* Please make sure the callback function will return the value you want to put into the cache.
|
|
235
|
+
* The cache will abort output a warning:
|
|
236
|
+
* If the key is invalid
|
|
237
|
+
*
|
|
238
|
+
* @param key - the key of the item
|
|
239
|
+
* @param options - the options of callback function
|
|
240
|
+
*/
|
|
241
|
+
InMemoryCacheClass.prototype.getItem = function (key, options) {
|
|
242
|
+
var _a;
|
|
243
|
+
var ret = null;
|
|
244
|
+
var prefixedKey = this.cacheConfig.keyPrefix + key;
|
|
245
|
+
if (prefixedKey === this.cacheConfig.keyPrefix ||
|
|
246
|
+
prefixedKey === (0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix)) {
|
|
247
|
+
logger.warn("Invalid key: should not be empty or 'CurSize'");
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
// check whether it's in the cachelist
|
|
251
|
+
var presentKeyPrio = this.containsKey(key);
|
|
252
|
+
if (presentKeyPrio !== -1) {
|
|
253
|
+
if (this._isExpired(prefixedKey)) {
|
|
254
|
+
// if expired, remove that item and return null
|
|
255
|
+
this._removeItem(prefixedKey, presentKeyPrio - 1);
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
// if not expired, great, return the value and refresh it
|
|
259
|
+
ret = (_a = Utils_1.CacheObject.getItem(prefixedKey)) !== null && _a !== void 0 ? _a : '';
|
|
260
|
+
var item = JSON.parse(ret);
|
|
261
|
+
this.cacheList[item.priority - 1].refresh(prefixedKey);
|
|
262
|
+
return item.data;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (options && options.callback !== undefined) {
|
|
266
|
+
var val = options.callback();
|
|
267
|
+
if (val !== null) {
|
|
268
|
+
this.setItem(key, val, options);
|
|
269
|
+
}
|
|
270
|
+
return val;
|
|
271
|
+
}
|
|
272
|
+
return null;
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* remove item from the cache
|
|
276
|
+
*
|
|
277
|
+
* @param key - the key of the item
|
|
278
|
+
*/
|
|
279
|
+
InMemoryCacheClass.prototype.removeItem = function (key) {
|
|
280
|
+
var prefixedKey = this.cacheConfig.keyPrefix + key;
|
|
281
|
+
// check if the key is in the cache
|
|
282
|
+
var presentKeyPrio = this.containsKey(key);
|
|
283
|
+
if (presentKeyPrio !== -1) {
|
|
284
|
+
this._removeItem(prefixedKey, presentKeyPrio - 1);
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
/**
|
|
288
|
+
* clear the entire cache
|
|
289
|
+
*/
|
|
290
|
+
InMemoryCacheClass.prototype.clear = function () {
|
|
291
|
+
var e_1, _a;
|
|
292
|
+
for (var i = 0; i < this.maxPriority; i += 1) {
|
|
293
|
+
try {
|
|
294
|
+
for (var _b = (e_1 = void 0, __values(this.cacheList[i].getKeys())), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
295
|
+
var key = _c.value;
|
|
296
|
+
this._removeItem(key, i);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
300
|
+
finally {
|
|
301
|
+
try {
|
|
302
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
303
|
+
}
|
|
304
|
+
finally { if (e_1) throw e_1.error; }
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
/**
|
|
309
|
+
* Return all the keys in the cache.
|
|
310
|
+
*/
|
|
311
|
+
InMemoryCacheClass.prototype.getAllKeys = function () {
|
|
312
|
+
var e_2, _a;
|
|
313
|
+
var keys = [];
|
|
314
|
+
for (var i = 0; i < this.maxPriority; i += 1) {
|
|
315
|
+
try {
|
|
316
|
+
for (var _b = (e_2 = void 0, __values(this.cacheList[i].getKeys())), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
317
|
+
var key = _c.value;
|
|
318
|
+
keys.push(key.substring(this.cacheConfig.keyPrefix.length));
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
322
|
+
finally {
|
|
323
|
+
try {
|
|
324
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
325
|
+
}
|
|
326
|
+
finally { if (e_2) throw e_2.error; }
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return keys;
|
|
330
|
+
};
|
|
331
|
+
/**
|
|
332
|
+
* return the current size of the cache
|
|
333
|
+
*
|
|
334
|
+
* @return the current size of the cache
|
|
335
|
+
*/
|
|
336
|
+
InMemoryCacheClass.prototype.getCacheCurSize = function () {
|
|
337
|
+
return this.curSizeInBytes;
|
|
338
|
+
};
|
|
339
|
+
/**
|
|
340
|
+
* Return a new instance of cache with customized configuration.
|
|
341
|
+
* @param config - the customized configuration
|
|
342
|
+
*/
|
|
343
|
+
InMemoryCacheClass.prototype.createInstance = function (config) {
|
|
344
|
+
return new InMemoryCacheClass(config);
|
|
345
|
+
};
|
|
346
|
+
return InMemoryCacheClass;
|
|
347
|
+
}(StorageCache_1.StorageCache));
|
|
348
|
+
exports.InMemoryCacheClass = InMemoryCacheClass;
|
|
349
|
+
exports.InMemoryCache = new InMemoryCacheClass();
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { CacheConfig, CacheItem, CacheItemOptions } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Initialization of the cache
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export declare class StorageCache {
|
|
7
|
+
private instanceConfig?;
|
|
8
|
+
/**
|
|
9
|
+
* Initialize the cache
|
|
10
|
+
*
|
|
11
|
+
* @param config - Custom configuration for this instance.
|
|
12
|
+
*/
|
|
13
|
+
constructor(config?: CacheConfig);
|
|
14
|
+
getModuleName(): string;
|
|
15
|
+
private sanitizeConfig;
|
|
16
|
+
/**
|
|
17
|
+
* produce a JSON object with meta-data and data value
|
|
18
|
+
* @param value - the value of the item
|
|
19
|
+
* @param options - optional, the specified meta-data
|
|
20
|
+
*
|
|
21
|
+
* @return - the item which has the meta-data and the value
|
|
22
|
+
*/
|
|
23
|
+
protected fillCacheItem(key: string, value: object | number | string | boolean, options: CacheItemOptions): CacheItem;
|
|
24
|
+
/**
|
|
25
|
+
* Set custom configuration for the cache instance.
|
|
26
|
+
*
|
|
27
|
+
* @param config - customized configuration (without keyPrefix, which can't be changed)
|
|
28
|
+
*
|
|
29
|
+
* @return - the current configuration
|
|
30
|
+
*/
|
|
31
|
+
configure(config?: Omit<CacheConfig, 'keyPrefix'>): CacheConfig;
|
|
32
|
+
/**
|
|
33
|
+
* Returns an appropriate configuration for the Cache instance. Will apply any custom configuration for this
|
|
34
|
+
* instance on top of the global configuration. Default configuration will be applied in all cases.
|
|
35
|
+
*
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
protected get cacheConfig(): CacheConfig;
|
|
39
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.StorageCache = void 0;
|
|
6
|
+
var Utils_1 = require("./Utils");
|
|
7
|
+
var singleton_1 = require("../singleton");
|
|
8
|
+
var Logger_1 = require("../Logger");
|
|
9
|
+
var logger = new Logger_1.ConsoleLogger('StorageCache');
|
|
10
|
+
/**
|
|
11
|
+
* Initialization of the cache
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
var StorageCache = /** @class */ (function () {
|
|
15
|
+
/**
|
|
16
|
+
* Initialize the cache
|
|
17
|
+
*
|
|
18
|
+
* @param config - Custom configuration for this instance.
|
|
19
|
+
*/
|
|
20
|
+
function StorageCache(config) {
|
|
21
|
+
if (config) {
|
|
22
|
+
// A configuration was specified for this specific instance
|
|
23
|
+
this.instanceConfig = config;
|
|
24
|
+
}
|
|
25
|
+
this.sanitizeConfig();
|
|
26
|
+
}
|
|
27
|
+
StorageCache.prototype.getModuleName = function () {
|
|
28
|
+
return 'Cache';
|
|
29
|
+
};
|
|
30
|
+
StorageCache.prototype.sanitizeConfig = function () {
|
|
31
|
+
var tempInstanceConfig = this.instanceConfig || {};
|
|
32
|
+
if (this.cacheConfig.itemMaxSize > this.cacheConfig.capacityInBytes) {
|
|
33
|
+
logger.error('Invalid parameter: itemMaxSize. It should be smaller than capacityInBytes. Setting back to default.');
|
|
34
|
+
tempInstanceConfig.itemMaxSize = Utils_1.defaultConfig.itemMaxSize;
|
|
35
|
+
}
|
|
36
|
+
if (this.cacheConfig.defaultPriority > 5 ||
|
|
37
|
+
this.cacheConfig.defaultPriority < 1) {
|
|
38
|
+
logger.error('Invalid parameter: defaultPriority. It should be between 1 and 5. Setting back to default.');
|
|
39
|
+
tempInstanceConfig.defaultPriority = Utils_1.defaultConfig.defaultPriority;
|
|
40
|
+
}
|
|
41
|
+
if (Number(this.cacheConfig.warningThreshold) > 1 ||
|
|
42
|
+
Number(this.cacheConfig.warningThreshold) < 0) {
|
|
43
|
+
logger.error('Invalid parameter: warningThreshold. It should be between 0 and 1. Setting back to default.');
|
|
44
|
+
tempInstanceConfig.warningThreshold = Utils_1.defaultConfig.warningThreshold;
|
|
45
|
+
}
|
|
46
|
+
// Set 5MB limit
|
|
47
|
+
var cacheLimit = 5 * 1024 * 1024;
|
|
48
|
+
if (this.cacheConfig.capacityInBytes > cacheLimit) {
|
|
49
|
+
logger.error('Cache Capacity should be less than 5MB. Setting back to default. Setting back to default.');
|
|
50
|
+
tempInstanceConfig.capacityInBytes = Utils_1.defaultConfig.capacityInBytes;
|
|
51
|
+
}
|
|
52
|
+
// Apply sanitized values to the instance config
|
|
53
|
+
if (Object.keys(tempInstanceConfig).length > 0) {
|
|
54
|
+
this.instanceConfig = tempInstanceConfig;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* produce a JSON object with meta-data and data value
|
|
59
|
+
* @param value - the value of the item
|
|
60
|
+
* @param options - optional, the specified meta-data
|
|
61
|
+
*
|
|
62
|
+
* @return - the item which has the meta-data and the value
|
|
63
|
+
*/
|
|
64
|
+
StorageCache.prototype.fillCacheItem = function (key, value, options) {
|
|
65
|
+
var _a, _b;
|
|
66
|
+
var ret = {
|
|
67
|
+
key: key,
|
|
68
|
+
data: value,
|
|
69
|
+
timestamp: (0, Utils_1.getCurrTime)(),
|
|
70
|
+
visitedTime: (0, Utils_1.getCurrTime)(),
|
|
71
|
+
priority: (_a = options.priority) !== null && _a !== void 0 ? _a : 0,
|
|
72
|
+
expires: (_b = options.expires) !== null && _b !== void 0 ? _b : 0,
|
|
73
|
+
type: typeof value,
|
|
74
|
+
byteSize: 0,
|
|
75
|
+
};
|
|
76
|
+
ret.byteSize = (0, Utils_1.getByteLength)(JSON.stringify(ret));
|
|
77
|
+
// for accurate size
|
|
78
|
+
ret.byteSize = (0, Utils_1.getByteLength)(JSON.stringify(ret));
|
|
79
|
+
return ret;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Set custom configuration for the cache instance.
|
|
83
|
+
*
|
|
84
|
+
* @param config - customized configuration (without keyPrefix, which can't be changed)
|
|
85
|
+
*
|
|
86
|
+
* @return - the current configuration
|
|
87
|
+
*/
|
|
88
|
+
StorageCache.prototype.configure = function (config) {
|
|
89
|
+
if (config) {
|
|
90
|
+
if (config.keyPrefix) {
|
|
91
|
+
logger.warn('keyPrefix can not be re-configured on an existing Cache instance.');
|
|
92
|
+
}
|
|
93
|
+
this.instanceConfig = this.instanceConfig
|
|
94
|
+
? Object.assign({}, this.instanceConfig, config)
|
|
95
|
+
: config;
|
|
96
|
+
}
|
|
97
|
+
this.sanitizeConfig();
|
|
98
|
+
return this.cacheConfig;
|
|
99
|
+
};
|
|
100
|
+
Object.defineProperty(StorageCache.prototype, "cacheConfig", {
|
|
101
|
+
/**
|
|
102
|
+
* Returns an appropriate configuration for the Cache instance. Will apply any custom configuration for this
|
|
103
|
+
* instance on top of the global configuration. Default configuration will be applied in all cases.
|
|
104
|
+
*
|
|
105
|
+
* @internal
|
|
106
|
+
*/
|
|
107
|
+
get: function () {
|
|
108
|
+
var globalCacheConfig = singleton_1.AmplifyV6.getConfig().Cache || {};
|
|
109
|
+
if (this.instanceConfig) {
|
|
110
|
+
return Object.assign({}, Utils_1.defaultConfig, globalCacheConfig, this.instanceConfig);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
return Object.assign({}, Utils_1.defaultConfig, globalCacheConfig);
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
enumerable: false,
|
|
117
|
+
configurable: true
|
|
118
|
+
});
|
|
119
|
+
return StorageCache;
|
|
120
|
+
}());
|
|
121
|
+
exports.StorageCache = StorageCache;
|