Skip to content

Commit 33496e0

Browse files
committed
revisions
1 parent b7b2daf commit 33496e0

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

articles/azure-cache-for-redis/cache-nodejs-get-started.md

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ For examples of using other Node.js clients, see the individual documentation fo
3030
Add environment variables for your **HOST NAME** and **Primary** access key. Use these variables from your code instead of including the sensitive information directly in your code.
3131

3232
```powershell
33-
set AZURE_CACHE_FOR_REDIS_HOSTNAME=contosoCache
34-
set AZURE_CACHE_FOR_REDIS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
33+
set AZURE_CACHE_FOR_REDIS_HOST_NAME=contosoCache
34+
set AZURE_CACHE_FOR_REDIS_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
3535
```
3636

3737
## Connect to the cache
@@ -49,7 +49,58 @@ The latest builds of [node_redis](https://github.com/mranney/node_redis) provide
4949

5050
1. Add the following example JavaScript to the file.
5151

52-
:::code language="javascript" source="~/azure-cache-redis-samples/quickstart/nodejs/redistest.js":::
52+
53+
```javascript
54+
const redis = require("redis");
55+
56+
// Environment variables for cache
57+
const cacheHostName = process.env.AZURE_CACHE_FOR_REDIS_HOST_NAME;
58+
const cachePassword = process.env.AZURE_CACHE_FOR_REDIS_ACCESS_KEY;
59+
60+
if(!cacheHostName) throw Error("AZURE_CACHE_FOR_REDIS_HOST_NAME is empty")
61+
if(!cachePassword) throw Error("AZURE_CACHE_FOR_REDIS_ACCESS_KEY is empty")
62+
63+
async function testCache() {
64+
65+
// Connection configuration
66+
const cacheConnection = redis.createClient({
67+
// rediss for TLS
68+
url: `rediss://${cacheHostName}:6380`,
69+
password: cachePassword
70+
});
71+
72+
// Connect to Redis
73+
await cacheConnection.connect();
74+
75+
// PING command
76+
console.log("\nCache command: PING");
77+
console.log("Cache response : " + await cacheConnection.ping());
78+
79+
// GET
80+
console.log("\nCache command: GET Message");
81+
console.log("Cache response : " + await cacheConnection.get("Message"));
82+
83+
// SET
84+
console.log("\nCache command: SET Message");
85+
console.log("Cache response : " + await cacheConnection.set("Message",
86+
"Hello! The cache is working from Node.js!"));
87+
88+
// GET again
89+
console.log("\nCache command: GET Message");
90+
console.log("Cache response : " + await cacheConnection.get("Message"));
91+
92+
// Client list, useful to see if connection list is growing...
93+
console.log("\nCache command: CLIENT LIST");
94+
console.log("Cache response : " + await cacheConnection.sendCommand(["CLIENT", "LIST"]));
95+
96+
// Disconnect
97+
cacheConnection.disconnect()
98+
99+
return "Done"
100+
}
101+
102+
testCache().then((result) => console.log(result)).catch(ex => console.log(ex));
103+
```
53104
54105
This code shows you how to connect to an Azure Cache for Redis instance using the cache host name and key environment variables. The code also stores and retrieves a string value in the cache. The `PING` and `CLIENT LIST` commands are also executed. For more examples of using Redis with the [node_redis](https://github.com/mranney/node_redis) client, see [https://redis.js.org/](https://redis.js.org/).
55106
@@ -76,7 +127,7 @@ The latest builds of [node_redis](https://github.com/mranney/node_redis) provide
76127
Cache response : Hello! The cache is working from Node.js!
77128
78129
Cache command: CLIENT LIST
79-
Cache response : id=3129885 addr=76.22.73.183:64798 fd=14 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 argv-mem=10 obl=0 oll=0 omem=0 tot-mem=61466 ow=0 owmem=0 events=r cmd=client user=default numops=6
130+
Cache response : id=10017364 addr=76.22.73.183:59380 fd=221 name= age=1 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 argv-mem=10 obl=0 oll=0 omem=0 tot-mem=61466 ow=0 owmem=0 events=r cmd=client user=default numops=6
80131
81132
Done
82133
```

0 commit comments

Comments
 (0)