Skip to content

Commit 755307e

Browse files
committed
Merge pull request DefinitelyTyped#5913 from chrootsu/node-cache
node-cache: added definitions and tests
2 parents 81af1f4 + 0f5d7e0 commit 755307e

File tree

2 files changed

+361
-0
lines changed

2 files changed

+361
-0
lines changed

node-cache/node-cache-tests.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/// <reference path="./node-cache.d.ts" />
2+
3+
import NodeCache = require('node-cache');
4+
5+
import Options = NodeCacheTypes.Options;
6+
import Stats = NodeCacheTypes.Stats;
7+
import Callback = NodeCacheTypes.Callback;
8+
9+
interface TypeSample {
10+
a: number;
11+
b: string;
12+
c: boolean;
13+
}
14+
15+
{
16+
let options: Options;
17+
let cache: NodeCacheTypes.NodeCache;
18+
cache = new NodeCache();
19+
cache = new NodeCache(options);
20+
}
21+
22+
{
23+
let cache: NodeCache;
24+
let key: string;
25+
let cb: Callback<TypeSample>;
26+
let result: TypeSample;
27+
result = cache.get<TypeSample>(key);
28+
result = cache.get<TypeSample>(key, cb);
29+
}
30+
31+
{
32+
let cache: NodeCache;
33+
let keys: string[];
34+
let cb: Callback<{[key: string]: TypeSample}>;
35+
let result: {[key: string]: TypeSample};
36+
result = cache.mget<TypeSample>(keys);
37+
result = cache.mget<TypeSample>(keys, cb);
38+
}
39+
40+
{
41+
let cache: NodeCache;
42+
let key: string;
43+
let value: TypeSample;
44+
let ttl: number|string;
45+
let cb: Callback<boolean>;
46+
let result: boolean;
47+
result = cache.set<TypeSample>(key, value);
48+
result = cache.set<TypeSample>(key, value, ttl);
49+
result = cache.set<TypeSample>(key, value, ttl, cb);
50+
result = cache.set<TypeSample>(key, value, cb);
51+
}
52+
53+
{
54+
let cache: NodeCache;
55+
let keys: string|string[];
56+
let cb: Callback<number>;
57+
let result: number;
58+
result = cache.del(keys);
59+
result = cache.del(keys, cb);
60+
}
61+
62+
{
63+
let cache: NodeCache;
64+
let key: string;
65+
let ttl: number;
66+
let cb: Callback<boolean>;
67+
let result: boolean;
68+
result = cache.ttl(key);
69+
result = cache.ttl(key, ttl);
70+
result = cache.ttl(key, ttl, cb);
71+
result = cache.ttl(key, cb);
72+
}
73+
74+
{
75+
let cache: NodeCache;
76+
let cb: Callback<string[]>;
77+
let result: string[];
78+
result = cache.keys();
79+
result = cache.keys(cb);
80+
}
81+
82+
{
83+
let cache: NodeCache;
84+
let result: Stats;
85+
result = cache.getStats();
86+
}
87+
88+
{
89+
let cache: NodeCache;
90+
let result: void;
91+
result = cache.flushAll();
92+
}
93+
94+
{
95+
let cache: NodeCache;
96+
let result: void;
97+
result = cache.close();
98+
}

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)