Feature - RObject.getSizeInMemory method added #1726

pull/1730/head
Nikita Koksharov 6 years ago
parent fbda0da62d
commit 7143e8593c

@ -220,6 +220,12 @@ public class RedissonBloomFilter<T> extends RedissonExpirable implements RBloomF
return commandExecutor.writeAsync(getName(), RedisCommands.DEL_OBJECTS, getName(), configName);
}
@Override
public RFuture<Long> sizeInMemoryAsync() {
List<Object> keys = Arrays.<Object>asList(getName(), configName);
return super.sizeInMemoryAsync(keys);
}
private void readConfig() {
RFuture<Map<String, String>> future = commandExecutor.readAsync(configName, StringCodec.INSTANCE,
new RedisCommand<Map<Object, Object>>("HGETALL", new ObjectMapReplayDecoder()), configName);

@ -370,6 +370,12 @@ public class RedissonBoundedBlockingQueue<V> extends RedissonQueue<V> implements
public RFuture<Boolean> deleteAsync() {
return commandExecutor.writeAsync(getName(), RedisCommands.DEL_OBJECTS, getName(), getSemaphoreName());
}
@Override
public RFuture<Long> sizeInMemoryAsync() {
List<Object> keys = Arrays.<Object>asList(getName(), getSemaphoreName());
return super.sizeInMemoryAsync(keys);
}
@Override
public RFuture<Boolean> expireAsync(long timeToLive, TimeUnit timeUnit) {

@ -401,6 +401,12 @@ public class RedissonDelayedQueue<V> extends RedissonExpirable implements RDelay
return commandExecutor.writeAsync(getName(), RedisCommands.DEL_OBJECTS, queueName, timeoutSetName);
}
@Override
public RFuture<Long> sizeInMemoryAsync() {
List<Object> keys = Arrays.<Object>asList(queueName, timeoutSetName);
return super.sizeInMemoryAsync(keys);
}
@Override
public RFuture<Boolean> expireAsync(long timeToLive, TimeUnit timeUnit) {
return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,

@ -16,6 +16,7 @@
package org.redisson;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
@ -230,6 +231,12 @@ public class RedissonFairLock extends RedissonLock implements RLock {
return commandExecutor.writeAsync(getName(), RedisCommands.DEL_OBJECTS, getName(), threadsQueueName, timeoutSetName);
}
@Override
public RFuture<Long> sizeInMemoryAsync() {
List<Object> keys = Arrays.<Object>asList(getName(), threadsQueueName, timeoutSetName);
return super.sizeInMemoryAsync(keys);
}
@Override
public RFuture<Boolean> expireAsync(long timeToLive, TimeUnit timeUnit) {
return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,

@ -192,6 +192,11 @@ public class RedissonListMultimapCache<K, V> extends RedissonListMultimap<K, V>
public RFuture<Boolean> expireKeyAsync(K key, long timeToLive, TimeUnit timeUnit) {
return baseCache.expireKeyAsync(key, timeToLive, timeUnit);
}
@Override
public RFuture<Long> sizeInMemoryAsync() {
return baseCache.sizeInMemoryAsync();
}
@Override
public RFuture<Boolean> deleteAsync() {

@ -92,6 +92,12 @@ public class RedissonListMultimapValues<V> extends RedissonExpirable implements
throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set");
}
@Override
public RFuture<Long> sizeInMemoryAsync() {
List<Object> keys = Arrays.<Object>asList(getName(), timeoutSetName);
return super.sizeInMemoryAsync(keys);
}
public RFuture<Boolean> deleteAsync() {
return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,
"local expireDate = 92233720368547758; " +

@ -520,6 +520,11 @@ public class RedissonLocalCachedMap<K, V> extends RedissonMap<K, V> implements R
return commandExecutor.writeAsync(getName(), codec, RedisCommands.HDEL, params.toArray());
}
@Override
public RFuture<Long> sizeInMemoryAsync() {
List<Object> keys = Arrays.<Object>asList(getName(), listener.getUpdatesLogName());
return super.sizeInMemoryAsync(keys);
}
@Override
public RFuture<Boolean> deleteAsync() {

@ -1975,6 +1975,12 @@ public class RedissonMapCache<K, V> extends RedissonMap<K, V> implements RMapCac
expiredTopic.removeListener(listenerId);
}
@Override
public RFuture<Long> sizeInMemoryAsync() {
List<Object> keys = Arrays.<Object>asList(getName(), getTimeoutSetName(), getIdleSetName(), getLastAccessTimeSetName(), getOptionsName());
return super.sizeInMemoryAsync(keys);
}
@Override
public RFuture<Boolean> deleteAsync() {
return commandExecutor.writeAsync(getName(), RedisCommands.DEL_OBJECTS,

@ -16,6 +16,7 @@
package org.redisson;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.redisson.api.RFuture;
@ -62,6 +63,11 @@ public class RedissonMultimapCache<K> {
ttlTimeout, ((RedissonObject)object).encodeMapKey(key));
}
public RFuture<Long> sizeInMemoryAsync() {
List<Object> keys = Arrays.<Object>asList(object.getName(), timeoutSetName);
return ((RedissonObject)object).sizeInMemoryAsync(keys);
}
public RFuture<Boolean> deleteAsync() {
return commandExecutor.evalWriteAsync(object.getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN_AMOUNT,
"local entries = redis.call('hgetall', KEYS[1]); " +

@ -18,6 +18,7 @@ package org.redisson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ -97,6 +98,29 @@ public abstract class RedissonObject implements RObject {
public void rename(String newName) {
get(renameAsync(newName));
}
@Override
public RFuture<Long> sizeInMemoryAsync() {
return commandExecutor.writeAsync(getName(), RedisCommands.MEMORY_USAGE, getName());
}
public final RFuture<Long> sizeInMemoryAsync(List<Object> keys) {
return commandExecutor.evalWriteAsync((String)keys.get(0), StringCodec.INSTANCE, RedisCommands.EVAL_LONG,
"local total = 0;"
+ "for j = 1, #KEYS, 1 do "
+ "local size = redis.call('memory', 'usage', KEYS[j]); "
+ "if size ~= false then "
+ "total = total + size;"
+ "end; "
+ "end; "
+ "return total; ", keys);
}
@Override
public long sizeInMemory() {
return get(sizeInMemoryAsync());
}
@Override
public RFuture<Void> renameAsync(String newName) {

@ -16,6 +16,7 @@
package org.redisson;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
@ -624,6 +625,12 @@ public class RedissonPermitExpirableSemaphore extends RedissonExpirable implemen
Arrays.<Object>asList(getName(), getChannelName(), timeoutName), permitId, 1);
}
@Override
public RFuture<Long> sizeInMemoryAsync() {
List<Object> keys = Arrays.<Object>asList(getName(), timeoutName);
return super.sizeInMemoryAsync(keys);
}
@Override
public RFuture<Boolean> deleteAsync() {
return commandExecutor.writeAsync(getName(), RedisCommands.DEL_OBJECTS, getName(), timeoutName);

@ -186,6 +186,11 @@ public class RedissonSetMultimapCache<K, V> extends RedissonSetMultimap<K, V> im
return baseCache.expireKeyAsync(key, timeToLive, timeUnit);
}
@Override
public RFuture<Long> sizeInMemoryAsync() {
return baseCache.sizeInMemoryAsync();
}
@Override
public RFuture<Boolean> deleteAsync() {
return baseCache.deleteAsync();

@ -122,6 +122,11 @@ public class RedissonSetMultimapValues<V> extends RedissonExpirable implements R
System.currentTimeMillis(), encodeMapKey(key));
}
@Override
public RFuture<Long> sizeInMemoryAsync() {
List<Object> keys = Arrays.<Object>asList(getName(), timeoutSetName);
return super.sizeInMemoryAsync(keys);
}
@Override
public RFuture<Integer> sizeAsync() {

@ -27,6 +27,13 @@ import org.redisson.client.codec.Codec;
*/
public interface RObject extends RObjectAsync {
/**
* Returns size of object in Redis memory
*
* @return size of object
*/
long sizeInMemory();
/**
* Restores object using its state returned by {@link #dump()} method.
*

@ -25,6 +25,13 @@ import java.util.concurrent.TimeUnit;
*/
public interface RObjectAsync {
/**
* Returns size of object in Redis memory
*
* @return size of object
*/
RFuture<Long> sizeInMemoryAsync();
/**
* Restores object using its state returned by {@link #dumpAsync()} method.
*

@ -382,6 +382,7 @@ public interface RedisCommands {
RedisStrictCommand<Boolean> EXISTS = new RedisStrictCommand<Boolean>("EXISTS", new BooleanReplayConvertor());
RedisStrictCommand<Boolean> NOT_EXISTS = new RedisStrictCommand<Boolean>("EXISTS", new BooleanNumberReplayConvertor(1L));
RedisStrictCommand<Long> MEMORY_USAGE = new RedisStrictCommand<Long>("MEMORY", "USAGE", new LongReplayConvertor());
RedisStrictCommand<Boolean> RENAMENX = new RedisStrictCommand<Boolean>("RENAMENX", new BooleanReplayConvertor());
RedisStrictCommand<Void> RENAME = new RedisStrictCommand<Void>("RENAME", new VoidReplayConvertor());
RedisStrictCommand<Boolean> MOVE = new RedisStrictCommand<Boolean>("MOVE", new BooleanReplayConvertor());

@ -17,6 +17,13 @@ import org.redisson.config.Config;
public class RedissonBucketTest extends BaseTest {
@Test
public void testSizeInMemory() {
RBucket<Integer> al = redisson.getBucket("test");
al.set(1234);
assertThat(al.sizeInMemory()).isEqualTo(49);
}
@Test
public void testDumpAndRestore() {
RBucket<Integer> al = redisson.getBucket("test");

@ -70,6 +70,16 @@ public class RedissonMapCacheTest extends BaseMapTest {
return redisson.getMapCache("test", options);
}
@Test
public void testSizeInMemory() {
RMapCache<Integer, Integer> map = redisson.getMapCache("test");
for (int i = 0; i < 10; i++) {
map.put(i, i, 5, TimeUnit.SECONDS);
}
assertThat(map.sizeInMemory()).isGreaterThanOrEqualTo(466);
}
@Test
public void testRemainTimeToLive() {
RMapCache<String, String> map = redisson.getMapCache("test");

Loading…
Cancel
Save