Skip to content

Commit 0f89d84

Browse files
committed
Revert "Cleaning up"
This reverts commit 5f480b0.
1 parent 5f480b0 commit 0f89d84

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>fi.hsl</groupId>
44
<artifactId>transitdata-common</artifactId>
5-
<version>2.0.3-RC8</version>
5+
<version>2.0.3-RC7</version>
66
<packaging>jar</packaging>
77
<name>Common utilities for Transitdata projects</name>
88
<properties>

src/main/java/fi/hsl/common/pulsar/PulsarApplication.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,14 @@ public PulsarApplicationContext initialize(@NotNull Config config) throws Except
9393
}
9494

9595
if (config.getBoolean("redis.enabled")) {
96+
int connTimeOutSecs = 2;
97+
if (config.hasPath("redis.connTimeOutSecs")) {
98+
connTimeOutSecs = config.getInt("redis.connTimeOutSecs");
99+
}
96100
jedis = createRedisClient(
97101
config.getString("redis.host"),
98-
config.getInt("redis.port"));
102+
config.getInt("redis.port"),
103+
connTimeOutSecs);
99104
}
100105

101106
if (config.getBoolean("health.enabled")) {
@@ -154,8 +159,8 @@ public PulsarApplicationContext initialize(@NotNull Config config) throws Except
154159
}
155160

156161
@NotNull
157-
protected Jedis createRedisClient(@NotNull String redisHost, int port) {
158-
log.info("Connecting to Redis at " + redisHost + ":" + port);
162+
protected Jedis createRedisClient(@NotNull String redisHost, int port, int connTimeOutSecs) {
163+
log.info("Connecting to Redis at " + redisHost + ":" + port + " with connection timeout of (s): "+ connTimeOutSecs);
159164

160165
//Construct a Token Credential from Identity library, e.g. DefaultAzureCredential / ClientSecretCredential / Client CertificateCredential / ManagedIdentityCredential etc.
161166
DefaultAzureCredential defaultAzureCredential = new DefaultAzureCredentialBuilder().build();

src/main/java/fi/hsl/common/redis/RedisUtils.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import com.google.gson.JsonObject;
88
import com.google.gson.JsonParser;
99
import fi.hsl.common.pulsar.PulsarApplicationContext;
10+
import fi.hsl.common.transitdata.TransitdataProperties;
1011
import org.jetbrains.annotations.NotNull;
12+
import org.jetbrains.annotations.Nullable;
1113
import org.slf4j.Logger;
1214
import org.slf4j.LoggerFactory;
1315
import redis.clients.jedis.*;
@@ -17,6 +19,7 @@
1719
import java.nio.charset.StandardCharsets;
1820
import java.time.Duration;
1921
import java.time.OffsetDateTime;
22+
import java.time.format.DateTimeFormatter;
2023
import java.util.*;
2124
import java.util.concurrent.ThreadLocalRandom;
2225

@@ -161,6 +164,78 @@ public List<String> getKeys(@NotNull final String prefix, @NotNull final String
161164
return new ArrayList<>(keys);
162165
}
163166
}
167+
168+
/**
169+
* Fetches hash values for keys
170+
* @param keys
171+
* @return HashMap of keys and their hash values if they exist
172+
*/
173+
@NotNull
174+
public Map<@NotNull String, Optional<Map<@NotNull String, @NotNull String>>> getValuesByKeys(@NotNull final List<@NotNull String> keys) {
175+
synchronized (jedis) {
176+
final Transaction transaction = jedis.multi();
177+
final Map<String, Response<Map<String, String>>> responses = new HashMap<>();
178+
keys.forEach(key -> responses.put(key, transaction.hgetAll(key)));
179+
transaction.exec();
180+
181+
final Map<String, Optional<Map<String, String>>> values = new HashMap<>(responses.size());
182+
responses.forEach((k, v) -> {
183+
final Map<String, String> value = v.get();
184+
if (value == null || value.isEmpty()) {
185+
values.put(k, Optional.empty());
186+
} else {
187+
values.put(k, Optional.of(value));
188+
}
189+
});
190+
191+
return values;
192+
}
193+
}
194+
195+
/**
196+
* Fetches string values for keys
197+
* @param keys
198+
* @return HashMap of keys and their values if they exist
199+
*/
200+
@NotNull
201+
public Map<@NotNull String, Optional<String>> getValueBykeys(@NotNull final List<@NotNull String> keys) {
202+
synchronized (jedis) {
203+
final Transaction transaction = jedis.multi();
204+
final Map<String, Response<String>> responses = new HashMap<>();
205+
keys.forEach(key -> responses.put(key, transaction.get(key)));
206+
transaction.exec();
207+
208+
final Map<String, Optional<String>> values = new HashMap<>(responses.size());
209+
responses.forEach((k, v) -> {
210+
final String value = v.get();
211+
if (value == null || value.isEmpty()) {
212+
values.put(k, Optional.empty());
213+
} else {
214+
values.put(k, Optional.of(value));
215+
}
216+
});
217+
218+
return values;
219+
}
220+
}
221+
222+
@NotNull
223+
public String updateTimestamp() {
224+
synchronized (jedis) {
225+
final OffsetDateTime now = OffsetDateTime.now();
226+
final String ts = DateTimeFormatter.ISO_INSTANT.format(now);
227+
log.info("Updating Redis timestamp to {}", ts);
228+
return jedis.set(TransitdataProperties.KEY_LAST_CACHE_UPDATE_TIMESTAMP, ts);
229+
}
230+
}
231+
232+
public boolean checkResponse(@Nullable final String response) {
233+
return response != null && response.trim().equalsIgnoreCase("OK");
234+
}
235+
236+
public boolean checkResponse(@Nullable final Long response) {
237+
return response != null && response == 1;
238+
}
164239

165240
// Azure Cache for Redis helper code
166241
public static Jedis createJedisClient(String cacheHostname, int port, String username, AccessToken accessToken, boolean useSsl) {

0 commit comments

Comments
 (0)