Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 BITCOUNT command ([#1982](https://github.com/valkey-io/valkey-glide/pull/1982))
* Node: Added BITPOS command ([#1998](https://github.com/valkey-io/valkey-glide/pull/1998))
* Node: Added BITFIELD and BITFIELD_RO commands ([#2026](https://github.com/valkey-io/valkey-glide/pull/2026))
* Node: Added TOUCH command ([#2055](https://github.com/valkey-io/valkey-glide/pull/2055))
* Node: Added FLUSHDB command ([#1986](https://github.com/valkey-io/valkey-glide/pull/1986))
* Node: Added GETDEL command ([#1968](https://github.com/valkey-io/valkey-glide/pull/1968))
* Node: Added BITOP command ([#2012](https://github.com/valkey-io/valkey-glide/pull/2012))
Expand Down
24 changes: 23 additions & 1 deletion node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
StreamReadOptions,
StreamTrimOptions,
ZAddOptions,
createBLMove,
createBLPop,
createBRPop,
createBZMPop,
Expand Down Expand Up @@ -89,7 +90,6 @@ import {
createLInsert,
createLLen,
createLMove,
createBLMove,
createLPop,
createLPos,
createLPush,
Expand Down Expand Up @@ -136,6 +136,7 @@ import {
createSetBit,
createStrlen,
createTTL,
createTouch,
createType,
createUnlink,
createXAdd,
Expand Down Expand Up @@ -4323,6 +4324,27 @@ export class BaseClient {
);
}

/**
* Updates the last access time of the specified keys.
*
* See https://valkey.io/commands/touch/ for more details.
*
* @remarks When in cluster mode, the command may route to multiple nodes when `keys` map to different hash slots.
* @param keys - The keys to update the last access time of.
* @returns The number of keys that were updated. A key is ignored if it doesn't exist.
*
* @example
* ```typescript
* await client.set("key1", "value1");
* await client.set("key2", "value2");
* const result = await client.touch(["key1", "key2", "nonExistingKey"]);
* console.log(result); // Output: 2 - The last access time of 2 keys has been updated.
* ```
*/
public touch(keys: string[]): Promise<number> {
return this.createWritePromise(createTouch(keys));
}

/**
* @internal
*/
Expand Down
7 changes: 7 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2810,3 +2810,10 @@ export function createLCS(

return createCommand(RequestType.LCS, args);
}

/**
* @internal
*/
export function createTouch(keys: string[]): command_request.Command {
return createCommand(RequestType.Touch, keys);
}
16 changes: 15 additions & 1 deletion node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
StreamReadOptions,
StreamTrimOptions,
ZAddOptions,
createBLMove,
createBLPop,
createBRPop,
createBZMPop,
Expand Down Expand Up @@ -104,7 +105,6 @@ import {
createLInsert,
createLLen,
createLMove,
createBLMove,
createLPop,
createLPos,
createLPush,
Expand Down Expand Up @@ -155,6 +155,7 @@ import {
createStrlen,
createTTL,
createTime,
createTouch,
createType,
createUnlink,
createXAdd,
Expand Down Expand Up @@ -2580,6 +2581,19 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
): T {
return this.addAndReturn(createLCS(key1, key2, { idx: options ?? {} }));
}

/**
* Updates the last access time of the specified keys.
*
* See https://valkey.io/commands/touch/ for more details.
*
* @param keys - The keys to update the last access time of.
*
* Command Response - The number of keys that were updated. A key is ignored if it doesn't exist.
*/
public touch(keys: string[]): T {
return this.addAndReturn(createTouch(keys));
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion node/tests/RedisClusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ describe("GlideClusterClient", () => {
await client.del(["abc", "zxy", "lkn"]);
await client.mget(["abc", "zxy", "lkn"]);
await client.mset({ abc: "1", zxy: "2", lkn: "3" });
// TODO touch
await client.touch(["abc", "zxy", "lkn"]);
client.close();
},
);
Expand Down
20 changes: 20 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5904,6 +5904,26 @@ export function runBaseTests<Context>(config: {
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`touch test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key1 = `{key}-${uuidv4()}`;
const key2 = `{key}-${uuidv4()}`;
const nonExistingKey = `{key}-${uuidv4()}`;

expect(
await client.mset({ [key1]: "value1", [key2]: "value2" }),
).toEqual("OK");
expect(await client.touch([key1, key2])).toEqual(2);
expect(
await client.touch([key2, nonExistingKey, key1]),
).toEqual(2);
}, protocol);
},
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`zrandmember test_%p`,
async (protocol) => {
Expand Down
2 changes: 2 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,8 @@ export async function transactionTest(
responseData.push(["rename(key9, key10)", "OK"]);
baseTransaction.exists([key10]);
responseData.push(["exists([key10])", 1]);
baseTransaction.touch([key10]);
responseData.push(["touch([key10])", 1]);
baseTransaction.renamenx(key10, key9);
responseData.push(["renamenx(key10, key9)", true]);
baseTransaction.exists([key9, key10]);
Expand Down