Skip to content

Commit 938b237

Browse files
authored
updates
1 parent b898abf commit 938b237

File tree

2 files changed

+78
-28
lines changed

2 files changed

+78
-28
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ Using Maven, generate a new quickstart app:
113113
---
114114

115115

116-
117116
1. Close the *pom.xml* file.
118117

119118
1. Open *App.java* and see the code with the following code:

articles/azure-cache-for-redis/cache-java-redisson-get-started.md

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ Save the *pom.xml* file.
122122

123123
Open *App.java* and replace the code with the following code:
124124

125+
126+
### [Microsoft Entra ID Authentication (recommended)](#tab/entraid)
125127
```java
126128
package example.demo;
127129

@@ -140,7 +142,6 @@ import javax.cache.configuration.Configuration;
140142
import javax.cache.configuration.MutableConfiguration;
141143
import java.time.LocalDateTime;
142144

143-
144145
/**
145146
* Redis test
146147
*
@@ -165,56 +166,106 @@ public class App {
165166

166167
System.out.println("\nCache Command : SET Message");
167168
map.put("Message",
168-
String.format("Hello! The cache is working from Java! %s", LocalDateTime.now()));
169+
String.format("Hello! The cache is working from Java! %s", LocalDateTime.now()));
169170

170171
// Demonstrate "SET Message" executed as expected
171172
System.out.println("\nCache Command : GET Message");
172173
System.out.println("Cache Response : " + map.get("Message"));
173174

174175
redissonClient.shutdown();
175-
}
176-
177-
private static Config getConfig(){
178-
if ("MicrosoftEntraID".equals(System.getenv("AUTH_TYPE"))) {
179-
System.out.println("Auth with Microsoft Entra ID");
180-
return getConfigAuthWithAAD();
181-
} else if ("RedisKey".equals(System.getenv("AUTH_TYPE"))) {
182-
System.out.println("Auth with Redis key");
183-
return getConfigAuthWithKey();
184-
}
185-
System.out.println("Auth with Redis key");
186-
return getConfigAuthWithKey();
187-
}
188176

189-
private static Config getConfigAuthWithKey() {
190-
// Connect to the Azure Cache for Redis over the TLS/SSL port using the key
191-
Config redissonconfig = new Config();
192-
redissonconfig.useSingleServer().setPassword(System.getenv("REDIS_CACHE_KEY"))
193-
.setAddress(String.format("rediss://%s:6380", System.getenv("REDIS_CACHE_HOSTNAME")));
194-
return redissonconfig;
195177
}
196178

197-
private static Config getConfigAuthWithAAD() {
179+
private static Config getConfig() {
198180
//Construct a Token Credential from Identity library, e.g. DefaultAzureCredential / ClientSecretCredential / Client CertificateCredential / ManagedIdentityCredential etc.
199181
DefaultAzureCredential defaultAzureCredential = new DefaultAzureCredentialBuilder().build();
200182

201183
// Fetch a Microsoft Entra token to be used for authentication.
202184
String token = defaultAzureCredential
203-
.getToken(new TokenRequestContext()
204-
.addScopes("acca5fbb-b7e4-4009-81f1-37e38fd66d78/.default")).block().getToken();
185+
.getToken(new TokenRequestContext()
186+
.addScopes("https://redis.azure.com/.default")).block().getToken();
205187

206188
// Connect to the Azure Cache for Redis over the TLS/SSL port using the key
207189
Config redissonconfig = new Config();
208190
redissonconfig.useSingleServer()
209-
.setAddress(String.format("rediss://%s:6380", System.getenv("REDIS_CACHE_HOSTNAME")))
210-
.setUsername(System.getenv("USER_NAME")) // (Required) Username is Object ID of your managed identity or service principal
211-
.setPassword(token); // Microsoft Entra access token as password is required.
191+
.setAddress(String.format("rediss://%s:6380", System.getenv("REDIS_CACHE_HOSTNAME")))
192+
.setUsername(System.getenv("USER_NAME")) // (Required) Username is Object ID of your managed identity or service principal
193+
.setPassword(token); // Microsoft Entra access token as password is required.
212194
return redissonconfig;
213195
}
196+
}
197+
198+
```
199+
200+
### [Access Key Authentication](#tab/accesskey)
201+
202+
```java
203+
package example.demo;
204+
205+
import org.redisson.Redisson;
206+
import org.redisson.api.RedissonClient;
207+
import org.redisson.config.Config;
208+
import org.redisson.jcache.configuration.RedissonConfiguration;
209+
210+
import javax.cache.Cache;
211+
import javax.cache.CacheManager;
212+
import javax.cache.Caching;
213+
import javax.cache.configuration.Configuration;
214+
import javax.cache.configuration.MutableConfiguration;
215+
import java.time.LocalDateTime;
216+
217+
/**
218+
* Redis test
219+
*
220+
*/
221+
public class App {
222+
public static void main(String[] args) {
223+
224+
Config redissonconfig = getConfig();
214225

226+
RedissonClient redissonClient = Redisson.create(redissonconfig);
227+
228+
MutableConfiguration<String, String> jcacheConfig = new MutableConfiguration<>();
229+
Configuration<String, String> config = RedissonConfiguration.fromInstance(redissonClient, jcacheConfig);
230+
231+
// Perform cache operations using JCache
232+
CacheManager manager = Caching.getCachingProvider().getCacheManager();
233+
Cache<String, String> map = manager.createCache("test", config);
234+
235+
// Simple get and put of string data into the cache
236+
System.out.println("\nCache Command : GET Message");
237+
System.out.println("Cache Response : " + map.get("Message"));
238+
239+
System.out.println("\nCache Command : SET Message");
240+
map.put("Message",
241+
String.format("Hello! The cache is working from Java! %s", LocalDateTime.now()));
242+
243+
// Demonstrate "SET Message" executed as expected
244+
System.out.println("\nCache Command : GET Message");
245+
System.out.println("Cache Response : " + map.get("Message"));
246+
247+
redissonClient.shutdown();
248+
249+
}
250+
251+
private static Config getConfig() {
252+
// Connect to the Azure Cache for Redis over the TLS/SSL port using the key
253+
Config redissonconfig = new Config();
254+
redissonconfig.useSingleServer().setPassword(System.getenv("REDIS_CACHE_KEY"))
255+
.setAddress(String.format("rediss://%s:6380", System.getenv("REDIS_CACHE_HOSTNAME")));
256+
return redissonconfig;
257+
}
215258
}
259+
216260
```
217261

262+
---
263+
264+
265+
266+
```java
267+
268+
218269
This code shows you how to connect to an Azure Cache for Redis instance using Microsoft Entra ID with the JCache API support from the Redisson client library. The code also stores and retrieves a string value in the cache. For more information on JCache, see the [JCache specification](https://jcp.org/en/jsr/detail?id=107).
219270

220271
Save *App.java*.

0 commit comments

Comments
 (0)