Factor Coin Gecko caching to methods

This commit is contained in:
Chris Eager authored and Chris Eager committed 2025-10-06 12:15:11 -05:00
1 parent ab1ec86cd2
commit b8a720c5b4
1 file changed
+30 -20
@@ -138,24 +138,15 @@ public class CurrencyConversionManager implements Managed {
private void updateCoinGeckoCacheIfNecessary() throws IOException { private void updateCoinGeckoCacheIfNecessary() throws IOException {
{ {
final Map<String, BigDecimal> coinGeckoValuesFromSharedCache = cacheCluster.withCluster(connection -> { final Map<String, BigDecimal> coinGeckoValuesFromSharedCache = getCachedData(COIN_GECKO_SHARED_CACHE_DATA_KEY);
final Map<String, BigDecimal> parsedSharedCacheData = new HashMap<>();
connection.sync().hgetall(COIN_GECKO_SHARED_CACHE_DATA_KEY).forEach((currency, conversionRate) ->
parsedSharedCacheData.put(currency, new BigDecimal(conversionRate)));
return parsedSharedCacheData;
});
if (coinGeckoValuesFromSharedCache != null && !coinGeckoValuesFromSharedCache.isEmpty()) { if (coinGeckoValuesFromSharedCache != null && !coinGeckoValuesFromSharedCache.isEmpty()) {
cachedCoinGeckoValues = coinGeckoValuesFromSharedCache; cachedCoinGeckoValues = coinGeckoValuesFromSharedCache;
} }
} }
final boolean shouldUpdateSharedCache = cacheCluster.withCluster(connection -> final boolean shouldUpdateSharedCache = shouldUpdateSharedCache(COIN_GECKO_SHARED_CACHE_CURRENT_KEY,
"OK".equals(connection.sync().set(COIN_GECKO_SHARED_CACHE_CURRENT_KEY, COIN_GECKO_REFRESH_INTERVAL);
"true",
SetArgs.Builder.nx().ex(COIN_GECKO_REFRESH_INTERVAL))));
if (shouldUpdateSharedCache || cachedCoinGeckoValues == null) { if (shouldUpdateSharedCache || cachedCoinGeckoValues == null) {
final Map<String, BigDecimal> conversionRatesFromCoinGecko = new HashMap<>(currencies.size()); final Map<String, BigDecimal> conversionRatesFromCoinGecko = new HashMap<>(currencies.size());
@@ -167,18 +158,37 @@ public class CurrencyConversionManager implements Managed {
cachedCoinGeckoValues = conversionRatesFromCoinGecko; cachedCoinGeckoValues = conversionRatesFromCoinGecko;
if (shouldUpdateSharedCache) { if (shouldUpdateSharedCache) {
cacheCluster.useCluster(connection -> { updateCachedData(COIN_GECKO_SHARED_CACHE_DATA_KEY, cachedCoinGeckoValues);
final Map<String, String> sharedCoinGeckoValues = new HashMap<>();
cachedCoinGeckoValues.forEach((currency, conversionRate) ->
sharedCoinGeckoValues.put(currency, conversionRate.toString()));
connection.sync().hset(COIN_GECKO_SHARED_CACHE_DATA_KEY, sharedCoinGeckoValues);
});
} }
} }
} }
private Map<String, BigDecimal> getCachedData(final String cacheKey) {
return cacheCluster.withCluster(connection -> {
final Map<String, BigDecimal> parsedSharedCacheData = new HashMap<>();
connection.sync().hgetall(cacheKey).forEach((currency, conversionRate) ->
parsedSharedCacheData.put(currency, new BigDecimal(conversionRate)));
return parsedSharedCacheData;
});
}
private boolean shouldUpdateSharedCache(final String cacheKey, final Duration interval) {
return cacheCluster.withCluster(connection ->
"OK".equals(connection.sync().set(cacheKey, "true", SetArgs.Builder.nx().ex(interval))));
}
private void updateCachedData(final String cacheKey, final Map<String, BigDecimal> data) {
cacheCluster.useCluster(connection -> {
final Map<String, String> sharedCoinGeckoValues = new HashMap<>();
data.forEach((currency, conversionRate) -> sharedCoinGeckoValues.put(currency, conversionRate.toString()));
connection.sync().hset(cacheKey, sharedCoinGeckoValues);
});
}
private BigDecimal stripTrailingZerosAfterDecimal(BigDecimal bigDecimal) { private BigDecimal stripTrailingZerosAfterDecimal(BigDecimal bigDecimal) {
BigDecimal n = bigDecimal.stripTrailingZeros(); BigDecimal n = bigDecimal.stripTrailingZeros();
if (n.scale() < 0) { if (n.scale() < 0) {