From d58f3031894ae7c64f03134f230e9ef135b0f971 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Sun, 12 Jul 2020 14:39:02 +0300 Subject: [PATCH] refactoring --- .../org/redisson/RedissonLocalCachedMap.java | 95 +++++++++---------- .../org/redisson/cache/LocalCacheView.java | 2 +- .../transaction/RedissonTransaction.java | 4 +- .../redisson/RedissonLocalCachedMapTest.java | 1 + 4 files changed, 47 insertions(+), 55 deletions(-) diff --git a/redisson/src/main/java/org/redisson/RedissonLocalCachedMap.java b/redisson/src/main/java/org/redisson/RedissonLocalCachedMap.java index 86d2b9914..298d5bd72 100644 --- a/redisson/src/main/java/org/redisson/RedissonLocalCachedMap.java +++ b/redisson/src/main/java/org/redisson/RedissonLocalCachedMap.java @@ -86,7 +86,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R @Override protected void updateCache(ByteBuf keyBuf, ByteBuf valueBuf) throws IOException { - CacheKey cacheKey = toCacheKey(keyBuf); + CacheKey cacheKey = localCacheView.toCacheKey(keyBuf); Object key = codec.getMapKeyDecoder().decode(keyBuf, null); Object value = codec.getMapValueDecoder().decode(valueBuf, null); cachePut(cacheKey, key, value); @@ -107,6 +107,10 @@ public class RedissonLocalCachedMap extends RedissonMap implements R } } + public LocalCacheView getLocalCacheView() { + return localCacheView; + } + private void broadcastLocalCacheStore(V value, ByteBuf mapKey, CacheKey cacheKey) { if (storeMode != LocalCachedMapOptions.StoreMode.LOCALCACHE) { return; @@ -165,19 +169,6 @@ public class RedissonLocalCachedMap extends RedissonMap implements R return cache.remove(cacheKey, new CacheValue(key, value)); } - public CacheKey toCacheKey(Object key) { - ByteBuf encoded = encodeMapKey(key); - try { - return toCacheKey(encoded); - } finally { - encoded.release(); - } - } - - private CacheKey toCacheKey(ByteBuf encodedKey) { - return new CacheKey(Hash.hash128toArray(encodedKey)); - } - @Override public RFuture sizeAsync() { if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) { @@ -190,7 +181,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R public RFuture containsKeyAsync(Object key) { checkKey(key); - CacheKey cacheKey = toCacheKey(key); + CacheKey cacheKey = localCacheView.toCacheKey(key); if (!cache.containsKey(cacheKey)) { if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) { return RedissonPromise.newSucceededFuture(false); @@ -220,7 +211,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R public RFuture getAsync(Object key) { checkKey(key); - CacheKey cacheKey = toCacheKey(key); + CacheKey cacheKey = localCacheView.toCacheKey(key); CacheValue cacheValue = cache.get(cacheKey); if (cacheValue != null && cacheValue.getValue() != null) { return RedissonPromise.newSucceededFuture((V) cacheValue.getValue()); @@ -258,7 +249,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R @Override protected RFuture putOperationAsync(K key, V value) { ByteBuf mapKey = encodeMapKey(key); - CacheKey cacheKey = toCacheKey(mapKey); + CacheKey cacheKey = localCacheView.toCacheKey(mapKey); CacheValue prevValue = cachePut(cacheKey, key, value); broadcastLocalCacheStore(value, mapKey, cacheKey); @@ -298,7 +289,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R @Override protected RFuture fastPutOperationAsync(K key, V value) { ByteBuf encodedKey = encodeMapKey(key); - CacheKey cacheKey = toCacheKey(encodedKey); + CacheKey cacheKey = localCacheView.toCacheKey(encodedKey); CacheValue prevValue = cachePut(cacheKey, key, value); broadcastLocalCacheStore(value, encodedKey, cacheKey); @@ -335,7 +326,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R @Override protected RFuture removeOperationAsync(K key) { ByteBuf keyEncoded = encodeMapKey(key); - CacheKey cacheKey = toCacheKey(keyEncoded); + CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded); CacheValue value = cache.remove(cacheKey); if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) { @@ -381,7 +372,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R ByteBuf keyEncoded = encodeMapKey(k); params.add(keyEncoded); - CacheKey cacheKey = toCacheKey(keyEncoded); + CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded); cache.remove(cacheKey); ByteBuf msgEncoded = encode(new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash())); params.add(msgEncoded); @@ -408,7 +399,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R ByteBuf keyEncoded = encodeMapKey(k); params.add(keyEncoded); - CacheKey cacheKey = toCacheKey(keyEncoded); + CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded); cache.remove(cacheKey); ByteBuf msgEncoded = encode(new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash())); params.add(msgEncoded); @@ -437,7 +428,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R ByteBuf keyEncoded = encodeMapKey(k); params.add(keyEncoded); - CacheKey cacheKey = toCacheKey(keyEncoded); + CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded); cache.remove(cacheKey); } @@ -458,7 +449,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) { long count = 0; for (K k : keys) { - CacheKey cacheKey = toCacheKey(k); + CacheKey cacheKey = localCacheView.toCacheKey(k); CacheValue val = cache.remove(cacheKey); if (val != null) { count++; @@ -475,7 +466,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R ByteBuf keyEncoded = encodeMapKey(k); params.add(keyEncoded); - CacheKey cacheKey = toCacheKey(keyEncoded); + CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded); cache.remove(cacheKey); ByteBuf msgEncoded = encode(new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash())); params.add(msgEncoded); @@ -501,7 +492,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R ByteBuf keyEncoded = encodeMapKey(k); params.add(keyEncoded); - CacheKey cacheKey = toCacheKey(keyEncoded); + CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded); cache.remove(cacheKey); ByteBuf msgEncoded = encode(new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash())); params.add(msgEncoded); @@ -530,7 +521,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R ByteBuf keyEncoded = encodeMapKey(k); params.add(keyEncoded); - CacheKey cacheKey = toCacheKey(keyEncoded); + CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded); cache.remove(cacheKey); } @@ -567,7 +558,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R Set mapKeys = new HashSet(keys); for (Iterator iterator = mapKeys.iterator(); iterator.hasNext();) { K key = iterator.next(); - CacheKey cacheKey = toCacheKey(key); + CacheKey cacheKey = localCacheView.toCacheKey(key); CacheValue value = cache.get(cacheKey); if (value != null) { result.put(key, (V) value.getValue()); @@ -598,7 +589,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R private void cacheMap(Map map) { for (java.util.Map.Entry entry : map.entrySet()) { - CacheKey cacheKey = toCacheKey(entry.getKey()); + CacheKey cacheKey = localCacheView.toCacheKey(entry.getKey()); cachePut(cacheKey, entry.getKey(), entry.getValue()); } } @@ -608,7 +599,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) { for (Entry entry : map.entrySet()) { ByteBuf keyEncoded = encodeMapKey(entry.getKey()); - CacheKey cacheKey = toCacheKey(keyEncoded); + CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded); cachePut(cacheKey, entry.getKey(), entry.getValue()); broadcastLocalCacheStore(entry.getValue(), keyEncoded, cacheKey); @@ -627,7 +618,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R ByteBuf mapValue = encodeMapValue(t.getValue()); params.add(mapKey); params.add(mapValue); - CacheKey cacheKey = toCacheKey(mapKey); + CacheKey cacheKey = localCacheView.toCacheKey(mapKey); hashes[i] = cacheKey.getKeyHash(); i++; } @@ -691,7 +682,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R @Override protected RFuture addAndGetOperationAsync(K key, Number value) { ByteBuf keyState = encodeMapKey(key); - CacheKey cacheKey = toCacheKey(keyState); + CacheKey cacheKey = localCacheView.toCacheKey(keyState); ByteBuf msg = encode(new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash())); byte[] entryId = generateLogEntryId(cacheKey.getKeyHash()); RFuture future = commandExecutor.evalWriteAsync(getName(), StringCodec.INSTANCE, new RedisCommand("EVAL", new NumberConvertor(value.getClass())), @@ -709,7 +700,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R future.onComplete((res, e) -> { if (res != null) { - CacheKey cKey = toCacheKey(key); + CacheKey cKey = localCacheView.toCacheKey(key); cachePut(cKey, key, res); } }); @@ -720,7 +711,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R public RFuture fastPutIfAbsentAsync(K key, V value) { if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) { ByteBuf mapKey = encodeMapKey(key); - CacheKey cacheKey = toCacheKey(mapKey); + CacheKey cacheKey = localCacheView.toCacheKey(mapKey); CacheValue prevValue = cachePutIfAbsent(cacheKey, key, value); if (prevValue == null) { broadcastLocalCacheStore(value, mapKey, cacheKey); @@ -738,7 +729,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R } if (res) { - CacheKey cacheKey = toCacheKey(key); + CacheKey cacheKey = localCacheView.toCacheKey(key); cachePut(cacheKey, key, value); } }); @@ -822,7 +813,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R } for (java.util.Map.Entry entry : res.entrySet()) { - CacheKey cacheKey = toCacheKey(entry.getKey()); + CacheKey cacheKey = localCacheView.toCacheKey(entry.getKey()); cachePut(cacheKey, entry.getKey(), entry.getValue()); } result.putAll(res); @@ -835,7 +826,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R @Override public void preloadCache() { for (Entry entry : super.entrySet()) { - CacheKey cacheKey = toCacheKey(entry.getKey()); + CacheKey cacheKey = localCacheView.toCacheKey(entry.getKey()); cachePut(cacheKey, entry.getKey(), entry.getValue()); } } @@ -843,7 +834,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R @Override public void preloadCache(int count) { for (Entry entry : super.entrySet(count)) { - CacheKey cacheKey = toCacheKey(entry.getKey()); + CacheKey cacheKey = localCacheView.toCacheKey(entry.getKey()); cachePut(cacheKey, entry.getKey(), entry.getValue()); } } @@ -885,7 +876,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R } for (java.util.Map.Entry entry : res) { - CacheKey cacheKey = toCacheKey(entry.getKey()); + CacheKey cacheKey = localCacheView.toCacheKey(entry.getKey()); cachePut(cacheKey, entry.getKey(), entry.getValue()); } result.addAll(res); @@ -922,7 +913,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R public RFuture fastReplaceAsync(K key, V value) { if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) { ByteBuf mapKey = encodeMapKey(key); - CacheKey cacheKey = toCacheKey(mapKey); + CacheKey cacheKey = localCacheView.toCacheKey(mapKey); CacheValue prevValue = cacheReplace(cacheKey, key, value); if (prevValue != null) { broadcastLocalCacheStore(value, mapKey, cacheKey); @@ -940,7 +931,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R } if (res) { - CacheKey cacheKey = toCacheKey(key); + CacheKey cacheKey = localCacheView.toCacheKey(key); cachePut(cacheKey, key, value); } }); @@ -952,7 +943,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R protected RFuture fastReplaceOperationAsync(K key, V value) { ByteBuf keyState = encodeMapKey(key); ByteBuf valueState = encodeMapValue(value); - CacheKey cacheKey = toCacheKey(keyState); + CacheKey cacheKey = localCacheView.toCacheKey(keyState); byte[] entryId = generateLogEntryId(cacheKey.getKeyHash()); ByteBuf msg = createSyncMessage(keyState, valueState, cacheKey); return commandExecutor.evalWriteAsync(getName(key), codec, RedisCommands.EVAL_BOOLEAN, @@ -979,7 +970,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R protected RFuture replaceOperationAsync(K key, V value) { ByteBuf keyState = encodeMapKey(key); ByteBuf valueState = encodeMapValue(value); - CacheKey cacheKey = toCacheKey(keyState); + CacheKey cacheKey = localCacheView.toCacheKey(keyState); byte[] entryId = generateLogEntryId(cacheKey.getKeyHash()); ByteBuf msg = createSyncMessage(keyState, valueState, cacheKey); return commandExecutor.evalWriteAsync(getName(key), codec, RedisCommands.EVAL_MAP_VALUE, @@ -1007,7 +998,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R public RFuture replaceAsync(K key, V value) { if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) { ByteBuf mapKey = encodeMapKey(key); - CacheKey cacheKey = toCacheKey(mapKey); + CacheKey cacheKey = localCacheView.toCacheKey(mapKey); CacheValue prevValue = cacheReplace(cacheKey, key, value); if (prevValue != null) { broadcastLocalCacheStore(value, mapKey, cacheKey); @@ -1025,7 +1016,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R } if (res != null) { - CacheKey cacheKey = toCacheKey(key); + CacheKey cacheKey = localCacheView.toCacheKey(key); cachePut(cacheKey, key, value); } }); @@ -1038,7 +1029,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R ByteBuf keyState = encodeMapKey(key); ByteBuf oldValueState = encodeMapValue(oldValue); ByteBuf newValueState = encodeMapValue(newValue); - CacheKey cacheKey = toCacheKey(keyState); + CacheKey cacheKey = localCacheView.toCacheKey(keyState); byte[] entryId = generateLogEntryId(cacheKey.getKeyHash()); ByteBuf msg = createSyncMessage(keyState, newValueState, cacheKey); return commandExecutor.evalWriteAsync(getName(key), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, @@ -1063,7 +1054,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R public RFuture replaceAsync(K key, V oldValue, V newValue) { if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) { ByteBuf mapKey = encodeMapKey(key); - CacheKey cacheKey = toCacheKey(mapKey); + CacheKey cacheKey = localCacheView.toCacheKey(mapKey); if (cacheReplace(cacheKey, key, oldValue, newValue)) { broadcastLocalCacheStore(newValue, mapKey, cacheKey); return RedissonPromise.newSucceededFuture(true); @@ -1080,7 +1071,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R } if (res) { - CacheKey cacheKey = toCacheKey(key); + CacheKey cacheKey = localCacheView.toCacheKey(key); cachePut(cacheKey, key, newValue); } }); @@ -1092,7 +1083,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R protected RFuture removeOperationAsync(Object key, Object value) { ByteBuf keyState = encodeMapKey(key); ByteBuf valueState = encodeMapValue(value); - CacheKey cacheKey = toCacheKey(keyState); + CacheKey cacheKey = localCacheView.toCacheKey(keyState); byte[] entryId = generateLogEntryId(cacheKey.getKeyHash()); ByteBuf msg = encode(new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash())); @@ -1117,7 +1108,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R public RFuture removeAsync(Object key, Object value) { if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) { ByteBuf mapKey = encodeMapKey(key); - CacheKey cacheKey = toCacheKey(mapKey); + CacheKey cacheKey = localCacheView.toCacheKey(mapKey); if (cacheRemove(cacheKey, key, value)) { broadcastLocalCacheStore((V) value, mapKey, cacheKey); return RedissonPromise.newSucceededFuture(true); @@ -1135,7 +1126,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R } if (res) { - CacheKey cacheKey = toCacheKey(key); + CacheKey cacheKey = localCacheView.toCacheKey(key); cache.remove(cacheKey); } }); @@ -1147,7 +1138,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R public RFuture putIfAbsentAsync(K key, V value) { if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) { ByteBuf mapKey = encodeMapKey(key); - CacheKey cacheKey = toCacheKey(mapKey); + CacheKey cacheKey = localCacheView.toCacheKey(mapKey); CacheValue prevValue = cachePutIfAbsent(cacheKey, key, value); if (prevValue == null) { broadcastLocalCacheStore((V) value, mapKey, cacheKey); @@ -1165,7 +1156,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R } if (res == null) { - CacheKey cacheKey = toCacheKey(key); + CacheKey cacheKey = localCacheView.toCacheKey(key); cachePut(cacheKey, key, value); } }); diff --git a/redisson/src/main/java/org/redisson/cache/LocalCacheView.java b/redisson/src/main/java/org/redisson/cache/LocalCacheView.java index 8807f9b5f..ffac05b7b 100644 --- a/redisson/src/main/java/org/redisson/cache/LocalCacheView.java +++ b/redisson/src/main/java/org/redisson/cache/LocalCacheView.java @@ -252,7 +252,7 @@ public class LocalCacheView { } } - private CacheKey toCacheKey(ByteBuf encodedKey) { + public CacheKey toCacheKey(ByteBuf encodedKey) { return new CacheKey(Hash.hash128toArray(encodedKey)); } diff --git a/redisson/src/main/java/org/redisson/transaction/RedissonTransaction.java b/redisson/src/main/java/org/redisson/transaction/RedissonTransaction.java index 8641b0f1b..15ea0a15f 100644 --- a/redisson/src/main/java/org/redisson/transaction/RedissonTransaction.java +++ b/redisson/src/main/java/org/redisson/transaction/RedissonTransaction.java @@ -348,7 +348,7 @@ public class RedissonTransaction implements RTransaction { RedissonLocalCachedMap map = (RedissonLocalCachedMap) mapOperation.getMap(); HashKey hashKey = new HashKey(transactionalOperation.getName(), transactionalOperation.getCodec()); - byte[] key = map.toCacheKey(mapOperation.getKey()).getKeyHash(); + byte[] key = map.getLocalCacheView().toCacheKey(mapOperation.getKey()).getKeyHash(); HashValue value = hashes.get(hashKey); if (value == null) { value = new HashValue(); @@ -442,7 +442,7 @@ public class RedissonTransaction implements RTransaction { RedissonLocalCachedMap map = (RedissonLocalCachedMap) mapOperation.getMap(); HashKey hashKey = new HashKey(transactionalOperation.getName(), transactionalOperation.getCodec()); - byte[] key = map.toCacheKey(mapOperation.getKey()).getKeyHash(); + byte[] key = map.getLocalCacheView().toCacheKey(mapOperation.getKey()).getKeyHash(); HashValue value = hashes.get(hashKey); if (value == null) { value = new HashValue(); diff --git a/redisson/src/test/java/org/redisson/RedissonLocalCachedMapTest.java b/redisson/src/test/java/org/redisson/RedissonLocalCachedMapTest.java index 26eeddf87..fce4ef9d7 100644 --- a/redisson/src/test/java/org/redisson/RedissonLocalCachedMapTest.java +++ b/redisson/src/test/java/org/redisson/RedissonLocalCachedMapTest.java @@ -582,6 +582,7 @@ public class RedissonLocalCachedMapTest extends BaseMapTest { SimpleKey key1 = new SimpleKey("2"); SimpleValue value1 = new SimpleValue("4"); assertThat(map.fastPutIfAbsent(key1, value1)).isTrue(); + Thread.sleep(50); assertThat(cache.size()).isEqualTo(2); assertThat(map.get(key1)).isEqualTo(value1); }