-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUntitled-1.js
64 lines (55 loc) · 1.38 KB
/
Untitled-1.js
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
/**
* @file redisClient.js
* @description Simple Redis client that writes a record to a local Redis database.
* @version 1.0.0
* @date 2023-10-05
* @license MIT
*
* @dependencies
* - redis: Redis client for Node.js
*
* @usage
* To run the script, use the following command:
* ```
* node redisClient.js
* ```
*
* @notes
* - Ensure that Redis is running locally before running the script.
*
* @maintainer
* - GitHub Copilot
*/
const redis = require('redis');
// Redis connection options
const redisOptions = {
url: 'redis://localhost:6379',
};
// Sample record to insert
const sampleRecord = {
name: 'John Doe',
email: 'john.doe@example.com',
age: 30,
createdAt: new Date().toISOString(),
};
async function writeRecord() {
const client = redis.createClient(redisOptions);
client.on('error', (err) => console.error('Redis Client Error', err));
try {
// Connect to Redis
await client.connect();
console.log('Connected to Redis');
// Insert the sample record
const key = 'user:1001';
await client.set(key, JSON.stringify(sampleRecord));
console.log(`Record inserted with key: ${key}`);
} catch (error) {
console.error('Error writing record to Redis:', error);
} finally {
// Disconnect from Redis
await client.disconnect();
console.log('Redis connection closed');
}
}
// Run the writeRecord function
writeRecord();