UNPKG

5.55 kBTypeScriptView Raw
1// Type definitions for node-cache 4.1
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/**
11 * Since 4.1.0: Key-validation: The keys can be given as either string or number,
12 * but are casted to a string internally anyway.
13 */
14type Key = string | number;
15
16declare namespace NodeCache {
17 interface NodeCache {
18 /** container for cached data */
19 data: Data;
20
21 /** module options */
22 options: Options;
23
24 /** statistics container */
25 stats: Stats;
26
27 /**
28 * get a cached key and change the stats
29 *
30 * @param key cache key or an array of keys
31 * @param cb Callback function
32 */
33 get<T>(
34 key: Key,
35 cb?: Callback<T>
36 ): T | undefined;
37
38 /**
39 * get multiple cached keys at once and change the stats
40 *
41 * @param keys an array of keys
42 * @param cb Callback function
43 */
44 mget<T>(
45 keys: Key[],
46 cb?: Callback<{ [key: string]: T }>
47 ): { [key: string]: T };
48
49 /**
50 * set a cached key and change the stats
51 *
52 * @param key cache key
53 * @param value A element to cache. If the option `option.forceString` is `true` the module trys to translate
54 * it to a serialized JSON
55 * @param ttl The time to live in seconds.
56 * @param cb Callback function
57 */
58 set<T>(
59 key: Key,
60 value: T,
61 ttl: number | string,
62 cb?: Callback<boolean>
63 ): boolean;
64
65 set<T>(
66 key: Key,
67 value: T,
68 cb?: Callback<boolean>
69 ): boolean;
70
71 /**
72 * remove keys
73 * @param keys cache key to delete or a array of cache keys
74 * @param cb Callback function
75 * @returns Number of deleted keys
76 */
77 del(
78 keys: Key | Key[],
79 cb?: Callback<number>
80 ): number;
81
82 /**
83 * reset or redefine the ttl of a key. If `ttl` is not passed or set to 0 it's similar to `.del()`
84 */
85 ttl(
86 key: Key,
87 ttl: number,
88 cb?: Callback<boolean>
89 ): boolean;
90
91 ttl(
92 key: Key,
93 cb?: Callback<boolean>
94 ): boolean;
95
96 getTtl(
97 key: Key,
98 ): number|undefined;
99
100 getTtl(
101 key: Key,
102 cb?: Callback<boolean>
103 ): boolean;
104
105 /**
106 * list all keys within this cache
107 * @param cb Callback function
108 * @returns An array of all keys
109 */
110 keys(cb?: Callback<string[]>): string[];
111
112 /**
113 * get the stats
114 *
115 * @returns Stats data
116 */
117 getStats(): Stats;
118
119 /**
120 * flush the hole data and reset the stats
121 */
122 flushAll(): void;
123
124 /**
125 * This will clear the interval timeout which is set on checkperiod option.
126 */
127 close(): void;
128 }
129
130 interface Data {
131 [key: string]: WrappedValue<any>;
132 }
133
134 interface Options {
135 forceString?: boolean;
136 objectValueSize?: number;
137 arrayValueSize?: number;
138 stdTTL?: number;
139 checkperiod?: number;
140 useClones?: boolean;
141 errorOnMissing?: boolean;
142 deleteOnExpire?: boolean;
143 }
144
145 interface Stats {
146 hits: number;
147 misses: number;
148 keys: number;
149 ksize: number;
150 vsize: number;
151 }
152
153 interface WrappedValue<T> {
154 // ttl
155 t: number;
156 // value
157 v: T;
158 }
159
160 type Callback<T> = (err: any, data: T | undefined) => void;
161}
162
163import events = require("events");
164
165import Data = NodeCache.Data;
166import Options = NodeCache.Options;
167import Stats = NodeCache.Stats;
168import Callback = NodeCache.Callback;
169
170declare class NodeCache extends events.EventEmitter implements NodeCache.NodeCache {
171 /** container for cached data */
172 data: Data;
173
174 /** module options */
175 options: Options;
176
177 /** statistics container */
178 stats: Stats;
179
180 constructor(options?: Options);
181
182 /**
183 * get a cached key and change the stats
184 *
185 * @param key cache key or an array of keys
186 * @param cb Callback function
187 */
188 get<T>(
189 key: Key,
190 cb?: Callback<T>
191 ): T | undefined;
192
193 /**
194 * get multiple cached keys at once and change the stats
195 *
196 * @param keys an array of keys
197 * @param cb Callback function
198 */
199 mget<T>(
200 keys: Key[],
201 cb?: Callback<{ [key: string]: T }>
202 ): { [key: string]: T };
203
204 /**
205 * set a cached key and change the stats
206 *
207 * @param key cache key
208 * @param value A element to cache. If the option `option.forceString` is `true` the module trys to translate
209 * it to a serialized JSON
210 * @param ttl The time to live in seconds.
211 * @param cb Callback function
212 */
213 set<T>(
214 key: Key,
215 value: T,
216 ttl: number | string,
217 cb?: Callback<boolean>
218 ): boolean;
219
220 set<T>(
221 key: Key,
222 value: T,
223 cb?: Callback<boolean>
224 ): boolean;
225
226 /**
227 * remove keys
228 * @param keys cache key to delete or a array of cache keys
229 * @param cb Callback function
230 * @returns Number of deleted keys
231 */
232 del(
233 keys: Key | Key[],
234 cb?: Callback<number>
235 ): number;
236
237 /**
238 * reset or redefine the ttl of a key. If `ttl` is not passed or set to 0 `stdTtl` is used. if set lt 0 it's similar to `.del()`
239 */
240 ttl(
241 key: Key,
242 ttl: number,
243 cb?: Callback<boolean>
244 ): boolean;
245
246 ttl(
247 key: Key,
248 cb?: Callback<boolean>
249 ): boolean;
250
251 getTtl(
252 key: Key
253 ): number|undefined;
254
255 getTtl(
256 key: Key,
257 cb?: Callback<boolean>,
258 ): boolean;
259
260
261 /**
262 * list all keys within this cache
263 * @param cb Callback function
264 * @returns An array of all keys
265 */
266 keys(cb?: Callback<string[]>): string[];
267
268 /**
269 * get the stats
270 *
271 * @returns Stats data
272 */
273 getStats(): Stats;
274
275 /**
276 * flush the hole data and reset the stats
277 */
278 flushAll(): void;
279
280 /**
281 * This will clear the interval timeout which is set on checkperiod option.
282 */
283 close(): void;
284}
285
286export = NodeCache;