Skip to content

Commit

Permalink
Node: added Pttl command. (#1082)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan Wattad authored Mar 6, 2024
1 parent d179378 commit 712c4ac
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Node: Added LINDEX command ([#999](https://github.com/aws/glide-for-redis/pull/999))
* Python, Node: Added ZPOPMAX command ([#996](https://github.com/aws/glide-for-redis/pull/996), [#1009](https://github.com/aws/glide-for-redis/pull/1009))
* Python: Added ZRANGE command ([#906](https://github.com/aws/glide-for-redis/pull/906))
* Python, Node: Added PTTL command ([#1036](https://github.com/aws/glide-for-redis/pull/1036), [#1082](https://github.com/aws/glide-for-redis/pull/1082))

#### Features

Expand Down
11 changes: 11 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
createMSet,
createPExpire,
createPExpireAt,
createPttl,
createRPop,
createRPush,
createSAdd,
Expand Down Expand Up @@ -1160,6 +1161,16 @@ export class BaseClient {
return this.createWritePromise(createEcho(message));
}

/** Returns the remaining time to live of `key` that has a timeout, in milliseconds.
* See https://redis.io/commands/pttl for more details.
*
* @param key - The key to return its timeout.
* @returns TTL in milliseconds. -2 if `key` does not exist, -1 if `key` exists but has no associated expire.
*/
public pttl(key: string): Promise<number> {
return this.createWritePromise(createPttl(key));
}

private readonly MAP_READ_FROM_STRATEGY: Record<
ReadFrom,
connection_request.ReadFrom
Expand Down
7 changes: 7 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -877,3 +877,10 @@ export function createZpopmax(key: string, count?: number): redis_request.Comman
export function createEcho(message: string): redis_request.Command {
return createCommand(RequestType.Echo, [message]);
}

/**
* @internal
*/
export function createPttl(key: string): redis_request.Command {
return createCommand(RequestType.PTTL, [key]);
}
12 changes: 12 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
createPExpire,
createPExpireAt,
createPing,
createPttl,
createRPop,
createRPush,
createSAdd,
Expand Down Expand Up @@ -924,6 +925,17 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createEcho(message));
}

/** Returns the remaining time to live of `key` that has a timeout, in milliseconds.
* See https://redis.io/commands/pttl for more details.
*
* @param key - The key to return its timeout.
*
* Command Response - TTL in milliseconds. -2 if `key` does not exist, -1 if `key` exists but has no associated expire.
*/
public pttl(key: string): T {
return this.addAndReturn(createPttl(key));
}

/** Executes a single command, without checking inputs. Every part of the command, including subcommands,
* should be added as a separate value in args.
*
Expand Down
29 changes: 29 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,35 @@ export function runBaseTests<Context>(config: {
},
config.timeout
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`Pttl test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key = uuidv4();
expect(await client.pttl(key)).toEqual(-2);

expect(await client.set(key, "value")).toEqual("OK");
expect(await client.pttl(key)).toEqual(-1);

expect(await client.expire(key, 10)).toEqual(true);
let result = await client.pttl(key);
expect(result).toBeGreaterThan(0);
expect(result).toBeLessThanOrEqual(10000);

expect(await client.expireAt(key, Math.floor(Date.now() / 1000) + 20)).toEqual(true);
result = await client.pttl(key);
expect(result).toBeGreaterThan(0);
expect(result).toBeLessThanOrEqual(20000);

expect(await client.pexpireAt(key, Date.now() + 30000)).toEqual(true);
result = await client.pttl(key);
expect(result).toBeGreaterThan(0);
expect(result).toBeLessThanOrEqual(30000);
}, protocol);
},
config.timeout
);
}

export function runCommonTests<Context>(config: {
Expand Down

0 comments on commit 712c4ac

Please sign in to comment.