refactoring

pull/6120/head
Nikita Koksharov 8 months ago
parent c931831a63
commit 3f23018c83

@ -15,10 +15,6 @@
*/ */
package org.redisson; package org.redisson;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.redisson.api.RFuture; import org.redisson.api.RFuture;
import org.redisson.api.RObject; import org.redisson.api.RObject;
import org.redisson.client.codec.LongCodec; import org.redisson.client.codec.LongCodec;
@ -26,6 +22,10 @@ import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandAsyncExecutor; import org.redisson.command.CommandAsyncExecutor;
import org.redisson.eviction.EvictionScheduler; import org.redisson.eviction.EvictionScheduler;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
/** /**
* *
* @author Nikita Koksharov * @author Nikita Koksharov
@ -35,7 +35,7 @@ import org.redisson.eviction.EvictionScheduler;
class RedissonMultimapCache<K> { class RedissonMultimapCache<K> {
private final CommandAsyncExecutor commandExecutor; private final CommandAsyncExecutor commandExecutor;
private final RObject object; private final RedissonObject object;
private final String timeoutSetName; private final String timeoutSetName;
private final String prefix; private final String prefix;
private final EvictionScheduler evictionScheduler; private final EvictionScheduler evictionScheduler;
@ -43,7 +43,7 @@ class RedissonMultimapCache<K> {
RedissonMultimapCache(CommandAsyncExecutor commandExecutor, EvictionScheduler evictionScheduler, RedissonMultimapCache(CommandAsyncExecutor commandExecutor, EvictionScheduler evictionScheduler,
RObject object, String timeoutSetName, String prefix) { RObject object, String timeoutSetName, String prefix) {
this.commandExecutor = commandExecutor; this.commandExecutor = commandExecutor;
this.object = object; this.object = (RedissonObject) object;
this.timeoutSetName = timeoutSetName; this.timeoutSetName = timeoutSetName;
this.prefix = prefix; this.prefix = prefix;
this.evictionScheduler = evictionScheduler; this.evictionScheduler = evictionScheduler;
@ -55,7 +55,7 @@ class RedissonMultimapCache<K> {
public RFuture<Boolean> expireKeyAsync(K key, long timeToLive, TimeUnit timeUnit) { public RFuture<Boolean> expireKeyAsync(K key, long timeToLive, TimeUnit timeUnit) {
long ttlTimeout = System.currentTimeMillis() + timeUnit.toMillis(timeToLive); long ttlTimeout = System.currentTimeMillis() + timeUnit.toMillis(timeToLive);
return commandExecutor.evalWriteAsync(((RedissonObject) object).getRawName(), object.getCodec(), RedisCommands.EVAL_BOOLEAN, return commandExecutor.evalWriteAsync(object.getRawName(), object.getCodec(), RedisCommands.EVAL_BOOLEAN,
"if redis.call('hexists', KEYS[1], ARGV[2]) == 1 then " "if redis.call('hexists', KEYS[1], ARGV[2]) == 1 then "
+ "if tonumber(ARGV[1]) > 0 then " + "if tonumber(ARGV[1]) > 0 then "
+ "redis.call('zadd', KEYS[2], ARGV[1], ARGV[2]); " + + "redis.call('zadd', KEYS[2], ARGV[1], ARGV[2]); " +
@ -66,17 +66,17 @@ class RedissonMultimapCache<K> {
+ "else " + "else "
+ "return 0; " + "return 0; "
+ "end", + "end",
Arrays.<Object>asList(((RedissonObject) object).getRawName(), timeoutSetName), Arrays.asList(object.getRawName(), timeoutSetName),
ttlTimeout, ((RedissonObject) object).encodeMapKey(key)); ttlTimeout, object.encodeMapKey(key));
} }
public RFuture<Long> sizeInMemoryAsync() { public RFuture<Long> sizeInMemoryAsync() {
List<Object> keys = Arrays.<Object>asList(((RedissonObject) object).getRawName(), timeoutSetName); List<Object> keys = Arrays.asList(object.getRawName(), timeoutSetName);
return ((RedissonObject) object).sizeInMemoryAsync(keys); return object.sizeInMemoryAsync(keys);
} }
public RFuture<Boolean> deleteAsync() { public RFuture<Boolean> deleteAsync() {
return commandExecutor.evalWriteAsync(((RedissonObject) object).getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN_AMOUNT, return commandExecutor.evalWriteAsync(object.getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN_AMOUNT,
"local entries = redis.call('hgetall', KEYS[1]); " + "local entries = redis.call('hgetall', KEYS[1]); " +
"local keys = {KEYS[1], KEYS[2]}; " + "local keys = {KEYS[1], KEYS[2]}; " +
"for i, v in ipairs(entries) do " + "for i, v in ipairs(entries) do " +
@ -91,12 +91,12 @@ class RedissonMultimapCache<K> {
+ "n = n + redis.call('del', unpack(keys, i, math.min(i+4999, table.getn(keys)))) " + "n = n + redis.call('del', unpack(keys, i, math.min(i+4999, table.getn(keys)))) "
+ "end; " + "end; "
+ "return n;", + "return n;",
Arrays.asList(((RedissonObject) object).getRawName(), timeoutSetName), Arrays.asList(object.getRawName(), timeoutSetName),
prefix); prefix);
} }
public RFuture<Boolean> expireAsync(long timeToLive, TimeUnit timeUnit, String param) { public RFuture<Boolean> expireAsync(long timeToLive, TimeUnit timeUnit, String param) {
return commandExecutor.evalWriteAsync(((RedissonObject) object).getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, return commandExecutor.evalWriteAsync(object.getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"redis.call('zadd', KEYS[2], 92233720368547758, 'redisson__expiretag'); " + "redis.call('zadd', KEYS[2], 92233720368547758, 'redisson__expiretag'); " +
"local entries = redis.call('hgetall', KEYS[1]); " + "local entries = redis.call('hgetall', KEYS[1]); " +
"for i, v in ipairs(entries) do " + "for i, v in ipairs(entries) do " +
@ -115,12 +115,12 @@ class RedissonMultimapCache<K> {
+ "end; " + + "end; " +
"redis.call('pexpire', KEYS[2], ARGV[1]); " + "redis.call('pexpire', KEYS[2], ARGV[1]); " +
"return redis.call('pexpire', KEYS[1], ARGV[1]); ", "return redis.call('pexpire', KEYS[1], ARGV[1]); ",
Arrays.asList(((RedissonObject) object).getRawName(), timeoutSetName), Arrays.asList(object.getRawName(), timeoutSetName),
timeUnit.toMillis(timeToLive), prefix, param); timeUnit.toMillis(timeToLive), prefix, param);
} }
public RFuture<Boolean> expireAtAsync(long timestamp, String param) { public RFuture<Boolean> expireAtAsync(long timestamp, String param) {
return commandExecutor.evalWriteAsync(((RedissonObject) object).getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, return commandExecutor.evalWriteAsync(object.getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"redis.call('zadd', KEYS[2], 92233720368547758, 'redisson__expiretag');" + "redis.call('zadd', KEYS[2], 92233720368547758, 'redisson__expiretag');" +
"local entries = redis.call('hgetall', KEYS[1]); " + "local entries = redis.call('hgetall', KEYS[1]); " +
"for i, v in ipairs(entries) do " + "for i, v in ipairs(entries) do " +
@ -139,12 +139,12 @@ class RedissonMultimapCache<K> {
+ "end; " + + "end; " +
"redis.call('pexpireat', KEYS[2], ARGV[1]); " + "redis.call('pexpireat', KEYS[2], ARGV[1]); " +
"return redis.call('pexpireat', KEYS[1], ARGV[1]); ", "return redis.call('pexpireat', KEYS[1], ARGV[1]); ",
Arrays.<Object>asList(((RedissonObject) object).getRawName(), timeoutSetName), Arrays.asList(object.getRawName(), timeoutSetName),
timestamp, prefix, param); timestamp, prefix, param);
} }
public RFuture<Boolean> clearExpireAsync() { public RFuture<Boolean> clearExpireAsync() {
return commandExecutor.evalWriteAsync(((RedissonObject) object).getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, return commandExecutor.evalWriteAsync(object.getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"redis.call('zrem', KEYS[2], 'redisson__expiretag'); " + "redis.call('zrem', KEYS[2], 'redisson__expiretag'); " +
"local entries = redis.call('hgetall', KEYS[1]); " + "local entries = redis.call('hgetall', KEYS[1]); " +
"for i, v in ipairs(entries) do " + "for i, v in ipairs(entries) do " +
@ -155,15 +155,15 @@ class RedissonMultimapCache<K> {
"end; " + "end; " +
"redis.call('persist', KEYS[2]); " + "redis.call('persist', KEYS[2]); " +
"return redis.call('persist', KEYS[1]); ", "return redis.call('persist', KEYS[1]); ",
Arrays.<Object>asList(((RedissonObject) object).getRawName(), timeoutSetName), Arrays.asList(object.getRawName(), timeoutSetName),
prefix); prefix);
} }
public void destroy() { public void destroy() {
if (evictionScheduler != null) { if (evictionScheduler != null) {
evictionScheduler.remove(((RedissonObject) object).getRawName()); evictionScheduler.remove(object.getRawName());
} }
((RedissonObject) object).removeListeners(); object.removeListeners();
} }
} }

Loading…
Cancel
Save