readAll and readAllAsync methods added to RSetCache

pull/395/head
Nikita 9 years ago
parent 8002797c9e
commit edb3bfaf35

@ -25,6 +25,7 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.redisson.client.codec.Codec;
@ -220,7 +221,33 @@ public class RedissonSetCache<V> extends RedissonExpirable implements RSetCache<
};
}
private Future<Collection<V>> readAllAsync() {
@Override
public Set<V> readAll() {
return get(readAllAsync());
}
@Override
public Future<Set<V>> readAllAsync() {
return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_SET,
"local expireHead = redis.call('zrange', KEYS[2], 0, 0, 'withscores');" +
"local keys = redis.call('hkeys', KEYS[1]);" +
"local maxDate = ARGV[1]; " +
"local minExpireDate = 92233720368547758;" +
"if #expireHead == 2 and tonumber(expireHead[2]) <= tonumber(maxDate) then " +
"for i = #keys, 1, -1 do " +
"local key = keys[i]; " +
"local expireDate = redis.call('zscore', KEYS[2], key); " +
"if expireDate ~= false and tonumber(expireDate) <= tonumber(maxDate) then " +
"minExpireDate = math.min(tonumber(expireDate), minExpireDate); " +
"table.remove(keys, i); " +
"end;" +
"end;" +
"end; " +
"return redis.call('hmget', KEYS[1], unpack(keys));",
Arrays.<Object>asList(getName(), getTimeoutSetName()), System.currentTimeMillis());
}
private Future<List<Object>> readAllasListAsync() {
return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_LIST,
"local expireHead = redis.call('zrange', KEYS[2], 0, 0, 'withscores');" +
"local keys = redis.call('hkeys', KEYS[1]);" +
@ -242,13 +269,13 @@ public class RedissonSetCache<V> extends RedissonExpirable implements RSetCache<
@Override
public Object[] toArray() {
List<Object> res = (List<Object>) get(readAllAsync());
List<Object> res = get(readAllasListAsync());
return res.toArray();
}
@Override
public <T> T[] toArray(T[] a) {
List<Object> res = (List<Object>) get(readAllAsync());
List<Object> res = get(readAllasListAsync());
return res.toArray(a);
}

@ -163,6 +163,7 @@ public interface RedisCommands {
RedisStrictCommand<Long> EVAL_LONG = new RedisStrictCommand<Long>("EVAL");
RedisStrictCommand<Void> EVAL_VOID = new RedisStrictCommand<Void>("EVAL", new VoidReplayConvertor());
RedisCommand<List<Object>> EVAL_LIST = new RedisCommand<List<Object>>("EVAL", new ObjectListReplayDecoder<Object>());
RedisCommand<Set<Object>> EVAL_SET = new RedisCommand<Set<Object>>("EVAL", new ObjectSetReplayDecoder());
RedisCommand<Object> EVAL_OBJECT = new RedisCommand<Object>("EVAL");
RedisCommand<Object> EVAL_MAP_VALUE = new RedisCommand<Object>("EVAL", ValueType.MAP_VALUE);
RedisCommand<List<Object>> EVAL_MAP_VALUE_LIST = new RedisCommand<List<Object>>("EVAL", new ObjectListReplayDecoder<Object>(), ValueType.MAP_VALUE);

@ -64,4 +64,11 @@ public interface RSetCache<V> extends Set<V>, RExpirable, RSetCacheAsync<V> {
@Override
int size();
/**
* Read all elements at once
*
* @return
*/
Set<V> readAll();
}

@ -15,6 +15,7 @@
*/
package org.redisson.core;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import io.netty.util.concurrent.Future;
@ -51,4 +52,11 @@ public interface RSetCacheAsync<V> extends RCollectionAsync<V> {
@Override
Future<Integer> sizeAsync();
/**
* Read all elements at once
*
* @return
*/
Future<Set<V>> readAllAsync();
}

@ -9,6 +9,7 @@ import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.*;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert;
@ -267,6 +268,17 @@ public class RedissonSetCacheTest extends BaseTest {
Assert.assertEquals(5, set.size());
}
@Test
public void testReadAll() {
RSetCache<Integer> set = redisson.getSetCache("set");
set.add(1, 2, TimeUnit.MINUTES);
set.add(2);
set.add(3);
set.add(4);
set.add(5);
assertThat(set.readAll()).containsOnly(1, 2, 3, 4, 5);
}
@Test
public void testRetainAllEmpty() {

Loading…
Cancel
Save