Fixed - Multimap cluster compatibility. #1286

pull/1300/head
Nikita 7 years ago
parent def3ee883b
commit 07131b883a

@ -62,12 +62,13 @@ public class RedissonListMultimap<K, V> extends RedissonMultimap<K, V> implement
"local size = 0; " +
"for i, v in ipairs(keys) do " +
"if i % 2 == 0 then " +
"local name = '{' .. KEYS[1] .. '}:' .. v; " +
"local name = ARGV[1] .. v; " +
"size = size + redis.call('llen', name); " +
"end;" +
"end; " +
"return size; ",
Arrays.<Object>asList(getName()));
Arrays.<Object>asList(getName()),
prefix);
}
@Override
@ -87,7 +88,7 @@ public class RedissonListMultimap<K, V> extends RedissonMultimap<K, V> implement
"local keys = redis.call('hgetall', KEYS[1]); " +
"for i, v in ipairs(keys) do " +
"if i % 2 == 0 then " +
"local name = '{' .. KEYS[1] .. '}:' .. v; " +
"local name = ARGV[2] .. v; " +
"local items = redis.call('lrange', name, 0, -1) " +
"for i=1,#items do " +
@ -98,7 +99,8 @@ public class RedissonListMultimap<K, V> extends RedissonMultimap<K, V> implement
"end;" +
"end; " +
"return 0; ",
Arrays.<Object>asList(getName()), valueState);
Arrays.<Object>asList(getName()),
valueState, prefix);
}
@Override

@ -43,13 +43,13 @@ public class RedissonListMultimapCache<K, V> extends RedissonListMultimap<K, V>
RedissonListMultimapCache(UUID id, EvictionScheduler evictionScheduler, CommandAsyncExecutor connectionManager, String name) {
super(id, connectionManager, name);
evictionScheduler.scheduleCleanMultimap(name, getTimeoutSetName());
baseCache = new RedissonMultimapCache<K>(connectionManager, this, getTimeoutSetName());
baseCache = new RedissonMultimapCache<K>(connectionManager, this, getTimeoutSetName(), prefix);
}
RedissonListMultimapCache(UUID id, EvictionScheduler evictionScheduler, Codec codec, CommandAsyncExecutor connectionManager, String name) {
super(id, codec, connectionManager, name);
evictionScheduler.scheduleCleanMultimap(name, getTimeoutSetName());
baseCache = new RedissonMultimapCache<K>(connectionManager, this, getTimeoutSetName());
baseCache = new RedissonMultimapCache<K>(connectionManager, this, getTimeoutSetName(), prefix);
}
public RFuture<Boolean> containsKeyAsync(Object key) {
@ -76,7 +76,7 @@ public class RedissonListMultimapCache<K, V> extends RedissonListMultimap<K, V>
}
String getTimeoutSetName() {
return "redisson_list_multimap_ttl{" + getName() + "}";
return suffixName(getName(), "redisson_list_multimap_ttl");
}
@ -93,7 +93,7 @@ public class RedissonListMultimapCache<K, V> extends RedissonListMultimap<K, V>
+ "expireDate = tonumber(expireDateScore) "
+ "end; "
+ "if expireDate > tonumber(ARGV[2]) then " +
"local name = '{' .. KEYS[1] .. '}:' .. v; " +
"local name = ARGV[3] .. v; " +
"local items = redis.call('lrange', name, 0, -1) " +
"for i=1,#items do " +
@ -106,7 +106,8 @@ public class RedissonListMultimapCache<K, V> extends RedissonListMultimap<K, V>
"end;" +
"end; " +
"return 0; ",
Arrays.<Object>asList(getName(), getTimeoutSetName()), valueState, System.currentTimeMillis());
Arrays.<Object>asList(getName(), getTimeoutSetName()),
valueState, System.currentTimeMillis(), prefix);
}
public RFuture<Boolean> containsEntryAsync(Object key, Object value) {
@ -130,7 +131,8 @@ public class RedissonListMultimapCache<K, V> extends RedissonListMultimap<K, V>
"end; " +
"end; " +
"return 0; ",
Arrays.<Object>asList(valuesName, getTimeoutSetName()), System.currentTimeMillis(), keyState, valueState);
Arrays.<Object>asList(valuesName, getTimeoutSetName()),
System.currentTimeMillis(), keyState, valueState);
}
@Override

