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 |
|
10 | declare 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 |
|
230 | import events = require("events");
|
231 |
|
232 | import Data = NodeCache.Data;
|
233 | import Key = NodeCache.Key;
|
234 | import Options = NodeCache.Options;
|
235 | import Stats = NodeCache.Stats;
|
236 | import Callback = NodeCache.Callback;
|
237 | import ValueSetItem = NodeCache.ValueSetItem;
|
238 | import NodeCacheLegacyCallbacks = NodeCache.NodeCacheLegacyCallbacks;
|
239 |
|
240 | declare 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 or an array of keys
|
257 | * @param cb Callback function
|
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 | * @param cb Callback function
|
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 | * @param cb Callback function
|
281 | */
|
282 | set<T>(
|
283 | key: Key,
|
284 | value: T,
|
285 | ttl: number | string
|
286 | ): boolean;
|
287 |
|
288 | set<T>(
|
289 | key: Key,
|
290 | value: T
|
291 | ): boolean;
|
292 |
|
293 | /**
|
294 | * set multiple cached keys at once and change the stats
|
295 | *
|
296 | * @param keyValueSet an array of object which includes key,value and ttl
|
297 | */
|
298 | mset<T>(
|
299 | keyValueSet: ValueSetItem<T>[]
|
300 | ): boolean;
|
301 |
|
302 | /**
|
303 | * remove keys
|
304 | * @param keys cache key to delete or a array of cache keys
|
305 | * @param cb Callback function
|
306 | * @returns Number of deleted keys
|
307 | */
|
308 | del(
|
309 | keys: Key | Key[]
|
310 | ): number;
|
311 |
|
312 | /**
|
313 | * reset or redefine the ttl of a key. If `ttl` is not passed or set to 0 it's similar to `.del()`
|
314 | */
|
315 | ttl(
|
316 | key: Key,
|
317 | ttl: number
|
318 | ): boolean;
|
319 |
|
320 | ttl(
|
321 | key: Key
|
322 | ): boolean;
|
323 |
|
324 | getTtl(
|
325 | key: Key,
|
326 | ): number|undefined;
|
327 |
|
328 | getTtl(
|
329 | key: Key
|
330 | ): boolean;
|
331 |
|
332 | /**
|
333 | * list all keys within this cache
|
334 | * @returns An array of all keys
|
335 | */
|
336 | keys(): string[];
|
337 |
|
338 | /**
|
339 | * get the stats
|
340 | *
|
341 | * @returns Stats data
|
342 | */
|
343 | getStats(): Stats;
|
344 |
|
345 | /**
|
346 | * Check if a key is cached
|
347 | * @param key cache key to check
|
348 | * @returns Boolean indicating if the key is cached or not
|
349 | */
|
350 | has(key: Key): boolean;
|
351 |
|
352 | /**
|
353 | * flush the whole data and reset the stats
|
354 | */
|
355 | flushAll(): void;
|
356 |
|
357 | /**
|
358 | * This will clear the interval timeout which is set on checkperiod option.
|
359 | */
|
360 | close(): void;
|
361 | }
|
362 |
|
363 |
|
364 | export = NodeCache;
|