Skip to content

Commit 2d1c27a

Browse files
ChoHadamhubwriter
andauthored
Update creating-redis-service-containers.md (#22662)
Co-authored-by: hubwriter <hubwriter@github.com>
1 parent 7eb1a8c commit 2d1c27a

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

content/actions/using-containerized-services/creating-redis-service-containers.md

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -281,31 +281,40 @@ const redis = require("redis");
281281
// If REDIS_HOST is not set, the default host is localhost
282282
// If REDIS_PORT is not set, the default port is 6379
283283
const redisClient = redis.createClient({
284-
host: process.env.REDIS_HOST,
285-
port: process.env.REDIS_PORT
284+
url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`
286285
});
287286

288-
redisClient.on("error", function(err) {
289-
console.log("Error " + err);
290-
});
291-
292-
// Sets the key "octocat" to a value of "Mona the octocat"
293-
redisClient.set("octocat", "Mona the Octocat", redis.print);
294-
// Sets a key to "octocat", field to "species", and "value" to "Cat and Octopus"
295-
redisClient.hset("species", "octocat", "Cat and Octopus", redis.print);
296-
// Sets a key to "octocat", field to "species", and "value" to "Dinosaur and Octopus"
297-
redisClient.hset("species", "dinotocat", "Dinosaur and Octopus", redis.print);
298-
// Sets a key to "octocat", field to "species", and "value" to "Cat and Robot"
299-
redisClient.hset(["species", "robotocat", "Cat and Robot"], redis.print);
300-
// Gets all fields in "species" key
301-
302-
redisClient.hkeys("species", function (err, replies) {
287+
redisClient.on("error", (err) => console.log("Error", err));
288+
289+
(async () => {
290+
await redisClient.connect();
291+
292+
// Sets the key "octocat" to a value of "Mona the octocat"
293+
const setKeyReply = await redisClient.set("octocat", "Mona the Octocat");
294+
console.log("Reply: " + setKeyReply);
295+
// Sets a key to "species", field to "octocat", and "value" to "Cat and Octopus"
296+
const SetFieldOctocatReply = await redisClient.hSet("species", "octocat", "Cat and Octopus");
297+
console.log("Reply: " + SetFieldOctocatReply);
298+
// Sets a key to "species", field to "dinotocat", and "value" to "Dinosaur and Octopus"
299+
const SetFieldDinotocatReply = await redisClient.hSet("species", "dinotocat", "Dinosaur and Octopus");
300+
console.log("Reply: " + SetFieldDinotocatReply);
301+
// Sets a key to "species", field to "robotocat", and "value" to "Cat and Robot"
302+
const SetFieldRobotocatReply = await redisClient.hSet("species", "robotocat", "Cat and Robot");
303+
console.log("Reply: " + SetFieldRobotocatReply);
304+
305+
try {
306+
// Gets all fields in "species" key
307+
const replies = await redisClient.hKeys("species");
303308
console.log(replies.length + " replies:");
304-
replies.forEach(function (reply, i) {
309+
replies.forEach((reply, i) => {
305310
console.log(" " + i + ": " + reply);
306311
});
307-
redisClient.quit();
308-
});
312+
await redisClient.quit();
313+
}
314+
catch (err) {
315+
// statements to handle any exceptions
316+
}
317+
})();
309318
```
310319
311320
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.

0 commit comments

Comments
 (0)