@ -15,7 +15,6 @@
*/
package org.redisson;
import java.net.InetSocketAddress;
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.ArrayList;
@ -58,15 +57,18 @@ import io.netty.buffer.ByteBuf;
public abstract class RedissonMultimap<K, V> extends RedissonExpirable implements RMultimap<K, V> {
private final UUID id;
final String prefix;
RedissonMultimap(UUID id, CommandAsyncExecutor connectionManager, String name) {
super(connectionManager, name);
this.id = id;
prefix = suffixName(getName(), "");
}
RedissonMultimap(UUID id, Codec codec, CommandAsyncExecutor connectionManager, String name) {
super(codec, connectionManager, name);
this.id = id;
prefix = suffixName(getName(), "");
}
@Override
@ -139,7 +141,7 @@ public abstract class RedissonMultimap<K, V> extends RedissonExpirable implement
}
String getValuesName(String hash) {
return "{" + getName() + "}:" + hash;
return suffixName(getName(), hash);
}
@Override
@ -239,7 +241,7 @@ public abstract class RedissonMultimap<K, V> extends RedissonExpirable implement
"local keys = {KEYS[1]}; " +
"for i, v in ipairs(entries) do " +
"if i % 2 == 0 then " +
"local name = '{' .. KEYS[1] .. '}:' .. v; " +
"local name = ARGV[1] .. v; " +
"table.insert(keys, name); " +
"end;" +
"end; " +
@ -249,7 +251,7 @@ public abstract class RedissonMultimap<K, V> extends RedissonExpirable implement
+ "n = n + redis.call('del', unpack(keys, i, math.min(i+4999, table.getn(keys)))) "
+ "end; "
+ "return n;",
Arrays.<Object>asList(getName()));
Arrays.<Object>asList(getName()), prefix);
}
@Override
@ -258,12 +260,13 @@ public abstract class RedissonMultimap<K, V> extends RedissonExpirable implement
"local entries = redis.call('hgetall', KEYS[1]); " +
"for i, v in ipairs(entries) do " +
"if i % 2 == 0 then " +
"local name = '{' .. KEYS[1] .. '}:' .. v; " +
"local name = ARGV[2] .. v; " +
"redis.call('pexpire', name, ARGV[1]); " +
"end;" +
"end; " +
"return redis.call('pexpire', KEYS[1], ARGV[1]); ",
Arrays.<Object>asList(getName()), timeUnit.toMillis(timeToLive));
Arrays.<Object>asList(getName()),
timeUnit.toMillis(timeToLive), prefix);
}
@Override
@ -272,12 +275,13 @@ public abstract class RedissonMultimap<K, V> extends RedissonExpirable implement
"local entries = redis.call('hgetall', KEYS[1]); " +
"for i, v in ipairs(entries) do " +
"if i % 2 == 0 then " +
"local name = '{' .. KEYS[1] .. '}:' .. v; " +
"local name = ARGV[2] .. v; " +
"redis.call('pexpireat', name, ARGV[1]); " +
"end;" +
"end; " +
"return redis.call('pexpireat', KEYS[1], ARGV[1]); ",
Arrays.<Object>asList(getName()), timestamp);
Arrays.<Object>asList(getName()),
timestamp, prefix);
}
@Override
@ -286,12 +290,13 @@ public abstract class RedissonMultimap<K, V> extends RedissonExpirable implement
"local entries = redis.call('hgetall', KEYS[1]); " +
"for i, v in ipairs(entries) do " +
"if i % 2 == 0 then " +
"local name = '{' .. KEYS[1] .. '}:' .. v; " +
"local name = ARGV[1] .. v; " +
"redis.call('persist', name); " +
"end;" +
"end; " +
"return redis.call('persist', KEYS[1]); ",
Arrays.<Object>asList(getName()));
Arrays.<Object>asList(getName()),
prefix);
}
@Override

