1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | declare class LRUCache<K, V> {
|
9 | constructor(options?: LRUCache.Options<K, V>);
|
10 | constructor(max: number);
|
11 |
|
12 | /**
|
13 | * Return total length of objects in cache taking into account `length` options function.
|
14 | */
|
15 | readonly length: number;
|
16 |
|
17 | /**
|
18 | * Return total quantity of objects currently in cache. Note,
|
19 | * that `stale` (see options) items are returned as part of this item count.
|
20 | */
|
21 | readonly itemCount: number;
|
22 |
|
23 | /**
|
24 | * Same as Options.allowStale.
|
25 | */
|
26 | allowStale: boolean;
|
27 |
|
28 | /**
|
29 | * Same as Options.length.
|
30 | */
|
31 | lengthCalculator(value: V): number;
|
32 |
|
33 | /**
|
34 | * Same as Options.max. Resizes the cache when the `max` changes.
|
35 | */
|
36 | max: number;
|
37 |
|
38 | /**
|
39 | * Same as Options.maxAge. Resizes the cache when the `maxAge` changes.
|
40 | */
|
41 | maxAge: number;
|
42 |
|
43 | /**
|
44 | * Will update the "recently used"-ness of the key. They do what you think.
|
45 | * `maxAge` is optional and overrides the cache `maxAge` option if provided.
|
46 | */
|
47 | set(key: K, value: V, maxAge?: number): boolean;
|
48 |
|
49 | /**
|
50 | * Will update the "recently used"-ness of the key. They do what you think.
|
51 | * `maxAge` is optional and overrides the cache `maxAge` option if provided.
|
52 | *
|
53 | * If the key is not found, will return `undefined`.
|
54 | */
|
55 | get(key: K): V | undefined;
|
56 |
|
57 | /**
|
58 | * Returns the key value (or `undefined` if not found) without updating
|
59 | * the "recently used"-ness of the key.
|
60 | *
|
61 | * (If you find yourself using this a lot, you might be using the wrong
|
62 | * sort of data structure, but there are some use cases where it's handy.)
|
63 | */
|
64 | peek(key: K): V | undefined;
|
65 |
|
66 | /**
|
67 | * Check if a key is in the cache, without updating the recent-ness
|
68 | * or deleting it for being stale.
|
69 | */
|
70 | has(key: K): boolean;
|
71 |
|
72 | /**
|
73 | * Deletes a key out of the cache.
|
74 | */
|
75 | del(key: K): void;
|
76 |
|
77 | /**
|
78 | * Clear the cache entirely, throwing away all values.
|
79 | */
|
80 | reset(): void;
|
81 |
|
82 | /**
|
83 | * Manually iterates over the entire cache proactively pruning old entries.
|
84 | */
|
85 | prune(): void;
|
86 |
|
87 | /**
|
88 | * Just like `Array.prototype.forEach`. Iterates over all the keys in the cache,
|
89 | * in order of recent-ness. (Ie, more recently used items are iterated over first.)
|
90 | */
|
91 | forEach<T = this>(callbackFn: (this: T, value: V, key: K, cache: this) => void, thisArg?: T): void;
|
92 |
|
93 | /**
|
94 | * The same as `cache.forEach(...)` but items are iterated over in reverse order.
|
95 | * (ie, less recently used items are iterated over first.)
|
96 | */
|
97 | rforEach<T = this>(callbackFn: (this: T, value: V, key: K, cache: this) => void, thisArg?: T): void;
|
98 |
|
99 | /**
|
100 | * Return an array of the keys in the cache.
|
101 | */
|
102 | keys(): K[];
|
103 |
|
104 | /**
|
105 | * Return an array of the values in the cache.
|
106 | */
|
107 | values(): V[];
|
108 |
|
109 | /**
|
110 | * Return an array of the cache entries ready for serialization and usage with `destinationCache.load(arr)`.
|
111 | */
|
112 | dump(): Array<LRUCache.Entry<K, V>>;
|
113 |
|
114 | /**
|
115 | * Loads another cache entries array, obtained with `sourceCache.dump()`,
|
116 | * into the cache. The destination cache is reset before loading new entries
|
117 | *
|
118 | * @param cacheEntries Obtained from `sourceCache.dump()`
|
119 | */
|
120 | load(cacheEntries: ReadonlyArray<LRUCache.Entry<K, V>>): void;
|
121 | }
|
122 |
|
123 | declare namespace LRUCache {
|
124 | interface Options<K, V> {
|
125 | |
126 |
|
127 |
|
128 |
|
129 |
|
130 | max?: number;
|
131 |
|
132 | |
133 |
|
134 |
|
135 |
|
136 |
|
137 | maxAge?: number;
|
138 |
|
139 | |
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | length?(value: V, key?: K): number;
|
148 |
|
149 | |
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | dispose?(key: K, value: V): void;
|
158 |
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | stale?: boolean;
|
168 |
|
169 | |
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | noDisposeOnSet?: boolean;
|
176 |
|
177 | |
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | updateAgeOnGet?: boolean;
|
184 | }
|
185 |
|
186 | interface Entry<K, V> {
|
187 | k: K;
|
188 | v: V;
|
189 | e: number;
|
190 | }
|
191 | }
|
192 |
|
193 | export = LRUCache;
|