You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#Customer intent: As a Node.js developer, new to Azure Cache for Redis, I want to create a new Node.js app that uses Azure Cache for Redis.
12
12
---
13
13
# Quickstart: Use Azure Cache for Redis in Node.js
14
14
15
15
In this quickstart, you incorporate Azure Cache for Redis into a Node.js app to have access to a secure, dedicated cache that is accessible from any application within Azure.
16
16
17
-
## Skip to the code on GitHub
18
-
19
-
If you want to skip straight to the code, see the [Node.js quickstart](https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/nodejs) on GitHub.
20
-
21
17
## Prerequisites
22
18
23
19
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
@@ -34,103 +30,129 @@ For examples of using other Node.js clients, see the individual documentation fo
34
30
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.
35
31
36
32
```powershell
37
-
set REDISCACHEHOSTNAME=contosoCache.redis.cache.windows.net
38
-
set REDISCACHEKEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
33
+
set AZURE_CACHE_FOR_REDIS_HOST_NAME=contosoCache
34
+
set AZURE_CACHE_FOR_REDIS_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
39
35
```
40
36
41
37
## Connect to the cache
42
38
43
-
The latest builds of [node_redis](https://github.com/mranney/node_redis) provide support for connecting to Azure Cache for Redis using TLS. The following example shows how to connect to Azure Cache for Redis using the TLS endpoint of 6380.
44
-
45
-
```js
46
-
var redis =require("redis");
47
-
48
-
// Add your cache name and access key.
49
-
var client =redis.createClient(6380, process.env.REDISCACHEHOSTNAME,
Don't create a new connection for each operation in your code. Instead, reuse connections as much as possible.
39
+
The latest builds of [node_redis](https://github.com/mranney/node_redis) provide support several connection options. Don't create a new connection for each operation in your code. Instead, reuse connections as much as possible.
54
40
55
41
## Create a new Node.js app
56
42
57
-
Create a new script file named *redistest.js*. Use the command `npm install redis bluebird` to install required packages.
58
-
59
-
Add the following example JavaScript to the file. 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/).
60
-
61
-
```js
62
-
var redis =require("redis");
63
-
64
-
asyncfunctiontestCache() {
65
-
66
-
// Connect to the Azure Cache for Redis over the TLS port using the key.
67
-
var cacheHostName =process.env.REDISCACHEHOSTNAME;
68
-
var cachePassword =process.env.REDISCACHEKEY;
69
-
var cacheConnection =redis.createClient({
70
-
// rediss for TLS
71
-
url:"rediss://"+ cacheHostName +":6380",
72
-
password: cachePassword,
73
-
});
74
-
awaitcacheConnection.connect();
75
-
76
-
// Perform cache operations using the cache connection object...
In the example below, you can see the `Message` key previously had a cached value, which was set using the Redis Console in the Azure portal. The app updated that cached value. The app also executed the `PING` and `CLIENT LIST` commands.
This code shows you how to connect to an Azure Cache forRedis instance using the cache host name and key environment variables. The code also stores and retrieves a string valuein 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/).
106
+
107
+
108
+
1. Run the script with Node.js.
109
+
110
+
```bash
111
+
node redistest.js
112
+
```
113
+
114
+
1. Example the output.
115
+
116
+
```console
117
+
Cache command: PING
118
+
Cache response : PONG
119
+
120
+
Cache command: GET Message
121
+
Cache response : Hello! The cache is working from Node.js!
122
+
123
+
Cache command: SET Message
124
+
Cache response : OK
125
+
126
+
Cache command: GET Message
127
+
Cache response : Hello! The cache is working from Node.js!
If you continue to the next tutorial, can keep the resources created in this quickstart and reuse them.
118
-
119
-
Otherwise, if you're finished with the quickstart sample application, you can delete the Azure resources created in this quickstart to avoid charges.
137
+
If you continue to the next tutorial, can keep the resources created in this quickstart and reuse them. Otherwise, if you're finished with the quickstart sample application, you can delete the Azure resources created in this quickstart to avoid charges.
120
138
121
139
> [!IMPORTANT]
122
-
> Deleting a resource group is irreversible and that the resource group and all the resources in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the resources for hosting this sample inside an existing resource group that contains resources you want to keep, you can delete each resource individually on the left instead of deleting the resource group.
140
+
> Deleting a resource group is irreversible and that the resource group and all the resources in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the resources for hosting this sample inside an existing resource group that contains resources you want to keep, you can delete each resource individually instead of deleting the resource group.
123
141
>
124
142
125
-
Sign in to the [Azure portal](https://portal.azure.com) and select **Resource groups**.
143
+
1. Sign in to the [Azure portal](https://portal.azure.com) and select **Resource groups**.
144
+
145
+
1. In the **Filter by name** text box, enter the name of your resource group. The instructions for this article used a resource group named *TestResources*. On your resource group in the result list, select **...** then **Delete resource group**.
In the **Filter by name** text box, enter the name of your resource group. The instructions for this article used a resource group named *TestResources*. On your resource group in the result list, select **...** then **Delete resource group**.
149
+
1. Confirm the deletion of the resource group. Enter the name of your resource group to confirm, and select **Delete**.
0 commit comments