@ -24,16 +24,24 @@ import org.redisson.client.codec.LongCodec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandAsyncExecutor;
/**
*
* @author Nikita Koksharov
*
* @param <K> key type
*/
public class RedissonMultimapCache<K> {
private final CommandAsyncExecutor commandExecutor;
private final RObject object;
private final String timeoutSetName;
private final String prefix;
public RedissonMultimapCache(CommandAsyncExecutor commandExecutor, RObject object, String timeoutSetName) {
public RedissonMultimapCache(CommandAsyncExecutor commandExecutor, RObject object, String timeoutSetName, String prefix) {
this.commandExecutor = commandExecutor;
this.object = object;
this.timeoutSetName = timeoutSetName;
this.prefix = prefix;
}
public RFuture<Boolean> expireKeyAsync(K key, long timeToLive, TimeUnit timeUnit) {
@ -60,7 +68,7 @@ public class RedissonMultimapCache<K> {
"local keys = {KEYS[1], KEYS[2]}; " +
"for i, v in ipairs(entries) do " +
"if i % 2 == 0 then " +
"local name = '{' .. KEYS[1] .. '}:' .. v; " +
"local name = ARGV[1] .. v; " +
"table.insert(keys, name); " +
"end;" +
"end; " +
@ -70,7 +78,8 @@ public class RedissonMultimapCache<K> {
+ "n = n + redis.call('del', unpack(keys, i, math.min(i+4999, table.getn(keys)))) "
+ "end; "
+ "return n;",
Arrays.<Object>asList(object.getName(), timeoutSetName));
Arrays.<Object>asList(object.getName(), timeoutSetName),
prefix);
}
public RFuture<Boolean> expireAsync(long timeToLive, TimeUnit timeUnit) {
@ -79,13 +88,14 @@ public class RedissonMultimapCache<K> {
"local entries = redis.call('hgetall', KEYS[1]); " +
"for i, v in ipairs(entries) do " +
"if i % 2 == 0 then " +
"local name = '{' .. KEYS[1] .. '}:' .. v; " +
"local name = ARGV[2] .. v; " +
"redis.call('pexpire', name, ARGV[1]); " +
"end;" +
"end; " +
"redis.call('pexpire', KEYS[2], ARGV[1]); " +
"return redis.call('pexpire', KEYS[1], ARGV[1]); ",
Arrays.<Object>asList(object.getName(), timeoutSetName), timeUnit.toMillis(timeToLive));
Arrays.<Object>asList(object.getName(), timeoutSetName),
timeUnit.toMillis(timeToLive), prefix);
}
public RFuture<Boolean> expireAtAsync(long timestamp) {
@ -94,13 +104,14 @@ public class RedissonMultimapCache<K> {
"local entries = redis.call('hgetall', KEYS[1]); " +
"for i, v in ipairs(entries) do " +
"if i % 2 == 0 then " +
"local name = '{' .. KEYS[1] .. '}:' .. v; " +
"local name = ARGV[2] .. v; " +
"redis.call('pexpireat', name, ARGV[1]); " +
"end;" +
"end; " +
"redis.call('pexpireat', KEYS[2], ARGV[1]); " +
"return redis.call('pexpireat', KEYS[1], ARGV[1]); ",
Arrays.<Object>asList(object.getName(), timeoutSetName), timestamp);
Arrays.<Object>asList(object.getName(), timeoutSetName),
timestamp, prefix);
}
public RFuture<Boolean> clearExpireAsync() {
@ -109,13 +120,14 @@ public class RedissonMultimapCache<K> {
"local entries = redis.call('hgetall', KEYS[1]); " +
"for i, v in ipairs(entries) do " +
"if i % 2 == 0 then " +
"local name = '{' .. KEYS[1] .. '}:' .. v; " +
"local name = ARGV[1] .. v; " +
"redis.call('persist', name); " +
"end;" +
"end; " +
"redis.call('persist', KEYS[2]); " +
"return redis.call('persist', KEYS[1]); ",
Arrays.<Object>asList(object.getName(), timeoutSetName));
Arrays.<Object>asList(object.getName(), timeoutSetName),
prefix);
}

