UNPKG

5.46 kBTypeScriptView Raw
1import AsyncStorage from '@react-native-async-storage/async-storage';
2import { StorageCache } from './StorageCache';
3import { ICache } from './types';
4export declare class AsyncStorageCache extends StorageCache implements ICache {
5 /**
6 * initialize the cache
7 *
8 * @param {Object} config - the configuration of the cache
9 */
10 constructor(config?: any);
11 /**
12 * decrease current size of the cache
13 * @private
14 * @param amount - the amount of the cache size which needs to be decreased
15 */
16 _decreaseCurSizeInBytes(amount: any): Promise<void>;
17 /**
18 * increase current size of the cache
19 * @private
20 * @param amount - the amount of the cache szie which need to be increased
21 */
22 _increaseCurSizeInBytes(amount: any): Promise<void>;
23 /**
24 * update the visited time if item has been visited
25 * @private
26 * @param item - the item which need to be refreshed
27 * @param prefixedKey - the key of the item
28 *
29 * @return the refreshed item
30 */
31 _refreshItem(item: any, prefixedKey: any): Promise<any>;
32 /**
33 * check wether item is expired
34 * @private
35 * @param key - the key of the item
36 *
37 * @return true if the item is expired.
38 */
39 _isExpired(key: any): Promise<boolean>;
40 /**
41 * delete item from cache
42 * @private
43 * @param prefixedKey - the key of the item
44 * @param size - optional, the byte size of the item
45 */
46 _removeItem(prefixedKey: any, size?: any): Promise<void>;
47 /**
48 * put item into cache
49 * @private
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 */
54 _setItem(prefixedKey: any, item: any): Promise<void>;
55 /**
56 * total space needed when poping out items
57 * @private
58 * @param itemSize
59 *
60 * @return total space needed
61 */
62 _sizeToPop(itemSize: any): Promise<number>;
63 /**
64 * see whether cache is full
65 * @private
66 * @param itemSize
67 *
68 * @return true if cache is full
69 */
70 _isCacheFull(itemSize: any): Promise<boolean>;
71 /**
72 * scan the storage and find out all the keys owned by this cache
73 * also clean the expired keys while scanning
74 * @private
75 * @return array of keys
76 */
77 _findValidKeys(): Promise<any[]>;
78 /**
79 * get all the items we have, sort them by their priority,
80 * if priority is same, sort them by their last visited time
81 * pop out items from the low priority (5 is the lowest)
82 * @private
83 * @param keys - all the keys in this cache
84 * @param sizeToPop - the total size of the items which needed to be poped out
85 */
86 _popOutItems(keys: any, sizeToPop: any): Promise<void>;
87 /**
88 * Set item into cache. You can put number, string, boolean or object.
89 * The cache will first check whether has the same key.
90 * If it has, it will delete the old item and then put the new item in
91 * The cache will pop out items if it is full
92 * You can specify the cache item options. The cache will abort and output a warning:
93 * If the key is invalid
94 * If the size of the item exceeds itemMaxSize.
95 * If the value is undefined
96 * If incorrect cache item configuration
97 * If error happened with browser storage
98 *
99 * @param {String} key - the key of the item
100 * @param {Object} value - the value of the item
101 * @param {Object} [options] - optional, the specified meta-data
102 * @return {Prmoise}
103 */
104 setItem(key: any, value: any, options: any): Promise<void>;
105 /**
106 * Get item from cache. It will return null if item doesn’t exist or it has been expired.
107 * If you specified callback function in the options,
108 * then the function will be executed if no such item in the cache
109 * and finally put the return value into cache.
110 * Please make sure the callback function will return the value you want to put into the cache.
111 * The cache will abort output a warning:
112 * If the key is invalid
113 * If error happened with AsyncStorage
114 *
115 * @param {String} key - the key of the item
116 * @param {Object} [options] - the options of callback function
117 * @return {Promise} - return a promise resolves to be the value of the item
118 */
119 getItem(key: any, options: any): Promise<any>;
120 /**
121 * remove item from the cache
122 * The cache will abort output a warning:
123 * If error happened with AsyncStorage
124 * @param {String} key - the key of the item
125 * @return {Promise}
126 */
127 removeItem(key: any): Promise<void>;
128 /**
129 * clear the entire cache
130 * The cache will abort output a warning:
131 * If error happened with AsyncStorage
132 * @return {Promise}
133 */
134 clear(): Promise<void>;
135 /**
136 * return the current size of the cache
137 * @return {Promise}
138 */
139 getCacheCurSize(): Promise<number>;
140 /**
141 * Return all the keys in the cache.
142 * Will return an empty array if error happend.
143 * @return {Promise}
144 */
145 getAllKeys(): Promise<any[]>;
146 /**
147 * Return a new instance of cache with customized configuration.
148 * @param {Object} config - the customized configuration
149 * @return {Object} - the new instance of Cache
150 */
151 createInstance(config: any): ICache;
152}
153declare const instance: ICache;
154export { AsyncStorage, instance as Cache };