Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node: Add LASTSAVE command. #2059

Merged
merged 6 commits into from
Aug 1, 2024
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
@@ -1,4 +1,5 @@
#### Changes
* Node: Added LASTSAVE command ([#2059](https://github.com/valkey-io/valkey-glide/pull/2059))
* Node: Added LCS command ([#2049](https://github.com/valkey-io/valkey-glide/pull/2049))
* Node: Added MSETNX command ([#2046](https://github.com/valkey-io/valkey-glide/pull/2046))
* Node: Added BLMOVE command ([#2027](https://github.com/valkey-io/valkey-glide/pull/2027))
Expand Down
5 changes: 5 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2787,6 +2787,11 @@ export function createZRandMember(
return createCommand(RequestType.ZRandMember, args);
}

/** @internal */
export function createLastSave(): command_request.Command {
return createCommand(RequestType.LastSave, []);
}

/** @internal */
export function createLCS(
key1: string,
Expand Down
18 changes: 18 additions & 0 deletions node/src/GlideClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
createFunctionList,
createFunctionLoad,
createInfo,
createLastSave,
createLolwut,
createPing,
createPublish,
Expand Down Expand Up @@ -611,6 +612,23 @@ export class GlideClient extends BaseClient {
return this.createWritePromise(createPublish(message, channel));
}

/**
* Returns `UNIX TIME` of the last DB save timestamp or startup timestamp if no save
* was made since then.
*
* See https://valkey.io/commands/lastsave/ for more details.
*
* @returns `UNIX TIME` of the last DB save executed with success.
* @example
* ```typescript
* const timestamp = await client.lastsave();
* console.log("Last DB save was done at " + timestamp);
* ```
*/
public async lastsave(): Promise<number> {
return this.createWritePromise(createLastSave());
}

/**
* Returns a random existing key name from the currently selected database.
*
Expand Down
27 changes: 25 additions & 2 deletions node/src/GlideClusterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
createFunctionList,
createFunctionLoad,
createInfo,
createLastSave,
createLolwut,
createPing,
createPublish,
Expand Down Expand Up @@ -935,7 +936,7 @@ export class GlideClusterClient extends BaseClient {
*
* See https://valkey.io/commands/dbsize/ for more details.

* @param route - The command will be routed to all primaries, unless `route` is provided, in which
* @param route - The command will be routed to all primary nodes, unless `route` is provided, in which
* case the client will route the command to the nodes defined by `route`.
* @returns The number of keys in the database.
* In the case of routing the query to multiple nodes, returns the aggregated number of keys across the different nodes.
Expand All @@ -946,7 +947,7 @@ export class GlideClusterClient extends BaseClient {
* console.log("Number of keys across all primary nodes: ", numKeys);
* ```
*/
public dbsize(route?: Routes): Promise<ClusterResponse<number>> {
public dbsize(route?: Routes): Promise<number> {
return this.createWritePromise(createDBSize(), toProtobufRoute(route));
}

Expand Down Expand Up @@ -985,6 +986,28 @@ export class GlideClusterClient extends BaseClient {
);
}

/**
* Returns `UNIX TIME` of the last DB save timestamp or startup timestamp if no save
* was made since then.
*
* See https://valkey.io/commands/lastsave/ for more details.
*
* @param route - (Optional) The command will be routed to a random node, unless `route` is provided, in which
* case the client will route the command to the nodes defined by `route`.
* @returns `UNIX TIME` of the last DB save executed with success.
* @example
* ```typescript
* const timestamp = await client.lastsave();
* console.log("Last DB save was done at " + timestamp);
* ```
*/
public async lastsave(route?: Routes): Promise<ClusterResponse<number>> {
return this.createWritePromise(
createLastSave(),
toProtobufRoute(route),
);
}

/**
* Returns a random existing key name.
*
Expand Down
13 changes: 13 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ import {
createZRevRank,
createZRevRankWithScore,
createZScore,
createLastSave,
} from "./Commands";
import { command_request } from "./ProtobufMessage";

Expand Down Expand Up @@ -2520,6 +2521,18 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createGeoHash(key, members));
}

/**
* Returns `UNIX TIME` of the last DB save timestamp or startup timestamp if no save
* was made since then.
*
* See https://valkey.io/commands/lastsave/ for more details.
*
* Command Response - `UNIX TIME` of the last DB save executed with success.
*/
public lastsave(): T {
return this.addAndReturn(createLastSave());
}

/**
* Returns all the longest common subsequences combined between strings stored at `key1` and `key2`.
*
Expand Down
2 changes: 1 addition & 1 deletion node/tests/RedisClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "@jest/globals";
import { BufferReader, BufferWriter } from "protobufjs";
import { v4 as uuidv4 } from "uuid";
import { GlideClient, ProtocolVersion, Transaction, ListDirection } from "..";
import { GlideClient, ListDirection, ProtocolVersion, Transaction } from "..";
import { RedisCluster } from "../../utils/TestUtils.js";
import { FlushMode } from "../build-ts/src/Commands";
import { command_request } from "../src/ProtobufMessage";
Expand Down
30 changes: 30 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
BitmapIndexType,
BitwiseOperation,
ClosingError,
ClusterTransaction,
ConditionalChange,
ExpireOptions,
FlushMode,
Expand All @@ -36,6 +37,7 @@ import {
Script,
SignedEncoding,
SortOrder,
Transaction,
UnsignedEncoding,
UpdateByScore,
parseInfoResponse,
Expand Down Expand Up @@ -303,6 +305,34 @@ export function runBaseTests<Context>(config: {
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"lastsave %p",
async (protocol) => {
await runTest(async (client: BaseClient) => {
const today = new Date();
today.setDate(today.getDate() - 1);
const yesterday = today.getTime() / 1000; // as epoch time

expect(await client.lastsave()).toBeGreaterThan(yesterday);

if (client instanceof GlideClusterClient) {
Object.values(await client.lastsave("allNodes")).forEach(
(v) => expect(v).toBeGreaterThan(yesterday),
);
}

const response =
client instanceof GlideClient
? await client.exec(new Transaction().lastsave())
: await client.exec(
new ClusterTransaction().lastsave(),
);
expect(response?.[0]).toBeGreaterThan(yesterday);
}, protocol);
},
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`testing mset and mget with multiple existing keys and one non existing key_%p`,
async (protocol) => {
Expand Down
Loading