UNPKG

4.29 kBTypeScriptView Raw
1import { StorageCache } from './StorageCache';
2import { 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 * @member cacheSizeLimit - the limit of cache size
10 */
11export declare class InMemoryCacheClass extends StorageCache implements ICache {
12 private cacheList;
13 private curSizeInBytes;
14 private maxPriority;
15 private cacheSizeLimit;
16 /**
17 * initialize the cache
18 *
19 * @param config - the configuration of the cache
20 */
21 constructor(config?: CacheConfig);
22 /**
23 * decrease current size of the cache
24 *
25 * @param amount - the amount of the cache size which needs to be decreased
26 */
27 private _decreaseCurSizeInBytes;
28 /**
29 * increase current size of the cache
30 *
31 * @param amount - the amount of the cache szie which need to be increased
32 */
33 private _increaseCurSizeInBytes;
34 /**
35 * check whether item is expired
36 *
37 * @param key - the key of the item
38 *
39 * @return true if the item is expired.
40 */
41 private _isExpired;
42 /**
43 * delete item from cache
44 *
45 * @param prefixedKey - the key of the item
46 * @param listIdx - indicates which cache list the key belongs to
47 */
48 private _removeItem;
49 /**
50 * put item into cache
51 *
52 * @param prefixedKey - the key of the item
53 * @param itemData - the value of the item
54 * @param itemSizeInBytes - the byte size of the item
55 * @param listIdx - indicates which cache list the key belongs to
56 */
57 private _setItem;
58 /**
59 * see whether cache is full
60 *
61 * @param itemSize
62 *
63 * @return true if cache is full
64 */
65 private _isCacheFull;
66 /**
67 * check whether the cache contains the key
68 *
69 * @param key
70 */
71 private containsKey;
72 /**
73 * * Set item into cache. You can put number, string, boolean or object.
74 * The cache will first check whether has the same key.
75 * If it has, it will delete the old item and then put the new item in
76 * The cache will pop out items if it is full
77 * You can specify the cache item options. The cache will abort and output a warning:
78 * If the key is invalid
79 * If the size of the item exceeds itemMaxSize.
80 * If the value is undefined
81 * If incorrect cache item configuration
82 * If error happened with browser storage
83 *
84 * @param key - the key of the item
85 * @param value - the value of the item
86 * @param options - optional, the specified meta-data
87 *
88 * @throws if the item is too big which exceeds the limit of single item size
89 * @throws if the key is invalid
90 */
91 setItem(key: string, value: object | string | number | boolean, options?: CacheItemOptions): void;
92 /**
93 * Get item from cache. It will return null if item doesn’t exist or it has been expired.
94 * If you specified callback function in the options,
95 * then the function will be executed if no such item in the cache
96 * and finally put the return value into cache.
97 * Please make sure the callback function will return the value you want to put into the cache.
98 * The cache will abort output a warning:
99 * If the key is invalid
100 *
101 * @param key - the key of the item
102 * @param options - the options of callback function
103 */
104 getItem(key: string, options?: CacheItemOptions): any;
105 /**
106 * remove item from the cache
107 *
108 * @param key - the key of the item
109 */
110 removeItem(key: string): void;
111 /**
112 * clear the entire cache
113 */
114 clear(): void;
115 /**
116 * Return all the keys in the cache.
117 */
118 getAllKeys(): string[];
119 /**
120 * return the current size of the cache
121 *
122 * @return the current size of the cache
123 */
124 getCacheCurSize(): number;
125 /**
126 * Return a new instance of cache with customized configuration.
127 * @param config - the customized configuration
128 */
129 createInstance(config: CacheConfig): ICache;
130}
131export declare const InMemoryCache: ICache;