|
|
|
@ -8,6 +8,7 @@ import java.util.HashSet;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
|
|
import org.junit.Assert;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
@ -26,7 +27,38 @@ import mockit.Deencapsulation;
|
|
|
|
|
|
|
|
|
|
public class RedissonLocalCachedMapTest extends BaseMapTest {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public abstract class InvalidationTest {
|
|
|
|
|
|
|
|
|
|
RLocalCachedMap<String, Integer> map1;
|
|
|
|
|
RLocalCachedMap<String, Integer> map2;
|
|
|
|
|
Cache<CacheKey, CacheValue> cache1;
|
|
|
|
|
Cache<CacheKey, CacheValue> cache2;
|
|
|
|
|
|
|
|
|
|
public void execute() throws InterruptedException {
|
|
|
|
|
LocalCachedMapOptions<String, Integer> options = LocalCachedMapOptions.<String, Integer>defaults().evictionPolicy(EvictionPolicy.LFU).cacheSize(5);
|
|
|
|
|
map1 = redisson.getLocalCachedMap("test", options);
|
|
|
|
|
cache1 = Deencapsulation.getField(map1, "cache");
|
|
|
|
|
|
|
|
|
|
map2 = redisson.getLocalCachedMap("test", options);
|
|
|
|
|
cache2 = Deencapsulation.getField(map2, "cache");
|
|
|
|
|
|
|
|
|
|
map1.put("1", 1);
|
|
|
|
|
map1.put("2", 2);
|
|
|
|
|
|
|
|
|
|
assertThat(map2.get("1")).isEqualTo(1);
|
|
|
|
|
assertThat(map2.get("2")).isEqualTo(2);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1.size()).isEqualTo(2);
|
|
|
|
|
assertThat(cache2.size()).isEqualTo(2);
|
|
|
|
|
|
|
|
|
|
test();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract void test() throws InterruptedException;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @Test
|
|
|
|
|
public void testPerf() {
|
|
|
|
|
LocalCachedMapOptions options = LocalCachedMapOptions.defaults().evictionPolicy(EvictionPolicy.NONE).cacheSize(100000).invalidateEntryOnChange(true);
|
|
|
|
|
Map<String, Integer> map = redisson.getLocalCachedMap("test", options);
|
|
|
|
@ -102,68 +134,34 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testInvalidationOnClear() throws InterruptedException {
|
|
|
|
|
LocalCachedMapOptions options = LocalCachedMapOptions.defaults()
|
|
|
|
|
.evictionPolicy(EvictionPolicy.LFU)
|
|
|
|
|
.cacheSize(5);
|
|
|
|
|
|
|
|
|
|
RLocalCachedMap<String, Integer> map1 = redisson.getLocalCachedMap("test", options);
|
|
|
|
|
Cache<CacheKey, CacheValue> cache1 = Deencapsulation.getField(map1, "cache");
|
|
|
|
|
|
|
|
|
|
RLocalCachedMap<String, Integer> map2 = redisson.getLocalCachedMap("test", options);
|
|
|
|
|
Cache<CacheKey, CacheValue> cache2 = Deencapsulation.getField(map2, "cache");
|
|
|
|
|
|
|
|
|
|
map1.put("1", 1);
|
|
|
|
|
map1.put("2", 2);
|
|
|
|
|
map2.put("3", 2);
|
|
|
|
|
map2.put("4", 2);
|
|
|
|
|
|
|
|
|
|
assertThat(map1.size()).isEqualTo(4);
|
|
|
|
|
assertThat(map2.size()).isEqualTo(4);
|
|
|
|
|
|
|
|
|
|
assertThat(map1.readAllEntrySet()).hasSize(4);
|
|
|
|
|
assertThat(map2.readAllEntrySet()).hasSize(4);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1.size()).isEqualTo(4);
|
|
|
|
|
assertThat(cache2.size()).isEqualTo(4);
|
|
|
|
|
|
|
|
|
|
map1.clear();
|
|
|
|
|
|
|
|
|
|
Thread.sleep(50);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1.size()).isZero();
|
|
|
|
|
assertThat(cache2.size()).isZero();
|
|
|
|
|
|
|
|
|
|
assertThat(map1.size()).isZero();
|
|
|
|
|
assertThat(map2.size()).isZero();
|
|
|
|
|
new InvalidationTest() {
|
|
|
|
|
@Override
|
|
|
|
|
public void test() throws InterruptedException {
|
|
|
|
|
map1.clear();
|
|
|
|
|
|
|
|
|
|
Thread.sleep(50);
|
|
|
|
|
assertThat(cache1.size()).isZero();
|
|
|
|
|
assertThat(cache2.size()).isZero();
|
|
|
|
|
|
|
|
|
|
assertThat(map1.size()).isZero();
|
|
|
|
|
assertThat(map2.size()).isZero();
|
|
|
|
|
}
|
|
|
|
|
}.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testInvalidationOnUpdate() throws InterruptedException {
|
|
|
|
|
LocalCachedMapOptions<String, Integer> options = LocalCachedMapOptions.<String, Integer>defaults()
|
|
|
|
|
.evictionPolicy(EvictionPolicy.LFU)
|
|
|
|
|
.cacheSize(5);
|
|
|
|
|
|
|
|
|
|
RLocalCachedMap<String, Integer> map1 = redisson.getLocalCachedMap("test", options);
|
|
|
|
|
Cache<CacheKey, CacheValue> cache1 = Deencapsulation.getField(map1, "cache");
|
|
|
|
|
|
|
|
|
|
RLocalCachedMap<String, Integer> map2 = redisson.getLocalCachedMap("test", options);
|
|
|
|
|
Cache<CacheKey, CacheValue> cache2 = Deencapsulation.getField(map2, "cache");
|
|
|
|
|
|
|
|
|
|
map1.put("1", 1);
|
|
|
|
|
map1.put("2", 2);
|
|
|
|
|
|
|
|
|
|
assertThat(map2.get("1")).isEqualTo(1);
|
|
|
|
|
assertThat(map2.get("2")).isEqualTo(2);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1.size()).isEqualTo(2);
|
|
|
|
|
assertThat(cache2.size()).isEqualTo(2);
|
|
|
|
|
|
|
|
|
|
map1.put("1", 3);
|
|
|
|
|
map2.put("2", 4);
|
|
|
|
|
Thread.sleep(50);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1.size()).isEqualTo(1);
|
|
|
|
|
assertThat(cache2.size()).isEqualTo(1);
|
|
|
|
|
new InvalidationTest() {
|
|
|
|
|
@Override
|
|
|
|
|
public void test() throws InterruptedException {
|
|
|
|
|
map1.put("1", 3);
|
|
|
|
|
map2.put("2", 4);
|
|
|
|
|
Thread.sleep(50);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1.size()).isEqualTo(1);
|
|
|
|
|
assertThat(cache2.size()).isEqualTo(1);
|
|
|
|
|
}
|
|
|
|
|
}.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -191,7 +189,7 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
|
|
|
|
|
map1.put("1", 3);
|
|
|
|
|
map2.put("2", 4);
|
|
|
|
|
Thread.sleep(50);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(cache1.size()).isEqualTo(2);
|
|
|
|
|
assertThat(cache2.size()).isEqualTo(2);
|
|
|
|
|
}
|
|
|
|
@ -225,31 +223,20 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
|
|
|
|
|
assertThat(cache1.size()).isEqualTo(1);
|
|
|
|
|
assertThat(cache2.size()).isEqualTo(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testInvalidationOnRemove() throws InterruptedException {
|
|
|
|
|
LocalCachedMapOptions<String, Integer> options = LocalCachedMapOptions.<String, Integer>defaults().evictionPolicy(EvictionPolicy.LFU).cacheSize(5);
|
|
|
|
|
RLocalCachedMap<String, Integer> map1 = redisson.getLocalCachedMap("test", options);
|
|
|
|
|
Cache<CacheKey, CacheValue> cache1 = Deencapsulation.getField(map1, "cache");
|
|
|
|
|
|
|
|
|
|
RLocalCachedMap<String, Integer> map2 = redisson.getLocalCachedMap("test", options);
|
|
|
|
|
Cache<CacheKey, CacheValue> cache2 = Deencapsulation.getField(map2, "cache");
|
|
|
|
|
|
|
|
|
|
map1.put("1", 1);
|
|
|
|
|
map1.put("2", 2);
|
|
|
|
|
|
|
|
|
|
assertThat(map2.get("1")).isEqualTo(1);
|
|
|
|
|
assertThat(map2.get("2")).isEqualTo(2);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1.size()).isEqualTo(2);
|
|
|
|
|
assertThat(cache2.size()).isEqualTo(2);
|
|
|
|
|
|
|
|
|
|
map1.remove("1");
|
|
|
|
|
map2.remove("2");
|
|
|
|
|
Thread.sleep(50);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1.size()).isEqualTo(0);
|
|
|
|
|
assertThat(cache2.size()).isEqualTo(0);
|
|
|
|
|
new InvalidationTest() {
|
|
|
|
|
@Override
|
|
|
|
|
public void test() throws InterruptedException {
|
|
|
|
|
map1.remove("1");
|
|
|
|
|
map2.remove("2");
|
|
|
|
|
Thread.sleep(50);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1.size()).isEqualTo(0);
|
|
|
|
|
assertThat(cache2.size()).isEqualTo(0);
|
|
|
|
|
}
|
|
|
|
|
}.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -301,6 +288,21 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
|
|
|
|
|
assertThat(map.size()).isEqualTo(3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testInvalidationOnPut() throws InterruptedException {
|
|
|
|
|
new InvalidationTest() {
|
|
|
|
|
@Override
|
|
|
|
|
public void test() throws InterruptedException {
|
|
|
|
|
map1.put("1", 10);
|
|
|
|
|
map1.put("2", 20);
|
|
|
|
|
|
|
|
|
|
Thread.sleep(50);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1).hasSize(2);
|
|
|
|
|
assertThat(cache2).isEmpty();
|
|
|
|
|
}
|
|
|
|
|
}.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testPut() {
|
|
|
|
@ -347,6 +349,25 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
|
|
|
|
|
assertThat(filtered1).isEqualTo(expectedMap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testInvalidationOnPutAll() throws InterruptedException {
|
|
|
|
|
new InvalidationTest() {
|
|
|
|
|
@Override
|
|
|
|
|
public void test() throws InterruptedException {
|
|
|
|
|
Map<String, Integer> entries = new HashMap<>();
|
|
|
|
|
entries.put("1", 10);
|
|
|
|
|
entries.put("2", 20);
|
|
|
|
|
map1.putAll(entries);
|
|
|
|
|
|
|
|
|
|
Thread.sleep(50);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1).hasSize(2);
|
|
|
|
|
assertThat(cache2).isEmpty();
|
|
|
|
|
}
|
|
|
|
|
}.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testPutAll() throws InterruptedException {
|
|
|
|
|
Map<Integer, String> map = redisson.getLocalCachedMap("simple", LocalCachedMapOptions.defaults());
|
|
|
|
@ -458,6 +479,23 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
|
|
|
|
|
assertThat(cache.size()).isEqualTo(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testInvalidationOnRemoveValue() throws InterruptedException {
|
|
|
|
|
new InvalidationTest() {
|
|
|
|
|
@Override
|
|
|
|
|
public void test() throws InterruptedException {
|
|
|
|
|
map1.remove("1", 1);
|
|
|
|
|
map1.remove("2", 2);
|
|
|
|
|
|
|
|
|
|
Thread.sleep(50);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1).isEmpty();
|
|
|
|
|
assertThat(cache2).isEmpty();
|
|
|
|
|
}
|
|
|
|
|
}.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testRemoveValue() {
|
|
|
|
|
RMap<SimpleKey, SimpleValue> map = redisson.getLocalCachedMap("simple12", LocalCachedMapOptions.defaults());
|
|
|
|
@ -491,6 +529,23 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
|
|
|
|
|
assertThat(cache.size()).isEqualTo(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testInvalidationOnReplaceOldValue() throws InterruptedException {
|
|
|
|
|
new InvalidationTest() {
|
|
|
|
|
@Override
|
|
|
|
|
public void test() throws InterruptedException {
|
|
|
|
|
map1.replace("1", 1, 10);
|
|
|
|
|
map1.replace("2", 2, 20);
|
|
|
|
|
|
|
|
|
|
Thread.sleep(50);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1).hasSize(2);
|
|
|
|
|
assertThat(cache2).isEmpty();
|
|
|
|
|
}
|
|
|
|
|
}.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testReplaceOldValueFail() {
|
|
|
|
|
RMap<SimpleKey, SimpleValue> map = redisson.getLocalCachedMap("simple", LocalCachedMapOptions.defaults());
|
|
|
|
@ -522,6 +577,22 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
|
|
|
|
|
assertThat(cache.size()).isEqualTo(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testInvalidationOnReplaceValue() throws InterruptedException {
|
|
|
|
|
new InvalidationTest() {
|
|
|
|
|
@Override
|
|
|
|
|
public void test() throws InterruptedException {
|
|
|
|
|
map1.replace("1", 10);
|
|
|
|
|
map1.replace("2", 20);
|
|
|
|
|
|
|
|
|
|
Thread.sleep(50);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1).hasSize(2);
|
|
|
|
|
assertThat(cache2).isEmpty();
|
|
|
|
|
}
|
|
|
|
|
}.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testReplaceValue() {
|
|
|
|
|
RMap<SimpleKey, SimpleValue> map = redisson.getLocalCachedMap("simple", LocalCachedMapOptions.defaults());
|
|
|
|
@ -556,17 +627,18 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testFastRemoveAsync() throws InterruptedException, ExecutionException {
|
|
|
|
|
RMap<Integer, Integer> map = redisson.getLocalCachedMap("simple", LocalCachedMapOptions.defaults());
|
|
|
|
|
Cache<CacheKey, CacheValue> cache = Deencapsulation.getField(map, "cache");
|
|
|
|
|
map.put(1, 3);
|
|
|
|
|
map.put(3, 5);
|
|
|
|
|
map.put(4, 6);
|
|
|
|
|
map.put(7, 8);
|
|
|
|
|
|
|
|
|
|
assertThat(map.fastRemoveAsync(1, 3, 7).get()).isEqualTo(3);
|
|
|
|
|
assertThat(cache.size()).isEqualTo(1);
|
|
|
|
|
assertThat(map.size()).isEqualTo(1);
|
|
|
|
|
public void testInvalidationOnFastRemove() throws InterruptedException {
|
|
|
|
|
new InvalidationTest() {
|
|
|
|
|
@Override
|
|
|
|
|
public void test() throws InterruptedException {
|
|
|
|
|
map1.fastRemove("1", "2", "3");
|
|
|
|
|
|
|
|
|
|
Thread.sleep(50);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1).isEmpty();
|
|
|
|
|
assertThat(cache2).isEmpty();
|
|
|
|
|
}
|
|
|
|
|
}.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -606,6 +678,22 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
|
|
|
|
|
assertThat(map.fastRemove("test")).isZero();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testInvalidationOnFastPut() throws InterruptedException {
|
|
|
|
|
new InvalidationTest() {
|
|
|
|
|
@Override
|
|
|
|
|
public void test() throws InterruptedException {
|
|
|
|
|
map1.fastPut("1", 10);
|
|
|
|
|
map1.fastPut("2", 20);
|
|
|
|
|
|
|
|
|
|
Thread.sleep(50);
|
|
|
|
|
|
|
|
|
|
assertThat(cache1).hasSize(2);
|
|
|
|
|
assertThat(cache2).isEmpty();
|
|
|
|
|
}
|
|
|
|
|
}.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testFastPut() {
|
|
|
|
|