-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathindex.ts
72 lines (64 loc) · 2.48 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
*/
import { GlideClient, GlideClusterClient, Logger } from "@valkey/valkey-glide";
async function sendPingToStandAloneNode() {
// When Valkey is in standalone mode, add address of the primary node, and any replicas you'd like to be able to read from.
const addresses = [
{
host: "localhost",
port: 6379,
},
];
// Check `GlideClientConfiguration/GlideClusterClientConfiguration` for additional options.
const client = await GlideClient.createClient({
addresses: addresses,
// if the server uses TLS, you'll need to enable it. Otherwise the connection attempt will time out silently.
// useTLS: true,
clientName: "test_standalone_client",
});
// The empty array signifies that there are no additional arguments.
const pong = await client.customCommand(["PING"]);
console.log(pong);
await send_set_and_get(client);
client.close();
}
async function send_set_and_get(client: GlideClient | GlideClusterClient) {
const set_response = await client.set("foo", "bar");
console.log(`Set response is = ${set_response}`);
const get_response = await client.get("foo");
console.log(`Get response is = ${get_response}`);
}
async function sendPingToRandomNodeInCluster() {
// When Valkey is in cluster mode, add address of any nodes, and the client will find all nodes in the cluster.
const addresses = [
{
host: "localhost",
port: 6380,
},
];
// Check `GlideClientConfiguration/GlideClusterClientConfiguration` for additional options.
const client = await GlideClusterClient.createClient({
addresses: addresses,
// if the cluster nodes use TLS, you'll need to enable it. Otherwise the connection attempt will time out silently.
// useTLS: true,
clientName: "test_cluster_client",
});
// The empty array signifies that there are no additional arguments.
const pong = await client.customCommand(["PING"], { route: "randomNode" });
console.log(pong);
await send_set_and_get(client);
client.close();
}
function setFileLogger() {
Logger.setLoggerConfig("warn", "glide.log");
}
function setConsoleLogger() {
Logger.setLoggerConfig("warn");
}
setFileLogger();
setConsoleLogger();
// Enable for standalone mode
await sendPingToStandAloneNode();
// Enable for cluster mode
// await sendPingToRandomNodeInCluster();