|
7 | 7 | import com.google.gson.JsonObject;
|
8 | 8 | import com.google.gson.JsonParser;
|
9 | 9 | import fi.hsl.common.pulsar.PulsarApplicationContext;
|
| 10 | +import fi.hsl.common.transitdata.TransitdataProperties; |
10 | 11 | import org.jetbrains.annotations.NotNull;
|
| 12 | +import org.jetbrains.annotations.Nullable; |
11 | 13 | import org.slf4j.Logger;
|
12 | 14 | import org.slf4j.LoggerFactory;
|
13 | 15 | import redis.clients.jedis.*;
|
|
17 | 19 | import java.nio.charset.StandardCharsets;
|
18 | 20 | import java.time.Duration;
|
19 | 21 | import java.time.OffsetDateTime;
|
| 22 | +import java.time.format.DateTimeFormatter; |
20 | 23 | import java.util.*;
|
21 | 24 | import java.util.concurrent.ThreadLocalRandom;
|
22 | 25 |
|
@@ -161,6 +164,78 @@ public List<String> getKeys(@NotNull final String prefix, @NotNull final String
|
161 | 164 | return new ArrayList<>(keys);
|
162 | 165 | }
|
163 | 166 | }
|
| 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 | + } |
164 | 239 |
|
165 | 240 | // Azure Cache for Redis helper code
|
166 | 241 | public static Jedis createJedisClient(String cacheHostname, int port, String username, AccessToken accessToken, boolean useSsl) {
|
|
0 commit comments