Skip to content

Commit fc045b4

Browse files
committed
feat(command): added command GETEX supports
1 parent 7819a4e commit fc045b4

File tree

4 files changed

+259
-28
lines changed

4 files changed

+259
-28
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- feat(command): added command `HRANDFILED` supports.
66
- feat(command): added `count` param of command `R/LPOP`.
7+
- feat(command): added command `GETEX` supports.
78

89
## v1.2.3
910

src/examples/debug.ts

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ function sleep(ms: number): Promise<void> {
4141
// await cli.auth("hello");
4242
await cli.flushAll();
4343

44+
await cli.set('getex-test', 'hello');
45+
46+
console.log(await cli.getEx('getex-test', 1000));
47+
console.log(await cli.ttl('getex-test'));
48+
49+
await cli.getPEx('getex-test', 1000);
50+
console.log(await cli.pTTL('getex-test'));
51+
52+
await cli.getExAt('getex-test', Math.floor(Date.now() / 1000) + 30);
53+
console.log(await cli.ttl('getex-test'));
54+
55+
await cli.getPExAt('getex-test', Date.now() + 1000);
56+
console.log(await cli.pTTL('getex-test'));
57+
58+
await cli.getAndPersist('getex-test');
59+
console.log(await cli.ttl('getex-test'));
60+
4461
console.log(await cli.hRandField('aaaa', 1));
4562

4663
console.log(await cli.hRandField('aaaa', 2));
@@ -78,16 +95,17 @@ function sleep(ms: number): Promise<void> {
7895

7996
await sleep(2000);
8097

81-
let x = cli.set('a', '333');
98+
const x = cli.set('a', '333');
8299

83100
console.log('SET ->', await x);
84101
console.log('LPUSH ->', await cli.lPush('lll', ['a', 'b']));
85102
console.log('LINDEX ->', await cli.lIndex('lll', 1));
86103
console.log('LRANGE ->', await cli.lRange('lll', 0, -1));
87-
console.log('HMSET->', await cli.hMSet('ggg', {
104+
console.log('HMSET->');
105+
await cli.hMSet('ggg', {
88106
'a': '3333',
89107
'bb': '1231232131'
90-
}));
108+
});
91109
console.log('HMGET->', await cli.hMGet('ggg', ['bb', 'a']));
92110
console.log('INCR->', await cli.incr('a'));
93111
console.log('PUBSUBCHANNELS->', await cli.pubSubChannels());
@@ -99,52 +117,57 @@ function sleep(ms: number): Promise<void> {
99117

100118
await sleep(2000);
101119

102-
const pipeline = await cli.multi();
120+
const multiTrx = await cli.multi();
103121

104122
// Multi Mode
105-
await pipeline.multi();
106-
await pipeline.get('a');
123+
await multiTrx.multi();
124+
await multiTrx.get('a');
107125

108-
await pipeline.set('ccc', 'g');
126+
await multiTrx.set('ccc', 'g');
109127

110-
await pipeline.mGet(['a', 'ccc']);
128+
await multiTrx.mGet(['a', 'ccc']);
111129

112-
await pipeline.hSet('h', 'name', 'Mick');
113-
await pipeline.hMSet('h', {
130+
await multiTrx.hSet('h', 'name', 'Mick');
131+
await multiTrx.hMSet('h', {
114132
'age': 123,
115133
'title': 'Mr.'
116134
});
117135

118-
await pipeline.hMGet('h', ['age', 'title']);
119-
await pipeline.hGetAll('h');
120-
console.log(JSON.stringify(await pipeline.scan(0), null, 2));
136+
await multiTrx.hMGet('h', ['age', 'title']);
137+
await multiTrx.hGetAll('h');
138+
await multiTrx.scan(0);
139+
// console.log(JSON.stringify(await multiTrx.scan(0), null, 2));
121140

122-
await pipeline.incr('a', 123);
141+
await multiTrx.incr('a', 123);
123142

124-
await pipeline.command('HGETALL', ['h']);
143+
await multiTrx.command('HGETALL', ['h']);
125144

126-
console.log(JSON.stringify(await pipeline.exec(), null, 2));
145+
console.log(JSON.stringify(await multiTrx.exec(), null, 2));
146+
147+
await multiTrx.close();
148+
149+
const pipeline = await cli.pipeline();
127150

128151
// Pipeline Mode
129-
await pipeline.get('a');
152+
pipeline.get('a');
130153

131-
await pipeline.set('ccc', 'g');
154+
pipeline.set('ccc', 'g');
132155

133-
await pipeline.mGet(['a', 'ccc']);
156+
pipeline.mGet(['a', 'ccc']);
134157

135-
await pipeline.hSet('h', 'name', 'Mick');
136-
await pipeline.hMSet('h', {
158+
pipeline.hSet('h', 'name', 'Mick');
159+
pipeline.hMSet('h', {
137160
'age': 123,
138161
'title': 'Mr.'
139162
});
140163

141-
await pipeline.hMGet('h', ['age', 'title']);
142-
await pipeline.hGetAll('h');
143-
console.log(JSON.stringify(await pipeline.scan(0), null, 2));
164+
pipeline.hMGet('h', ['age', 'title']);
165+
pipeline.hGetAll('h');
166+
pipeline.scan(0);
144167

145-
await pipeline.incr('a', 123);
168+
pipeline.incr('a', 123);
146169

147-
await pipeline.command('HGETALL', ['h']);
170+
// pipeline.command('HGETALL', ['h']);
148171

149172
console.log(JSON.stringify(await pipeline.exec(), null, 2));
150173

@@ -153,4 +176,4 @@ function sleep(ms: number): Promise<void> {
153176
await cli.close();
154177
await sub.close();
155178

156-
})().catch((e) => console.error(e));
179+
})().catch(console.error);

src/lib/Commands.ts

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function isIntegerOne(data: unknown): data is number {
4646

4747
function createDefaultPreparer(cmd: string): Required<ICommand>['prepare'] {
4848

49-
return (...args: Array<string | Buffer>) => ({args, cmd});
49+
return (...args: Array<string | Buffer>) => ({ args, cmd });
5050
}
5151

5252
export const COMMANDS: Record<keyof C.ICommandAPIs, ICommand> = {
@@ -274,6 +274,141 @@ export const COMMANDS: Record<keyof C.ICommandAPIs, ICommand> = {
274274
prepare: createDefaultPreparer('GET'),
275275
},
276276

277+
/**
278+
* Command: getex
279+
* @see https://redis.io/commands/getex
280+
*/
281+
'getEx': {
282+
prepare(key: string, seconds: number): IPrepareResult {
283+
return {
284+
'args': [key, 'EX', seconds],
285+
'cmd': 'GETEX',
286+
};
287+
},
288+
process: U.nullableBuffer2String
289+
},
290+
291+
/**
292+
* Command: getex
293+
* @see https://redis.io/commands/getex
294+
*/
295+
'getEx$': {
296+
prepare(key: string, seconds: number): IPrepareResult {
297+
return {
298+
'args': [key, 'EX', seconds],
299+
'cmd': 'GETEX'
300+
};
301+
},
302+
},
303+
304+
/**
305+
* Command: getex
306+
* @see https://redis.io/commands/getex
307+
*/
308+
'getExAt': {
309+
prepare(key: string, tsInSec: number): IPrepareResult {
310+
return {
311+
'args': [key, 'EXAT', tsInSec],
312+
'cmd': 'GETEX',
313+
};
314+
},
315+
process: U.nullableBuffer2String
316+
},
317+
318+
/**
319+
* Command: getex
320+
* @see https://redis.io/commands/getex
321+
*/
322+
'getExAt$': {
323+
prepare(key: string, seconds: number): IPrepareResult {
324+
return {
325+
'args': [key, 'EXAT', seconds],
326+
'cmd': 'GETEX'
327+
};
328+
},
329+
},
330+
331+
/**
332+
* Command: getex
333+
* @see https://redis.io/commands/getex
334+
*/
335+
'getPEx': {
336+
prepare(key: string, seconds: number): IPrepareResult {
337+
return {
338+
'args': [key, 'PX', seconds],
339+
'cmd': 'GETEX',
340+
};
341+
},
342+
process: U.nullableBuffer2String
343+
},
344+
345+
/**
346+
* Command: getex
347+
* @see https://redis.io/commands/getex
348+
*/
349+
'getPEx$': {
350+
prepare(key: string, seconds: number): IPrepareResult {
351+
return {
352+
'args': [key, 'PX', seconds],
353+
'cmd': 'GETEX'
354+
};
355+
},
356+
},
357+
358+
/**
359+
* Command: getex
360+
* @see https://redis.io/commands/getex
361+
*/
362+
'getPExAt': {
363+
prepare(key: string, tsInSec: number): IPrepareResult {
364+
return {
365+
'args': [key, 'PXAT', tsInSec],
366+
'cmd': 'GETEX',
367+
};
368+
},
369+
process: U.nullableBuffer2String
370+
},
371+
372+
/**
373+
* Command: getex
374+
* @see https://redis.io/commands/getex
375+
*/
376+
'getPExAt$': {
377+
prepare(key: string, seconds: number): IPrepareResult {
378+
return {
379+
'args': [key, 'PXAT', seconds],
380+
'cmd': 'GETEX'
381+
};
382+
},
383+
},
384+
385+
/**
386+
* Command: getex
387+
* @see https://redis.io/commands/getex
388+
*/
389+
'getAndPersist': {
390+
prepare(key: string): IPrepareResult {
391+
return {
392+
'args': [key, 'PERSIST'],
393+
'cmd': 'GETEX',
394+
};
395+
},
396+
process: U.nullableBuffer2String
397+
},
398+
399+
/**
400+
* Command: getex
401+
* @see https://redis.io/commands/getex
402+
*/
403+
'getAndPersist$': {
404+
prepare(key: string): IPrepareResult {
405+
return {
406+
'args': [key, 'PERSIST'],
407+
'cmd': 'GETEX'
408+
};
409+
},
410+
},
411+
277412
/**
278413
* Command: getSet
279414
* @see https://redis.io/commands/getSet

src/lib/Common.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,87 @@ export interface ICommandAPIs {
260260
*/
261261
get$(key: string): Promise<Buffer | null>;
262262

263+
/**
264+
* Command: getex
265+
* @see https://redis.io/commands/getex
266+
* @since v6.2.0
267+
*/
268+
getAndPersist(key: string): Promise<string | null>;
269+
270+
/**
271+
* Command: getex
272+
* @see https://redis.io/commands/getex
273+
* @since v6.2.0
274+
*/
275+
getAndPersist$(key: string): Promise<Buffer | null>;
276+
277+
/**
278+
* Command: getex
279+
* @see https://redis.io/commands/getex
280+
* @since v6.2.0
281+
*/
282+
getEx(key: string, ttl: number): Promise<string | null>;
283+
284+
/**
285+
* Command: getex
286+
* @see https://redis.io/commands/getex
287+
* @since v6.2.0
288+
*/
289+
getEx$(key: string, ttl: number): Promise<Buffer | null>;
290+
291+
/**
292+
* Command: getex
293+
* @see https://redis.io/commands/getex
294+
* @since v6.2.0
295+
*/
296+
getExAt(key: string, at: number): Promise<string | null>;
297+
298+
/**
299+
* Command: getex
300+
* @see https://redis.io/commands/getex
301+
* @since v6.2.0
302+
*/
303+
getExAt$(key: string, at: number): Promise<Buffer | null>;
304+
305+
/**
306+
* Command: getex
307+
* @see https://redis.io/commands/getex
308+
* @since v6.2.0
309+
*/
310+
getPEx(key: string, ttl: number): Promise<string | null>;
311+
312+
/**
313+
* Command: getex
314+
* @see https://redis.io/commands/getex
315+
* @since v6.2.0
316+
*/
317+
getPEx$(key: string, ttl: number): Promise<Buffer | null>;
318+
319+
/**
320+
* Command: getex
321+
* @see https://redis.io/commands/getex
322+
* @since v6.2.0
323+
*/
324+
getPExAt(key: string, at: number): Promise<string | null>;
325+
326+
/**
327+
* Command: getex
328+
* @see https://redis.io/commands/getex
329+
* @since v6.2.0
330+
*/
331+
getPExAt$(key: string, at: number): Promise<Buffer | null>;
332+
263333
/**
264334
* Command: getSet
265335
* @see https://redis.io/commands/getSet
336+
* @deprecated since v6.2.0
266337
*/
267338
getSet(key: string, value: string): Promise<string | null>;
268339

269340
/**
270341
* Command: getSet
271342
* @see https://redis.io/commands/getSet
343+
* @deprecated since v6.2.0
272344
*/
273345
getSet$(key: string, value: Buffer): Promise<Buffer | null>;
274346

0 commit comments

Comments
 (0)