From 33bb3eb1bceee07d03e27ed823abbcf2469cf829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=ED=95=98=EB=8B=B4?= Date: Tue, 13 Dec 2022 16:27:55 +0900 Subject: [PATCH 1/3] Modify to operate while maintaining v3 syntax in v4 using legacy mode --- .../creating-redis-service-containers.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/content/actions/using-containerized-services/creating-redis-service-containers.md b/content/actions/using-containerized-services/creating-redis-service-containers.md index 839feceba8ef..1b503290a034 100644 --- a/content/actions/using-containerized-services/creating-redis-service-containers.md +++ b/content/actions/using-containerized-services/creating-redis-service-containers.md @@ -282,30 +282,32 @@ 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}`, + legacyMode: true }); redisClient.on("error", function(err) { console.log("Error " + err); }); +redisClient.connect(); + // Sets the key "octocat" to a value of "Mona the octocat" -redisClient.set("octocat", "Mona the Octocat", redis.print); +redisClient.set("octocat", "Mona the Octocat", function (err, reply) { console.log("Reply: " + reply) }); // Sets a key to "octocat", field to "species", and "value" to "Cat and Octopus" -redisClient.hset("species", "octocat", "Cat and Octopus", redis.print); +redisClient.hset("species", "octocat", "Cat and Octopus", function (err, reply) { console.log("Reply: " + reply) }); // Sets a key to "octocat", field to "species", and "value" to "Dinosaur and Octopus" -redisClient.hset("species", "dinotocat", "Dinosaur and Octopus", redis.print); +redisClient.hset("species", "dinotocat", "Dinosaur and Octopus", function (err, reply) { console.log("Reply: " + reply) }); // 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 +redisClient.hset(["species", "robotocat", "Cat and Robot"], function (err, reply) { console.log("Reply: " + reply) }); +// Gets all fields in "species" key redisClient.hkeys("species", function (err, replies) { console.log(replies.length + " replies:"); replies.forEach(function (reply, i) { console.log(" " + i + ": " + reply); }); - redisClient.quit(); + redisClient.disconnect(); }); ``` From 02e7790c511367b563a2640b180d8aa394a6f509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=ED=95=98=EB=8B=B4?= Date: Tue, 13 Dec 2022 16:33:01 +0900 Subject: [PATCH 2/3] Fix some comments --- .../creating-redis-service-containers.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/actions/using-containerized-services/creating-redis-service-containers.md b/content/actions/using-containerized-services/creating-redis-service-containers.md index 1b503290a034..5e9a09a19dee 100644 --- a/content/actions/using-containerized-services/creating-redis-service-containers.md +++ b/content/actions/using-containerized-services/creating-redis-service-containers.md @@ -294,11 +294,11 @@ redisClient.connect(); // Sets the key "octocat" to a value of "Mona the octocat" redisClient.set("octocat", "Mona the Octocat", function (err, reply) { console.log("Reply: " + reply) }); -// Sets a key to "octocat", field to "species", and "value" to "Cat and Octopus" +// Sets a key to "species", field to "octocat", and "value" to "Cat and Octopus" redisClient.hset("species", "octocat", "Cat and Octopus", function (err, reply) { console.log("Reply: " + reply) }); -// Sets a key to "octocat", field to "species", and "value" to "Dinosaur and Octopus" +// Sets a key to "species", field to "dinotocat", and "value" to "Dinosaur and Octopus" redisClient.hset("species", "dinotocat", "Dinosaur and Octopus", function (err, reply) { console.log("Reply: " + reply) }); -// Sets a key to "octocat", field to "species", and "value" to "Cat and Robot" +// Sets a key to "species", field to "robotocat", and "value" to "Cat and Robot" redisClient.hset(["species", "robotocat", "Cat and Robot"], function (err, reply) { console.log("Reply: " + reply) }); // Gets all fields in "species" key From db350871ddf99b4d8bc36345d08eb29260071aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=ED=95=98=EB=8B=B4?= Date: Tue, 13 Dec 2022 16:57:41 +0900 Subject: [PATCH 3/3] Modify it to promise-based async code using ES6 and disable legacy mode --- .../creating-redis-service-containers.md | 51 +++++++++++-------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/content/actions/using-containerized-services/creating-redis-service-containers.md b/content/actions/using-containerized-services/creating-redis-service-containers.md index 5e9a09a19dee..5aedab0dbf5c 100644 --- a/content/actions/using-containerized-services/creating-redis-service-containers.md +++ b/content/actions/using-containerized-services/creating-redis-service-containers.md @@ -282,33 +282,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({ - url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`, - legacyMode: true + url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}` }); -redisClient.on("error", function(err) { - console.log("Error " + err); -}); - -redisClient.connect(); - -// Sets the key "octocat" to a value of "Mona the octocat" -redisClient.set("octocat", "Mona the Octocat", function (err, reply) { console.log("Reply: " + reply) }); -// Sets a key to "species", field to "octocat", and "value" to "Cat and Octopus" -redisClient.hset("species", "octocat", "Cat and Octopus", function (err, reply) { console.log("Reply: " + reply) }); -// Sets a key to "species", field to "dinotocat", and "value" to "Dinosaur and Octopus" -redisClient.hset("species", "dinotocat", "Dinosaur and Octopus", function (err, reply) { console.log("Reply: " + reply) }); -// Sets a key to "species", field to "robotocat", and "value" to "Cat and Robot" -redisClient.hset(["species", "robotocat", "Cat and Robot"], function (err, reply) { console.log("Reply: " + reply) }); - -// Gets all fields in "species" key -redisClient.hkeys("species", function (err, replies) { +redisClient.on("error", (err) => console.log("Error", err)); + +(async () => { + await redisClient.connect(); + + // 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.disconnect(); -}); + 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.