@@ -5,7 +5,6 @@ author: KarlErickson
5
5
ms.author : zhihaoguo
6
6
ms.date : 01/04/2022
7
7
ms.topic : quickstart
8
-
9
8
ms.devlang : java
10
9
ms.custom : devx-track-java, devx-track-javaee, mode-api, mvc, devx-track-extended-java, ignite-2024
11
10
---
@@ -31,7 +30,6 @@ Clone the repo [Java quickstart](https://github.com/Azure-Samples/azure-cache-re
31
30
32
31
[ !INCLUDE [ redis-setup-working-environment] ( includes/redis-setup-working-environment.md )]
33
32
34
-
35
33
## Create a new Java app
36
34
37
35
1 . Use maven to generate a new quickstart app:
@@ -47,8 +45,8 @@ Clone the repo [Java quickstart](https://github.com/Azure-Samples/azure-cache-re
47
45
-Dversion=1.0
48
46
```
49
47
50
- 1. Change to the new **redis-jedis-test** project directory.
51
- 1. Open the *pom.xml* file. In the file, you see a dependency for [Jedis](https://github.com/xetorthio/jedis):
48
+ 1. Change to the new **redis-jedis-test** project directory.
49
+ 1. Open the ** pom.xml* * file. In the file, you see a dependency for [Jedis](https://github.com/xetorthio/jedis):
52
50
53
51
### [Microsoft Entra ID Authentication (recommended)](#tab/entraid)
54
52
@@ -66,7 +64,6 @@ Clone the repo [Java quickstart](https://github.com/Azure-Samples/azure-cache-re
66
64
</dependency >
67
65
```
68
66
69
-
70
67
### [Access Key Authentication](#tab/accesskey)
71
68
72
69
[!INCLUDE [redis-access-key-alert](includes/redis-access-key-alert.md)]
@@ -78,31 +75,31 @@ Clone the repo [Java quickstart](https://github.com/Azure-Samples/azure-cache-re
78
75
<version >5.1.0</version > <!-- {x-version-update;redis.clients:jedis;external_dependency} -->
79
76
</dependency >
80
77
```
81
-
82
- ---
83
78
84
- 1. Close the *pom.xml* file.
85
- 1. Open *App.java* and see the code with the following code:
86
-
79
+ 1. Close the **pom.xml** file.
80
+
81
+ 1. Open **App.java** and see the code with the following code:
82
+
87
83
### [Microsoft Entra ID Authentication (recommended)](#tab/entraid)
88
- ```java
84
+
85
+ ```java
89
86
package example.demo;
90
-
87
+
91
88
import com.azure.identity.DefaultAzureCredential;
92
89
import com.azure.identity.DefaultAzureCredentialBuilder;
93
90
import com.azure.core.credential.TokenRequestContext;
94
91
import redis.clients.jedis.DefaultJedisClientConfig;
95
92
import redis.clients.jedis.Jedis;
96
-
93
+
97
94
/**
98
95
* Redis test
99
96
*
100
97
*/
101
- public class App
98
+ public class App
102
99
{
103
100
public static void main( String[] args )
104
101
{
105
-
102
+
106
103
boolean useSsl = true;
107
104
108
105
//Construct a Token Credential from Identity library, e.g. DefaultAzureCredential / ClientSecretCredential / Client CertificateCredential / ManagedIdentityCredential etc.
@@ -149,62 +146,63 @@ Clone the repo [Java quickstart](https://github.com/Azure-Samples/azure-cache-re
149
146
```
150
147
151
148
### [Access Key Authentication](#tab/accesskey)
149
+
152
150
```java
153
151
package example.demo;
154
-
152
+
155
153
import redis.clients.jedis.DefaultJedisClientConfig;
156
154
import redis.clients.jedis.Jedis;
157
-
155
+
158
156
/**
159
157
* Redis test
160
158
*
161
159
*/
162
- public class App
160
+ public class App
163
161
{
164
162
public static void main( String[] args )
165
163
{
166
-
164
+
167
165
boolean useSsl = true;
168
166
String cacheHostname = System.getenv("REDIS_CACHE_HOSTNAME");
169
167
String cachekey = System.getenv("REDIS_CACHE_KEY");
170
-
168
+
171
169
// Connect to the Azure Cache for Redis over the TLS/SSL port using the key.
172
170
Jedis jedis = new Jedis(cacheHostname, 6380, DefaultJedisClientConfig.builder()
173
171
.password(cachekey)
174
172
.ssl(useSsl)
175
173
.build());
176
-
174
+
177
175
// Perform cache operations using the cache connection object...
178
-
176
+
179
177
// Simple PING command
180
178
System.out.println( "\nCache Command : Ping" );
181
179
System.out.println( "Cache Response : " + jedis.ping());
182
-
180
+
183
181
// Simple get and put of integral data types into the cache
184
182
System.out.println( "\nCache Command : GET Message" );
185
183
System.out.println( "Cache Response : " + jedis.get("Message"));
186
-
184
+
187
185
System.out.println( "\nCache Command : SET Message" );
188
186
System.out.println( "Cache Response : " + jedis.set("Message", "Hello! The cache is working from Java!"));
189
-
187
+
190
188
// Demonstrate "SET Message" executed as expected...
191
189
System.out.println( "\nCache Command : GET Message" );
192
190
System.out.println( "Cache Response : " + jedis.get("Message"));
193
-
191
+
194
192
// Get the client list, useful to see if connection list is growing...
195
193
System.out.println( "\nCache Command : CLIENT LIST" );
196
194
System.out.println( "Cache Response : " + jedis.clientList());
197
-
195
+
198
196
jedis.close();
199
197
}
200
198
}
201
199
```
200
+
202
201
---
203
-
204
- 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.
205
202
206
- 1. Close the *App.java*.
203
+ 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.
207
204
205
+ 1. Close the **App.java** file.
208
206
209
207
## Build and run the app
210
208
@@ -235,7 +233,6 @@ Cache Response : id=777430 addr= :58989 fd=22 name= age=1 idle=0 fla
235
233
236
234
[ !INCLUDE [ redis-cache-resource-group-clean-up] ( includes/redis-cache-resource-group-clean-up.md )]
237
235
238
-
239
236
## Next steps
240
237
241
238
In this quickstart, you learned how to use Azure Cache for Redis from a Java application. Continue to the next quickstart to use Azure Cache for Redis with an ASP.NET web app.
0 commit comments