UNPKG

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