@ -65,12 +65,13 @@ public class RedissonSetMultimap<K, V> extends RedissonMultimap<K, V> implements
"local size = 0; " +
"for i, v in ipairs(keys) do " +
"if i % 2 == 0 then " +
"local name = '{' .. KEYS[1] .. '}:' .. v; " +
"local name = ARGV[1] .. v; " +
"size = size + redis.call('scard', name); " +
"end;" +
"end; " +
"return size; ",
Arrays.<Object>asList(getName()));
Arrays.<Object>asList(getName()),
prefix);
}
@Override
@ -90,14 +91,15 @@ public class RedissonSetMultimap<K, V> extends RedissonMultimap<K, V> implements
"local keys = redis.call('hgetall', KEYS[1]); " +
"for i, v in ipairs(keys) do " +
"if i % 2 == 0 then " +
"local name = '{' .. KEYS[1] .. '}:' .. v; " +
"local name = ARGV[2] .. v; " +
"if redis.call('sismember', name, ARGV[1]) == 1 then "
+ "return 1; " +
"end;" +
"end;" +
"end; " +
"return 0; ",
Arrays.<Object>asList(getName()), valueState);
Arrays.<Object>asList(getName()),
valueState, prefix);
}
@Override

@ -43,13 +43,13 @@ public class RedissonSetMultimapCache<K, V> extends RedissonSetMultimap<K, V> im
RedissonSetMultimapCache(UUID id, EvictionScheduler evictionScheduler, CommandAsyncExecutor connectionManager, String name) {
super(id, connectionManager, name);
evictionScheduler.scheduleCleanMultimap(name, getTimeoutSetName());
baseCache = new RedissonMultimapCache<K>(connectionManager, this, getTimeoutSetName());
baseCache = new RedissonMultimapCache<K>(connectionManager, this, getTimeoutSetName(), prefix);
}
RedissonSetMultimapCache(UUID id, EvictionScheduler evictionScheduler, Codec codec, CommandAsyncExecutor connectionManager, String name) {
super(id, codec, connectionManager, name);
evictionScheduler.scheduleCleanMultimap(name, getTimeoutSetName());
baseCache = new RedissonMultimapCache<K>(connectionManager, this, getTimeoutSetName());
baseCache = new RedissonMultimapCache<K>(connectionManager, this, getTimeoutSetName(), prefix);
}
@Override
@ -73,11 +73,12 @@ public class RedissonSetMultimapCache<K, V> extends RedissonSetMultimap<K, V> im
+ "return redis.call('scard', ARGV[3]) > 0 and 1 or 0;" +
"end;" +
"return 0; ",
Arrays.<Object>asList(getName(), getTimeoutSetName()), System.currentTimeMillis(), keyState, setName);
Arrays.<Object>asList(getName(), getTimeoutSetName()),
System.currentTimeMillis(), keyState, setName);
}
String getTimeoutSetName() {
return "redisson_set_multimap_ttl{" + getName() + "}";
return suffixName(getName(), "redisson_set_multimap_ttl");
}
@ -95,7 +96,7 @@ public class RedissonSetMultimapCache<K, V> extends RedissonSetMultimap<K, V> im
+ "expireDate = tonumber(expireDateScore) "
+ "end; "
+ "if expireDate > tonumber(ARGV[2]) then " +
"local name = '{' .. KEYS[1] .. '}:' .. v; " +
"local name = ARGV[3] .. v; " +
"if redis.call('sismember', name, ARGV[1]) == 1 then "
+ "return 1; " +
"end;" +
@ -103,7 +104,8 @@ public class RedissonSetMultimapCache<K, V> extends RedissonSetMultimap<K, V> im
"end;" +
"end; " +
"return 0; ",
Arrays.<Object>asList(getName(), getTimeoutSetName()), valueState, System.currentTimeMillis());
Arrays.<Object>asList(getName(), getTimeoutSetName()),
valueState, System.currentTimeMillis(), prefix);
}
@Override

