-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathredis_client.index.ts
78 lines (72 loc) · 1.92 KB
/
redis_client.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
73
74
75
76
77
78
import { RedisClient } from './redis_client';
import { RedisCommands } from './redis_commands';
const client = new RedisClient();
client.connect();
process.stdin.on('data', async (input) => {
// Read the input provided by the user
const data = input.toString().trim();
if (data === 'exit') {
await client.disconnect();
process.exit(0);
}
try {
// The Redis commands are a list of strings.
const arr = data.split(' ');
const command = arr[0];
// Cross check if a valid command is provided or not.
// Send the command to the server
switch (command) {
case RedisCommands.PING: {
const message = arr[1];
await client.ping(message);
break;
}
case RedisCommands.ECHO: {
const message = arr[1];
if (message === undefined) {
console.error('Please provide a message');
break;
}
await client.echo(message);
break;
}
case RedisCommands.SET: {
const key = arr[1];
const value = arr[2];
if (key === undefined || value === undefined) {
console.error('Invalid key or value provided');
break;
}
await client.set(key, value);
break;
}
case RedisCommands.GET: {
const key = arr[1];
if (key === undefined) {
console.error('Please provide a key');
break;
}
const value = await client.get(key);
console.log(value);
break;
}
case RedisCommands.DEL: {
const key = arr[1];
if (key === undefined) {
console.error('Please provide a key');
break;
}
const value = await client.delete(key);
console.log(value);
break;
}
default:
console.error('Invalid command %s', command);
break;
}
} catch (e) {
if (e instanceof Error) {
console.error(e.message);
}
}
});