#2319 - redis - tidy whitespace

This commit is contained in:
rbygrave
2021-08-17 22:02:57 +12:00
parent 9161e17476
commit 96b4fbea4b
10 changed files with 5 additions and 65 deletions
@@ -9,7 +9,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class DuelCache implements ServerCache, NearCacheInvalidate {
public final class DuelCache implements ServerCache, NearCacheInvalidate {
private final DefaultServerCache near;
private final RedisCache remote;
@@ -46,10 +46,8 @@ public class DuelCache implements ServerCache, NearCacheInvalidate {
@Override
public Map<Object, Object> getAll(Set<Object> keys) {
Map<Object, Object> resultMap = near.getAll(keys);
Set<Object> localKeys = resultMap.keySet();
Set<Object> remainingKeys = new HashSet<>();
for (Object key : keys) {
if (!localKeys.contains(key)) {
@@ -64,7 +62,6 @@ public class DuelCache implements ServerCache, NearCacheInvalidate {
resultMap.putAll(remoteMap);
}
}
return resultMap;
}
@@ -20,7 +20,7 @@ import java.util.Base64;
* Note that this now internally uses java.util.Base64 to encode the values.
* </p>
*/
public class ModId {
public final class ModId {
private static final SecureRandom shortIdSecureRandom = new SecureRandom();
@@ -35,7 +35,6 @@ public class ModId {
* </p>
*/
public static String id() {
// Random 72 bits
byte[] randomBytes = new byte[9];
shortIdSecureRandom.nextBytes(randomBytes);
@@ -24,7 +24,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
class RedisCache implements ServerCache {
final class RedisCache implements ServerCache {
private static final Logger log = LoggerFactory.getLogger(RedisCache.class);
@@ -35,7 +35,6 @@ class RedisCache implements ServerCache {
private final String cacheKey;
private final Encode keyEncode;
private final Encode valueEncode;
private final TimedMetric metricGet;
private final TimedMetric metricGetAll;
private final TimedMetric metricPut;
@@ -47,7 +46,6 @@ class RedisCache implements ServerCache {
private final CountMetric missCount;
RedisCache(JedisPool jedisPool, ServerCacheConfig config, Encode valueEncode) {
this.jedisPool = jedisPool;
this.cacheKey = config.getCacheKey();
this.keyEncode = new EncodePrefixKey(config.getCacheKey());
@@ -106,10 +104,8 @@ class RedisCache implements ServerCache {
@Override
public Map<Object, Object> getAll(Set<Object> keys) {
long start = System.nanoTime();
Map<Object, Object> map = new LinkedHashMap<>();
List<Object> keyList = new ArrayList<>(keys);
try (Jedis resource = jedisPool.getResource()) {
List<byte[]> valsAsBytes = resource.mget(keysAsBytes(keyList));
@@ -147,7 +143,6 @@ class RedisCache implements ServerCache {
}
}
@Override
public void put(Object id, Object value) {
long start = System.nanoTime();
@@ -203,7 +198,6 @@ class RedisCache implements ServerCache {
public void clear() {
long start = System.nanoTime();
try (Jedis resource = jedisPool.getResource()) {
ScanParams params = new ScanParams();
params.match(cacheKey + ":*");
@@ -224,7 +218,6 @@ class RedisCache implements ServerCache {
next = SafeEncoder.encode(nextCursor);
} while (!next.equals("0"));
metricClear.addSinceNanos(start);
}
}
@@ -38,14 +38,12 @@ import java.util.concurrent.ConcurrentHashMap;
import static java.util.Arrays.asList;
class RedisCacheFactory implements ServerCacheFactory {
final class RedisCacheFactory implements ServerCacheFactory {
private static final Logger log = LoggerFactory.getLogger(RedisCacheFactory.class);
private static final Logger queryLogger = LoggerFactory.getLogger("io.ebean.cache.QUERY");
private static final Logger logger = LoggerFactory.getLogger("io.ebean.cache.CACHE");
private static final Logger tableModLogger = LoggerFactory.getLogger("io.ebean.cache.TABLEMODS");
private static final int MSG_NEARCACHE_CLEAR = 1;
@@ -66,30 +64,22 @@ class RedisCacheFactory implements ServerCacheFactory {
private static final byte[] CHANNEL_NEAR_BYTES = SafeEncoder.encode(CHANNEL_NEAR);
private final ConcurrentHashMap<String, RQueryCache> queryCaches = new ConcurrentHashMap<>();
private final Map<String, NearCacheInvalidate> nearCacheMap = new ConcurrentHashMap<>();
private final EncodeManyIdsData encodeManyIdsData = new EncodeManyIdsData();
private final EncodeBeanData encodeBeanData = new EncodeBeanData();
private final EncodeSerializable encodeSerializable = new EncodeSerializable();
private final BackgroundExecutor executor;
private final JedisPool jedisPool;
private final DaemonTopicRunner daemonTopicRunner;
private final NearCacheNotify nearCacheNotify;
private final TimedMetric metricOutNearCache;
private final TimedMetric metricOutTableMod;
private final TimedMetric metricOutQueryCache;
private final TimedMetric metricInNearCache;
private final TimedMetric metricInTableMod;
private final TimedMetric metricInQueryCache;
private final String serverId = ModId.id();
private ServerCacheNotify listener;
RedisCacheFactory(DatabaseConfig config, BackgroundExecutor executor) {
@@ -148,7 +138,6 @@ class RedisCacheFactory implements ServerCacheFactory {
}
private ServerCache createNormalCache(ServerCacheConfig config) {
RedisCache redisCache = createRedisCache(config);
boolean nearCache = config.getCacheOptions().isNearCache();
if (!nearCache) {
@@ -156,17 +145,14 @@ class RedisCacheFactory implements ServerCacheFactory {
}
String cacheKey = config.getCacheKey();
DefaultServerCache near = new DefaultServerCache(new DefaultServerCacheConfig(config));
near.periodicTrim(executor);
DuelCache duelCache = new DuelCache(near, redisCache, cacheKey, nearCacheNotify);
nearCacheMap.put(cacheKey, duelCache);
return duelCache;
}
private RedisCache createRedisCache(ServerCacheConfig config) {
switch (config.getType()) {
case NATURAL_KEY:
return new RedisCache(jedisPool, config, encodeSerializable);
@@ -247,15 +233,12 @@ class RedisCacheFactory implements ServerCacheFactory {
@Override
public void notify(ServerCacheNotification tableModifications) {
Set<String> dependentTables = tableModifications.getDependentTables();
if (dependentTables != null && !dependentTables.isEmpty()) {
StringBuilder msg = new StringBuilder(50);
for (String table : dependentTables) {
msg.append(table).append(",");
}
String formattedMsg = msg.toString();
if (tableModLogger.isDebugEnabled()) {
tableModLogger.debug("Publish TableMods - {}", formattedMsg);
@@ -11,25 +11,15 @@ import java.util.Properties;
public class RedisConfig {
private String server = "localhost";
private int port = 6379;
private int maxTotal = 200;
private int maxIdle = 200;
private int minIdle = 1;
private long maxWaitMillis = -1L;
private boolean blockWhenExhausted = true;
private int timeout = 2000;
private String username;
private String password;
private boolean ssl;
/**
@@ -12,16 +12,13 @@ public class EncodeBeanData implements Encode {
@Override
public byte[] encode(Object value) {
try {
CachedBeanData data = (CachedBeanData) value;
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
data.writeExternal(oos);
oos.flush();
oos.close();
return os.toByteArray();
} catch (IOException e) {
@@ -31,11 +28,9 @@ public class EncodeBeanData implements Encode {
@Override
public Object decode(byte[] data) {
try {
ByteArrayInputStream is = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(is);
CachedBeanData read = new CachedBeanData();
read.readExternal(ois);
return read;
@@ -12,18 +12,14 @@ public class EncodeManyIdsData implements Encode {
@Override
public byte[] encode(Object value) {
try {
CachedManyIds data = (CachedManyIds) value;
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
data.writeExternal(oos);
oos.flush();
oos.close();
return os.toByteArray();
} catch (IOException e) {
throw new RuntimeException("Failed to decode cache data", e);
}
@@ -31,15 +27,12 @@ public class EncodeManyIdsData implements Encode {
@Override
public Object decode(byte[] data) {
try {
ByteArrayInputStream is = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(is);
CachedManyIds read = new CachedManyIds();
read.readExternal(ois);
return read;
} catch (ClassNotFoundException | IOException e) {
throw new RuntimeException("Failed to decode cache data", e);
}
@@ -18,10 +18,8 @@ public class EncodePrefixKey implements Encode {
if (!(value instanceof String) && !(value instanceof TenantAwareKey.CacheKey)) {
throw new IllegalStateException("Expecting String keys but got type:" + value.getClass());
}
String key = prefix + value.toString();
String key = prefix + value;
return key.getBytes(StandardCharsets.UTF_8);
} catch (Exception e) {
throw new RuntimeException("Failed to decode cache data", e);
}
@@ -10,15 +10,12 @@ public class EncodeSerializable implements Encode {
@Override
public byte[] encode(Object value) {
try {
ObjectOutputStream oos = new ObjectOutputStream(new ByteArrayOutputStream());
oos.writeObject(value);
oos.flush();
oos.close();
return new ByteArrayOutputStream().toByteArray();
} catch (IOException e) {
throw new RuntimeException("Failed to decode cache data", e);
}
@@ -23,7 +23,6 @@ public class DaemonTopicRunner {
private static final long reconnectWaitMillis = 1000;
private final JedisPool jedisPool;
private final DaemonTopic daemonTopic;
public DaemonTopicRunner(JedisPool jedisPool, DaemonTopic daemonTopic) {
@@ -37,11 +36,9 @@ public class DaemonTopicRunner {
}
private void attemptConnections() {
Timer reloadTimer = new Timer("redis-sub-notify");
ReloadNotifyTask notifyTask = null;
int attempts = 1;
while (true) {
if (notifyTask != null) {
// we didn't successfully re-connect to redis
@@ -72,11 +69,9 @@ public class DaemonTopicRunner {
jedis.echo("hi");
try {
daemonTopic.subscribe(jedis);
} catch (Exception e) {
log.error("Lost connection to topic, starting re-connection loop", e);
attemptConnections();
} finally {
try {
jedis.close();