@ -0,0 +1,172 @@
package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.redisson.api.RMultimapCache;
public abstract class RedissonBaseMultimapCacheTest extends BaseTest {
abstract RMultimapCache<String, String> getMultimapCache(String name);
@Test
public void testContains() {
RMultimapCache<String, String> multimap = getMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
assertThat(multimap.containsKey("1")).isTrue();
assertThat(multimap.containsKey("2")).isFalse();
assertThat(multimap.containsValue("1")).isTrue();
assertThat(multimap.containsValue("3")).isTrue();
assertThat(multimap.containsValue("4")).isFalse();
assertThat(multimap.containsEntry("1", "1")).isTrue();
assertThat(multimap.containsEntry("1", "3")).isTrue();
assertThat(multimap.containsEntry("1", "4")).isFalse();
}
@Test
public void testContainsExpired() throws InterruptedException {
RMultimapCache<String, String> multimap = getMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
multimap.expireKey("1", 1, TimeUnit.SECONDS);
Thread.sleep(1000);
assertThat(multimap.containsKey("1")).isFalse();
assertThat(multimap.containsKey("2")).isFalse();
assertThat(multimap.containsValue("1")).isFalse();
assertThat(multimap.containsValue("3")).isFalse();
assertThat(multimap.containsValue("4")).isFalse();
assertThat(multimap.containsEntry("1", "1")).isFalse();
assertThat(multimap.containsEntry("1", "3")).isFalse();
assertThat(multimap.containsEntry("1", "4")).isFalse();
}
@Test
public void testGetAll() throws InterruptedException {
RMultimapCache<String, String> multimap = getMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
assertThat(multimap.getAll("1")).containsOnlyOnce("1", "2", "3");
}
@Test
public void testGetAllExpired() throws InterruptedException {
RMultimapCache<String, String> multimap = getMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
multimap.expireKey("1", 1, TimeUnit.SECONDS);
Thread.sleep(1000);
assertThat(multimap.getAll("1")).isEmpty();
}
@Test
public void testValuesExpired() throws InterruptedException {
RMultimapCache<String, String> multimap = getMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
multimap.expireKey("1", 1, TimeUnit.SECONDS);
Thread.sleep(1500);
assertThat(multimap.get("1").size()).isZero();
assertThat(multimap.get("1").isEmpty()).isTrue();
assertThat(multimap.get("1").remove("3")).isFalse();
assertThat(multimap.get("1").contains("3")).isFalse();
assertThat(multimap.get("1").retainAll(Arrays.asList("1"))).isFalse();
assertThat(multimap.get("1").containsAll(Arrays.asList("1"))).isFalse();
assertThat(multimap.get("1").removeAll(Arrays.asList("1"))).isFalse();
}
@Test
public void testScheduler() throws InterruptedException {
RMultimapCache<String, String> cache = getMultimapCache("simple33");
assertThat(cache.put("1", "1")).isTrue();
assertThat(cache.put("1", "2")).isTrue();
assertThat(cache.put("1", "3")).isTrue();
assertThat(cache.put("2", "1")).isTrue();
assertThat(cache.put("2", "2")).isTrue();
assertThat(cache.put("2", "3")).isTrue();
assertThat(cache.expireKey("1", 2, TimeUnit.SECONDS)).isTrue();
assertThat(cache.expireKey("2", 3, TimeUnit.SECONDS)).isTrue();
assertThat(cache.expireKey("3", 3, TimeUnit.SECONDS)).isFalse();
assertThat(cache.size()).isEqualTo(6);
Thread.sleep(10000);
assertThat(cache.size()).isZero();
}
@Test
public void testExpire() throws InterruptedException {
RMultimapCache<String, String> map = getMultimapCache("simple");
map.put("1", "2");
map.put("2", "3");
map.expire(100, TimeUnit.MILLISECONDS);
Thread.sleep(500);
assertThat(map.size()).isZero();
}
@Test
public void testExpireAt() throws InterruptedException {
RMultimapCache<String, String> map = getMultimapCache("simple");
map.put("1", "2");
map.put("2", "3");
map.expireAt(System.currentTimeMillis() + 100);
Thread.sleep(500);
assertThat(map.size()).isZero();
}
@Test
public void testClearExpire() throws InterruptedException {
RMultimapCache<String, String> map = getMultimapCache("simple");
map.put("1", "2");
map.put("2", "3");
map.expireAt(System.currentTimeMillis() + 100);
map.clearExpire();
Thread.sleep(500);
assertThat(map.size()).isEqualTo(2);
}
@Test
public void testDelete() {
RMultimapCache<String, String> map = getMultimapCache("simple");
map.put("1", "2");
map.put("2", "3");
assertThat(map.delete()).isTrue();
RMultimapCache<String, String> map2 = getMultimapCache("simple1");
assertThat(map2.delete()).isFalse();
}
}

