Skip to content

Commit 0123e36

Browse files
committed
node-cache: added definitions
1 parent 6f6e5c7 commit 0123e36

File tree

1 file changed

+263
-0
lines changed

1 file changed

+263
-0
lines changed

node-cache/node-cache.d.ts

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

0 commit comments

Comments
 (0)