@@ -281,31 +281,40 @@ const redis = require("redis");
|
||||
// If REDIS_HOST is not set, the default host is localhost
|
||||
// If REDIS_PORT is not set, the default port is 6379
|
||||
const redisClient = redis.createClient({
|
||||
host: process.env.REDIS_HOST,
|
||||
port: process.env.REDIS_PORT
|
||||
url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`
|
||||
});
|
||||
|
||||
redisClient.on("error", function(err) {
|
||||
console.log("Error " + err);
|
||||
});
|
||||
redisClient.on("error", (err) => console.log("Error", err));
|
||||
|
||||
// Sets the key "octocat" to a value of "Mona the octocat"
|
||||
redisClient.set("octocat", "Mona the Octocat", redis.print);
|
||||
// Sets a key to "octocat", field to "species", and "value" to "Cat and Octopus"
|
||||
redisClient.hset("species", "octocat", "Cat and Octopus", redis.print);
|
||||
// Sets a key to "octocat", field to "species", and "value" to "Dinosaur and Octopus"
|
||||
redisClient.hset("species", "dinotocat", "Dinosaur and Octopus", redis.print);
|
||||
// Sets a key to "octocat", field to "species", and "value" to "Cat and Robot"
|
||||
redisClient.hset(["species", "robotocat", "Cat and Robot"], redis.print);
|
||||
// Gets all fields in "species" key
|
||||
(async () => {
|
||||
await redisClient.connect();
|
||||
|
||||
redisClient.hkeys("species", function (err, replies) {
|
||||
// Sets the key "octocat" to a value of "Mona the octocat"
|
||||
const setKeyReply = await redisClient.set("octocat", "Mona the Octocat");
|
||||
console.log("Reply: " + setKeyReply);
|
||||
// Sets a key to "species", field to "octocat", and "value" to "Cat and Octopus"
|
||||
const SetFieldOctocatReply = await redisClient.hSet("species", "octocat", "Cat and Octopus");
|
||||
console.log("Reply: " + SetFieldOctocatReply);
|
||||
// Sets a key to "species", field to "dinotocat", and "value" to "Dinosaur and Octopus"
|
||||
const SetFieldDinotocatReply = await redisClient.hSet("species", "dinotocat", "Dinosaur and Octopus");
|
||||
console.log("Reply: " + SetFieldDinotocatReply);
|
||||
// Sets a key to "species", field to "robotocat", and "value" to "Cat and Robot"
|
||||
const SetFieldRobotocatReply = await redisClient.hSet("species", "robotocat", "Cat and Robot");
|
||||
console.log("Reply: " + SetFieldRobotocatReply);
|
||||
|
||||
try {
|
||||
// Gets all fields in "species" key
|
||||
const replies = await redisClient.hKeys("species");
|
||||
console.log(replies.length + " replies:");
|
||||
replies.forEach(function (reply, i) {
|
||||
replies.forEach((reply, i) => {
|
||||
console.log(" " + i + ": " + reply);
|
||||
});
|
||||
redisClient.quit();
|
||||
});
|
||||
await redisClient.quit();
|
||||
}
|
||||
catch (err) {
|
||||
// statements to handle any exceptions
|
||||
}
|
||||
})();
|
||||
```
|
||||
|
||||
The script creates a new Redis client using the `createClient` method, which accepts a `host` and `port` parameter. The script uses the `REDIS_HOST` and `REDIS_PORT` environment variables to set the client's IP address and port. If `host` and `port` are not defined, the default host is `localhost` and the default port is 6379.
|
||||
|
||||
Reference in New Issue
Block a user