Skip to content

Commit

Permalink
Remove unnecessary cache use in RedisJedisManager
Browse files Browse the repository at this point in the history
The cache has no expiration, so it's equivalent to `ConcurrentHashMap`
with `Map.computeIfAbsent`.
  • Loading branch information
findepi committed Jan 21, 2022
1 parent 51f18a1 commit 7c55910
Showing 1 changed file with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
*/
package io.trino.plugin.redis;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import io.airlift.log.Logger;
import io.trino.spi.HostAddress;
import io.trino.spi.NodeManager;
Expand All @@ -26,6 +23,8 @@
import javax.inject.Inject;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static java.lang.Math.toIntExact;
import static java.util.Objects.requireNonNull;
Expand All @@ -37,7 +36,7 @@ public class RedisJedisManager
{
private static final Logger log = Logger.get(RedisJedisManager.class);

private final LoadingCache<HostAddress, JedisPool> jedisPoolCache;
private final ConcurrentMap<HostAddress, JedisPool> jedisPoolCache = new ConcurrentHashMap<>();

private final RedisConnectorConfig redisConnectorConfig;
private final JedisPoolConfig jedisPoolConfig;
Expand All @@ -48,14 +47,13 @@ public class RedisJedisManager
NodeManager nodeManager)
{
this.redisConnectorConfig = requireNonNull(redisConnectorConfig, "redisConnectorConfig is null");
this.jedisPoolCache = CacheBuilder.newBuilder().build(CacheLoader.from(this::createConsumer));
this.jedisPoolConfig = new JedisPoolConfig();
}

@PreDestroy
public void tearDown()
{
for (Map.Entry<HostAddress, JedisPool> entry : jedisPoolCache.asMap().entrySet()) {
for (Map.Entry<HostAddress, JedisPool> entry : jedisPoolCache.entrySet()) {
try {
entry.getValue().destroy();
}
Expand All @@ -73,7 +71,7 @@ public RedisConnectorConfig getRedisConnectorConfig()
public JedisPool getJedisPool(HostAddress host)
{
requireNonNull(host, "host is null");
return jedisPoolCache.getUnchecked(host);
return jedisPoolCache.computeIfAbsent(host, this::createConsumer);
}

private JedisPool createConsumer(HostAddress host)
Expand Down

0 comments on commit 7c55910

Please sign in to comment.