@ -3,80 +3,20 @@ package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.redisson.api.RMultimapCache;
public class RedissonListMultimapCacheTest extends BaseTest {
public class RedissonListMultimapCacheTest extends RedissonBaseMultimapCacheTest {
@Test
public void testContains() {
RMultimapCache<String, String> multimap = redisson.getListMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
assertThat(multimap.containsKey("1")).isTrue();
assertThat(multimap.containsKey("2")).isFalse();
assertThat(multimap.containsValue("1")).isTrue();
assertThat(multimap.containsValue("3")).isTrue();
assertThat(multimap.containsValue("4")).isFalse();
assertThat(multimap.containsEntry("1", "1")).isTrue();
assertThat(multimap.containsEntry("1", "3")).isTrue();
assertThat(multimap.containsEntry("1", "4")).isFalse();
}
@Test
public void testContainsExpired() throws InterruptedException {
RMultimapCache<String, String> multimap = redisson.getListMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
multimap.expireKey("1", 1, TimeUnit.SECONDS);
Thread.sleep(1000);
assertThat(multimap.containsKey("1")).isFalse();
assertThat(multimap.containsKey("2")).isFalse();
assertThat(multimap.containsValue("1")).isFalse();
assertThat(multimap.containsValue("3")).isFalse();
assertThat(multimap.containsValue("4")).isFalse();
assertThat(multimap.containsEntry("1", "1")).isFalse();
assertThat(multimap.containsEntry("1", "3")).isFalse();
assertThat(multimap.containsEntry("1", "4")).isFalse();
}
@Test
public void testGetAll() throws InterruptedException {
RMultimapCache<String, String> multimap = redisson.getListMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
assertThat(multimap.getAll("1")).containsOnlyOnce("1", "2", "3");
}
@Test
public void testGetAllExpired() throws InterruptedException {
RMultimapCache<String, String> multimap = redisson.getListMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
multimap.expireKey("1", 1, TimeUnit.SECONDS);
Thread.sleep(1000);
assertThat(multimap.getAll("1")).isEmpty();
@Override
RMultimapCache<String, String> getMultimapCache(String name) {
return redisson.getListMultimapCache(name);
}
@Test
public void testValues() throws InterruptedException {
RMultimapCache<String, String> multimap = redisson.getListMultimapCache("test");
RMultimapCache<String, String> multimap = getMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
@ -86,6 +26,7 @@ public class RedissonListMultimapCacheTest extends BaseTest {
assertThat(multimap.get("1")).containsExactly("1", "2", "3", "3");
assertThat(multimap.get("1").remove("3")).isTrue();
assertThat(multimap.get("1").remove("3")).isTrue();
assertThat(multimap.get("1").remove("3")).isFalse();
assertThat(multimap.get("1").contains("3")).isFalse();
assertThat(multimap.get("1").contains("2")).isTrue();
assertThat(multimap.get("1").containsAll(Arrays.asList("1"))).isTrue();
@ -94,46 +35,4 @@ public class RedissonListMultimapCacheTest extends BaseTest {
assertThat(multimap.get("1").removeAll(Arrays.asList("1"))).isTrue();
}
@Test
public void testValuesExpired() throws InterruptedException {
RMultimapCache<String, String> multimap = redisson.getListMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
multimap.expireKey("1", 1, TimeUnit.SECONDS);
Thread.sleep(1000);
assertThat(multimap.get("1").size()).isZero();
assertThat(multimap.get("1").isEmpty()).isTrue();
assertThat(multimap.get("1").remove("3")).isFalse();
assertThat(multimap.get("1").contains("3")).isFalse();
assertThat(multimap.get("1").retainAll(Arrays.asList("1"))).isFalse();
assertThat(multimap.get("1").containsAll(Arrays.asList("1"))).isFalse();
assertThat(multimap.get("1").removeAll(Arrays.asList("1"))).isFalse();
}
@Test
public void testScheduler() throws InterruptedException {
RMultimapCache<String, String> cache = redisson.getListMultimapCache("simple33");
assertThat(cache.put("1", "1")).isTrue();
assertThat(cache.put("1", "2")).isTrue();
assertThat(cache.put("1", "3")).isTrue();
assertThat(cache.put("2", "1")).isTrue();
assertThat(cache.put("2", "2")).isTrue();
assertThat(cache.put("2", "3")).isTrue();
assertThat(cache.expireKey("1", 2, TimeUnit.SECONDS)).isTrue();
assertThat(cache.expireKey("2", 3, TimeUnit.SECONDS)).isTrue();
assertThat(cache.expireKey("3", 3, TimeUnit.SECONDS)).isFalse();
assertThat(cache.size()).isEqualTo(6);
Thread.sleep(10000);
assertThat(cache.size()).isZero();
}
}

@ -161,7 +161,7 @@ public class RedissonListMultimapTest extends BaseTest {
@Test
public void testPut() {
RListMultimap<SimpleKey, SimpleValue> map = redisson.getListMultimap("test1");
RListMultimap<SimpleKey, SimpleValue> map = redisson.getListMultimap("{multi.map}.some.key");
map.put(new SimpleKey("0"), new SimpleValue("1"));
map.put(new SimpleKey("0"), new SimpleValue("2"));
map.put(new SimpleKey("0"), new SimpleValue("3"));
@ -219,7 +219,7 @@ public class RedissonListMultimapTest extends BaseTest {
@Test
public void testContainsValue() {
RListMultimap<SimpleKey, SimpleValue> map = redisson.getListMultimap("test1");
RListMultimap<SimpleKey, SimpleValue> map = redisson.getListMultimap("{1}test1");
map.put(new SimpleKey("0"), new SimpleValue("1"));
assertThat(map.containsValue(new SimpleValue("1"))).isTrue();

@ -3,88 +3,27 @@ package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.redisson.api.RMultimapCache;
import org.redisson.api.RSetMultimap;
public class RedissonSetMultimapCacheTest extends BaseTest {
public class RedissonSetMultimapCacheTest extends RedissonBaseMultimapCacheTest {
@Test
public void testContains() {
RMultimapCache<String, String> multimap = redisson.getSetMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
assertThat(multimap.containsKey("1")).isTrue();
assertThat(multimap.containsKey("2")).isFalse();
assertThat(multimap.containsValue("1")).isTrue();
assertThat(multimap.containsValue("3")).isTrue();
assertThat(multimap.containsValue("4")).isFalse();
assertThat(multimap.containsEntry("1", "1")).isTrue();
assertThat(multimap.containsEntry("1", "3")).isTrue();
assertThat(multimap.containsEntry("1", "4")).isFalse();
}
@Test
public void testContainsExpired() throws InterruptedException {
RMultimapCache<String, String> multimap = redisson.getSetMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
multimap.expireKey("1", 1, TimeUnit.SECONDS);
Thread.sleep(1000);
assertThat(multimap.containsKey("1")).isFalse();
assertThat(multimap.containsKey("2")).isFalse();
assertThat(multimap.containsValue("1")).isFalse();
assertThat(multimap.containsValue("3")).isFalse();
assertThat(multimap.containsValue("4")).isFalse();
assertThat(multimap.containsEntry("1", "1")).isFalse();
assertThat(multimap.containsEntry("1", "3")).isFalse();
assertThat(multimap.containsEntry("1", "4")).isFalse();
}
@Test
public void testGetAll() throws InterruptedException {
RMultimapCache<String, String> multimap = redisson.getSetMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
assertThat(multimap.getAll("1")).containsOnlyOnce("1", "2", "3");
}
@Test
public void testGetAllExpired() throws InterruptedException {
RMultimapCache<String, String> multimap = redisson.getSetMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
multimap.expireKey("1", 1, TimeUnit.SECONDS);
Thread.sleep(1000);
assertThat(multimap.getAll("1")).isEmpty();
@Override
RMultimapCache<String, String> getMultimapCache(String name) {
return redisson.getSetMultimapCache(name);
}
@Test
public void testValues() throws InterruptedException {
RMultimapCache<String, String> multimap = redisson.getSetMultimapCache("test");
RMultimapCache<String, String> multimap = getMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
multimap.put("1", "3");
assertThat(multimap.get("1").size()).isEqualTo(3);
assertThat(multimap.get("1")).containsOnlyOnce("1", "2", "3");
assertThat(multimap.get("1")).containsExactlyInAnyOrder("1", "2", "3");
assertThat(multimap.get("1").remove("3")).isTrue();
assertThat(multimap.get("1").contains("3")).isFalse();
assertThat(multimap.get("1").contains("2")).isTrue();
@ -94,97 +33,4 @@ public class RedissonSetMultimapCacheTest extends BaseTest {
assertThat(multimap.get("1").removeAll(Arrays.asList("1"))).isTrue();
}
@Test
public void testValuesExpired() throws InterruptedException {
RMultimapCache<String, String> multimap = redisson.getSetMultimapCache("test");
multimap.put("1", "1");
multimap.put("1", "2");
multimap.put("1", "3");
multimap.expireKey("1", 1, TimeUnit.SECONDS);
Thread.sleep(1500);
assertThat(multimap.get("1").size()).isZero();
assertThat(multimap.get("1").isEmpty()).isTrue();
assertThat(multimap.get("1").remove("3")).isFalse();
assertThat(multimap.get("1").contains("3")).isFalse();
assertThat(multimap.get("1").retainAll(Arrays.asList("1"))).isFalse();
assertThat(multimap.get("1").containsAll(Arrays.asList("1"))).isFalse();
assertThat(multimap.get("1").removeAll(Arrays.asList("1"))).isFalse();
}
@Test
public void testScheduler() throws InterruptedException {
RMultimapCache<String, String> cache = redisson.getSetMultimapCache("simple33");
assertThat(cache.put("1", "1")).isTrue();
assertThat(cache.put("1", "2")).isTrue();
assertThat(cache.put("1", "3")).isTrue();
assertThat(cache.put("2", "1")).isTrue();
assertThat(cache.put("2", "2")).isTrue();
assertThat(cache.put("2", "3")).isTrue();
assertThat(cache.expireKey("1", 2, TimeUnit.SECONDS)).isTrue();
assertThat(cache.expireKey("2", 3, TimeUnit.SECONDS)).isTrue();
assertThat(cache.expireKey("3", 3, TimeUnit.SECONDS)).isFalse();
assertThat(cache.size()).isEqualTo(6);
Thread.sleep(10000);
assertThat(cache.size()).isZero();
}
@Test
public void testExpire() throws InterruptedException {
RSetMultimap<String, String> map = redisson.getSetMultimapCache("simple");
map.put("1", "2");
map.put("2", "3");
map.expire(100, TimeUnit.MILLISECONDS);
Thread.sleep(500);
assertThat(map.size()).isZero();
}
@Test
public void testExpireAt() throws InterruptedException {
RSetMultimap<String, String> map = redisson.getSetMultimapCache("simple");
map.put("1", "2");
map.put("2", "3");
map.expireAt(System.currentTimeMillis() + 100);
Thread.sleep(500);
assertThat(map.size()).isZero();
}
@Test
public void testClearExpire() throws InterruptedException {
RSetMultimap<String, String> map = redisson.getSetMultimapCache("simple");
map.put("1", "2");
map.put("2", "3");
map.expireAt(System.currentTimeMillis() + 100);
map.clearExpire();
Thread.sleep(500);
assertThat(map.size()).isEqualTo(2);
}
@Test
public void testDelete() {
RSetMultimap<String, String> map = redisson.getSetMultimapCache("simple");
map.put("1", "2");
map.put("2", "3");
assertThat(map.delete()).isTrue();
RSetMultimap<String, String> map2 = redisson.getSetMultimapCache("simple1");
assertThat(map2.delete()).isFalse();
}
}

Loading…
Cancel
Save