From 712c4ac244ce1795ac46429ce8a948be58786bd2 Mon Sep 17 00:00:00 2001 From: Adan Wattad Date: Wed, 6 Mar 2024 15:59:52 +0200 Subject: [PATCH] Node: added Pttl command. (#1082) --- CHANGELOG.md | 1 + node/src/BaseClient.ts | 11 +++++++++++ node/src/Commands.ts | 7 +++++++ node/src/Transaction.ts | 12 ++++++++++++ node/tests/SharedTests.ts | 29 +++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd21b1c526..da57d3e91b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index b804501790..61454210d0 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -47,6 +47,7 @@ import { createMSet, createPExpire, createPExpireAt, + createPttl, createRPop, createRPush, createSAdd, @@ -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 { + return this.createWritePromise(createPttl(key)); + } + private readonly MAP_READ_FROM_STRATEGY: Record< ReadFrom, connection_request.ReadFrom diff --git a/node/src/Commands.ts b/node/src/Commands.ts index a7428736c0..b70685b001 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -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]); +} diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index 6a36f74c91..de8a087703 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -49,6 +49,7 @@ import { createPExpire, createPExpireAt, createPing, + createPttl, createRPop, createRPush, createSAdd, @@ -924,6 +925,17 @@ export class BaseTransaction> { 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. * diff --git a/node/tests/SharedTests.ts b/node/tests/SharedTests.ts index e60b6b5bee..3920334eb4 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -1651,6 +1651,35 @@ export function runBaseTests(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(config: {