Skip to content

Commit 24cd9ba

Browse files
Avital-Fineleibale
andauthored
Support new expire features (#2036)
* Support new expire features * Update PEXPIRETIME.ts * Update EXPIRETIME.ts * fix version skip * clean code Co-authored-by: leibale <[email protected]>
1 parent c5c2bf9 commit 24cd9ba

File tree

13 files changed

+144
-18
lines changed

13 files changed

+144
-18
lines changed

packages/client/lib/cluster/commands.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as EVALSHA from '../commands/EVALSHA';
2121
import * as EXISTS from '../commands/EXISTS';
2222
import * as EXPIRE from '../commands/EXPIRE';
2323
import * as EXPIREAT from '../commands/EXPIREAT';
24+
import * as EXPIRETIME from '../commands/EXPIRETIME';
2425
import * as GEOADD from '../commands/GEOADD';
2526
import * as GEODIST from '../commands/GEODIST';
2627
import * as GEOHASH from '../commands/GEOHASH';
@@ -75,6 +76,7 @@ import * as MSETNX from '../commands/MSETNX';
7576
import * as PERSIST from '../commands/PERSIST';
7677
import * as PEXPIRE from '../commands/PEXPIRE';
7778
import * as PEXPIREAT from '../commands/PEXPIREAT';
79+
import * as PEXPIRETIME from '../commands/PEXPIRETIME';
7880
import * as PFADD from '../commands/PFADD';
7981
import * as PFCOUNT from '../commands/PFCOUNT';
8082
import * as PFMERGE from '../commands/PFMERGE';
@@ -224,6 +226,8 @@ export default {
224226
expire: EXPIRE,
225227
EXPIREAT,
226228
expireAt: EXPIREAT,
229+
EXPIRETIME,
230+
expireTime: EXPIRETIME,
227231
GEOADD,
228232
geoAdd: GEOADD,
229233
GEODIST,
@@ -332,6 +336,8 @@ export default {
332336
pExpire: PEXPIRE,
333337
PEXPIREAT,
334338
pExpireAt: PEXPIREAT,
339+
PEXPIRETIME,
340+
pExpireTime: PEXPIRETIME,
335341
PFADD,
336342
pfAdd: PFADD,
337343
PFCOUNT,

packages/client/lib/commands/EXPIRE.spec.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@ import testUtils, { GLOBAL } from '../test-utils';
33
import { transformArguments } from './EXPIRE';
44

55
describe('EXPIRE', () => {
6-
it('transformArguments', () => {
7-
assert.deepEqual(
8-
transformArguments('key', 1),
9-
['EXPIRE', 'key', '1']
10-
);
6+
describe('transformArguments', () => {
7+
it('simple', () => {
8+
assert.deepEqual(
9+
transformArguments('key', 1),
10+
['EXPIRE', 'key', '1']
11+
);
12+
});
13+
14+
it('with set option', () => {
15+
assert.deepEqual(
16+
transformArguments('key', 1, 'NX'),
17+
['EXPIRE', 'key', '1', 'NX']
18+
);
19+
});
1120
});
1221

1322
testUtils.testWithClient('client.expire', async client => {

packages/client/lib/commands/EXPIRE.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ export const FIRST_KEY_INDEX = 1;
44

55
export function transformArguments(
66
key: RedisCommandArgument,
7-
seconds: number
7+
seconds: number,
8+
mode?: 'NX' | 'XX' | 'GT' | 'LT'
89
): RedisCommandArguments {
9-
return ['EXPIRE', key, seconds.toString()];
10+
const args = ['EXPIRE', key, seconds.toString()];
11+
12+
if (mode) {
13+
args.push(mode);
14+
}
15+
16+
return args;
1017
}
1118

1219
export { transformBooleanReply as transformReply } from './generic-transformers';

packages/client/lib/commands/EXPIREAT.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ describe('EXPIREAT', () => {
1818
['EXPIREAT', 'key', Math.floor(d.getTime() / 1000).toString()]
1919
);
2020
});
21+
22+
it('with set option', () => {
23+
assert.deepEqual(
24+
transformArguments('key', 1, 'GT'),
25+
['EXPIREAT', 'key', '1', 'GT']
26+
);
27+
});
2128
});
2229

2330
testUtils.testWithClient('client.expireAt', async client => {

packages/client/lib/commands/EXPIREAT.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ export const FIRST_KEY_INDEX = 1;
55

66
export function transformArguments(
77
key: RedisCommandArgument,
8-
timestamp: number | Date
8+
timestamp: number | Date,
9+
mode?: 'NX' | 'XX' | 'GT' | 'LT'
910
): RedisCommandArguments {
10-
return [
11+
const args = [
1112
'EXPIREAT',
1213
key,
1314
transformEXAT(timestamp)
1415
];
16+
17+
if (mode) {
18+
args.push(mode);
19+
}
20+
21+
return args;
1522
}
1623

1724
export { transformBooleanReply as transformReply } from './generic-transformers';
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { strict as assert } from 'assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { transformArguments } from './EXPIRETIME';
4+
5+
describe('EXPIRETIME', () => {
6+
testUtils.isVersionGreaterThanHook([7, 0]);
7+
8+
it('transformArguments', () => {
9+
assert.deepEqual(
10+
transformArguments('key'),
11+
['EXPIRETIME', 'key']
12+
);
13+
});
14+
15+
testUtils.testWithClient('client.expireTime', async client => {
16+
assert.equal(
17+
await client.expireTime('key'),
18+
-2
19+
);
20+
}, GLOBAL.SERVERS.OPEN);
21+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { RedisCommandArgument, RedisCommandArguments } from '.';
2+
3+
export const FIRST_KEY_INDEX = 1;
4+
5+
export function transformArguments(key: RedisCommandArgument): RedisCommandArguments {
6+
return ['EXPIRETIME', key];
7+
}
8+
9+
export declare function transformReply(): number;

packages/client/lib/commands/PEXPIRE.spec.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@ import testUtils, { GLOBAL } from '../test-utils';
33
import { transformArguments } from './PEXPIRE';
44

55
describe('PEXPIRE', () => {
6-
it('transformArguments', () => {
7-
assert.deepEqual(
8-
transformArguments('key', 1),
9-
['PEXPIRE', 'key', '1']
10-
);
6+
describe('transformArguments', () => {
7+
it('simple', () => {
8+
assert.deepEqual(
9+
transformArguments('key', 1),
10+
['PEXPIRE', 'key', '1']
11+
);
12+
});
13+
14+
it('with set option', () => {
15+
assert.deepEqual(
16+
transformArguments('key', 1, 'GT'),
17+
['PEXPIRE', 'key', '1', 'GT']
18+
);
19+
});
1120
});
1221

1322
testUtils.testWithClient('client.pExpire', async client => {

packages/client/lib/commands/PEXPIRE.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ export const FIRST_KEY_INDEX = 1;
44

55
export function transformArguments(
66
key: RedisCommandArgument,
7-
milliseconds: number
7+
milliseconds: number,
8+
mode?: 'NX' | 'XX' | 'GT' | 'LT'
89
): RedisCommandArguments {
9-
return ['PEXPIRE', key, milliseconds.toString()];
10+
const args = ['PEXPIRE', key, milliseconds.toString()];
11+
12+
if (mode) {
13+
args.push(mode);
14+
}
15+
16+
return args;
1017
}
1118

1219
export { transformBooleanReply as transformReply } from './generic-transformers';

packages/client/lib/commands/PEXPIREAT.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ describe('PEXPIREAT', () => {
1818
['PEXPIREAT', 'key', d.getTime().toString()]
1919
);
2020
});
21+
22+
it('with set option', () => {
23+
assert.deepEqual(
24+
transformArguments('key', 1, 'XX'),
25+
['PEXPIREAT', 'key', '1', 'XX']
26+
);
27+
});
2128
});
2229

2330
testUtils.testWithClient('client.pExpireAt', async client => {

0 commit comments

Comments
 (0)