UNPKG

7.61 kBTypeScriptView Raw
1// Type definitions for node-cache 5
2// Project: https://github.com/tcs-de/nodecache
3// Definitions by: Ilya Mochalov <https://github.com/chrootsu>
4// Daniel Thunell <https://github.com/dthunell>
5// Ulf Seltmann <https://github.com/useltmann>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7
8/// <reference types="node" />
9
10declare namespace NodeCache {
11 interface NodeCacheLegacyCallbacks {
12 /** container for cached data */
13 data: Data;
14
15 /** module options */
16 options: Options;
17
18 /** statistics container */
19 stats: Stats;
20
21 /**
22 * get a cached key and change the stats
23 *
24 * @param key cache key or an array of keys
25 * @param cb Callback function
26 */
27 get<T>(
28 key: Key,
29 cb?: Callback<T>
30 ): T | undefined;
31
32 /**
33 * get multiple cached keys at once and change the stats
34 *
35 * @param keys an array of keys
36 * @param cb Callback function
37 */
38 mget<T>(
39 keys: Key[],
40 cb?: Callback<{ [key: string]: T }>
41 ): { [key: string]: T };
42
43 /**
44 * set a cached key and change the stats
45 *
46 * @param key cache key
47 * @param value A element to cache. If the option `option.forceString` is `true` the module trys to translate
48 * it to a serialized JSON
49 * @param ttl The time to live in seconds.
50 * @param cb Callback function
51 */
52 set<T>(
53 key: Key,
54 value: T,
55 ttl: number | string,
56 cb?: Callback<boolean>
57 ): boolean;
58
59 set<T>(
60 key: Key,
61 value: T,
62 cb?: Callback<boolean>
63 ): boolean;
64
65 /**
66 * set multiple cached keys at once and change the stats
67 *
68 * @param keyValueSet an array of object which includes key,value and ttl
69 */
70 mset<T>(
71 keyValueSet: ValueSetItem<T>[],
72 ): boolean;
73
74 /**
75 * remove keys
76 * @param keys cache key to delete or a array of cache keys
77 * @param cb Callback function
78 * @returns Number of deleted keys
79 */
80 del(
81 keys: Key | Key[],
82 cb?: Callback<number>
83 ): number;
84
85 /**
86 * reset or redefine the ttl of a key. If `ttl` is not passed or set to 0 it's similar to `.del()`
87 */
88 ttl(
89 key: Key,
90 ttl: number,
91 cb?: Callback<boolean>
92 ): boolean;
93
94 ttl(
95 key: Key,
96 cb?: Callback<boolean>
97 ): boolean;
98
99 getTtl(
100 key: Key,
101 ): number|undefined;
102
103 getTtl(
104 key: Key,
105 cb?: Callback<boolean>
106 ): boolean;
107
108 /**
109 * list all keys within this cache
110 * @param cb Callback function
111 * @returns An array of all keys
112 */
113 keys(cb?: Callback<string[]>): string[];
114
115 /**
116 * get the stats
117 *
118 * @returns Stats data
119 */
120 getStats(): Stats;
121
122 /**
123 * flush the whole data and reset the stats
124 */
125 flushAll(): void;
126
127 /**
128 * This will clear the interval timeout which is set on checkperiod option.
129 */
130 close(): void;
131 }
132
133 /**
134 * Since 4.1.0: Key-validation: The keys can be given as either string or number,
135 * but are casted to a string internally anyway.
136 */
137 type Key = string | number;
138
139 type ValueSetItem<T = any> = {
140 key: Key;
141 val: T;
142 ttl?: number;
143 }
144
145 interface Data {
146 [key: string]: WrappedValue<any>;
147 }
148
149 interface Options {
150 /**
151 * If enabled, all values will be stringified during the set operation
152 *
153 * @type {boolean}
154 * @memberof Options
155 */
156 forceString?: boolean;
157
158 objectValueSize?: number;
159 promiseValueSize?: number;
160 arrayValueSize?: number;
161
162 /**
163 * standard time to live in seconds. 0 = infinity
164 *
165 * @type {number}
166 * @memberof Options
167 */
168 stdTTL?: number;
169
170 /**
171 * time in seconds to check all data and delete expired keys
172 *
173 * @type {number}
174 * @memberof Options
175 */
176 checkperiod?: number;
177
178 /**
179 * en/disable cloning of variables.
180 * disabling this is strongly encouraged when aiming for performance!
181 *
182 * If `true`: set operations store a clone of the value and get operations will create a fresh clone of the cached value
183 * If `false` you'll just store a reference to your value
184 *
185 * @type {boolean}
186 * @memberof Options
187 */
188 useClones?: boolean;
189
190 errorOnMissing?: boolean;
191 deleteOnExpire?: boolean;
192
193 /**
194 * enable legacy callbacks.
195 * legacy callback support will drop in v6.x!
196 *
197 * @type {boolean}
198 * @memberof Options
199 */
200 enableLegacyCallbacks?: boolean;
201
202 /**
203 * max amount of keys that are being stored.
204 * set operations will throw an error when the cache is full
205 *
206 * @type {number}
207 * @memberof Options
208 */
209 maxKeys?: number;
210 }
211
212 interface Stats {
213 hits: number;
214 misses: number;
215 keys: number;
216 ksize: number;
217 vsize: number;
218 }
219
220 interface WrappedValue<T> {
221 // ttl
222 t: number;
223 // value
224 v: T;
225 }
226
227 type Callback<T> = (err: any, data: T | undefined) => void;
228}
229
230import events = require("events");
231
232import Data = NodeCache.Data;
233import Key = NodeCache.Key;
234import Options = NodeCache.Options;
235import Stats = NodeCache.Stats;
236import Callback = NodeCache.Callback;
237import ValueSetItem = NodeCache.ValueSetItem;
238import NodeCacheLegacyCallbacks = NodeCache.NodeCacheLegacyCallbacks;
239
240declare class NodeCache extends events.EventEmitter {
241 /** container for cached data */
242 data: Data;
243
244 /** module options */
245 options: Options;
246
247 /** statistics container */
248 stats: Stats;
249
250 /** constructor */
251 constructor(options?: Options);
252
253 /**
254 * get a cached key and change the stats
255 *
256 * @param key cache key
257 * @returns The value stored in the key
258 */
259 get<T>(
260 key: Key
261 ): T | undefined;
262
263 /**
264 * get multiple cached keys at once and change the stats
265 *
266 * @param keys an array of keys
267 * @returns an object containing the values stored in the matching keys
268 */
269 mget<T>(
270 keys: Key[]
271 ): { [key: string]: T };
272
273 /**
274 * set a cached key and change the stats
275 *
276 * @param key cache key
277 * @param value A element to cache. If the option `option.forceString` is `true` the module trys to translate
278 * it to a serialized JSON
279 * @param ttl The time to live in seconds.
280 */
281 set<T>(
282 key: Key,
283 value: T,
284 ttl: number | string
285 ): boolean;
286
287 set<T>(
288 key: Key,
289 value: T
290 ): boolean;
291
292 /**
293 * set multiple cached keys at once and change the stats
294 *
295 * @param keyValueSet an array of object which includes key,value and ttl
296 */
297 mset<T>(
298 keyValueSet: ValueSetItem<T>[]
299 ): boolean;
300
301 /**
302 * remove keys
303 * @param keys cache key to delete or a array of cache keys
304 * @param cb Callback function
305 * @returns Number of deleted keys
306 */
307 del(
308 keys: Key | Key[]
309 ): number;
310
311 /**
312 * get a cached key and remove it from the cache.
313 * Equivalent to calling `get(key)` + `del(key)`.
314 * Useful for implementing `single use` mechanism such as OTP, where once a value is read it will become obsolete.
315 *
316 * @param key cache key
317 * @returns The value stored in the key
318 */
319 take<T>(
320 key: Key
321 ): T | undefined;
322
323 /**
324 * reset or redefine the ttl of a key. If `ttl` is not passed or set to 0 it's similar to `.del()`
325 */
326 ttl(
327 key: Key,
328 ttl: number
329 ): boolean;
330
331 ttl(
332 key: Key
333 ): boolean;
334
335 getTtl(
336 key: Key,
337 ): number|undefined;
338
339 getTtl(
340 key: Key
341 ): boolean;
342
343 /**
344 * list all keys within this cache
345 * @returns An array of all keys
346 */
347 keys(): string[];
348
349 /**
350 * get the stats
351 *
352 * @returns Stats data
353 */
354 getStats(): Stats;
355
356 /**
357 * Check if a key is cached
358 * @param key cache key to check
359 * @returns Boolean indicating if the key is cached or not
360 */
361 has(key: Key): boolean;
362
363 /**
364 * flush the whole data and reset the stats
365 */
366 flushAll(): void;
367
368 /**
369 * This will clear the interval timeout which is set on checkperiod option.
370 */
371 close(): void;
372
373 /**
374 * flush the stats and reset all counters to 0
375 */
376 flushStats(): void;
377}
378
379
380export = NodeCache;