diff --git a/src/main/java/org/redisson/CommandExecutor.java b/src/main/java/org/redisson/CommandExecutor.java index 58baf6a37..5d63b7447 100644 --- a/src/main/java/org/redisson/CommandExecutor.java +++ b/src/main/java/org/redisson/CommandExecutor.java @@ -73,7 +73,7 @@ public interface CommandExecutor { Future> readAllAsync(RedisCommand command, Object ... params); - Future writeAllAsync(RedisCommand command, Object ... params); + Future writeAllAsync(RedisCommand command, Object ... params); Future writeAsync(String key, RedisCommand command, Object ... params); diff --git a/src/main/java/org/redisson/CommandExecutorService.java b/src/main/java/org/redisson/CommandExecutorService.java index 6e8d73f25..3af0cd170 100644 --- a/src/main/java/org/redisson/CommandExecutorService.java +++ b/src/main/java/org/redisson/CommandExecutorService.java @@ -99,34 +99,8 @@ public class CommandExecutorService implements CommandExecutor { return mainPromise; } - public Future writeAllAsync(RedisCommand command, Object ... params) { - return allAsync(false, command, params); - } - - public Future allAsync(boolean readOnlyMode, RedisCommand command, Object ... params) { - final Promise mainPromise = connectionManager.newPromise(); - Promise promise = new DefaultPromise() { - AtomicInteger counter = new AtomicInteger(connectionManager.getEntries().keySet().size()); - @Override - public Promise setSuccess(Object result) { - if (counter.decrementAndGet() == 0 - && !mainPromise.isDone()) { - mainPromise.setSuccess(true); - } - return this; - } - - @Override - public Promise setFailure(Throwable cause) { - mainPromise.setFailure(cause); - return this; - } - - }; - for (Integer slot : connectionManager.getEntries().keySet()) { - async(readOnlyMode, slot, null, connectionManager.getCodec(), command, params, promise, 0); - } - return mainPromise; + public Future writeAllAsync(RedisCommand command, Object ... params) { + return writeAllAsync(command, null, params); } public Future writeAllAsync(RedisCommand command, SlotCallback callback, Object ... params) { @@ -139,10 +113,15 @@ public class CommandExecutorService implements CommandExecutor { AtomicInteger counter = new AtomicInteger(connectionManager.getEntries().keySet().size()); @Override public Promise setSuccess(T result) { - callback.onSlotResult(result); - if (counter.decrementAndGet() == 0 - && !mainPromise.isDone()) { - mainPromise.setSuccess(callback.onFinish()); + if (callback != null) { + callback.onSlotResult(result); + } + if (counter.decrementAndGet() == 0) { + if (callback != null) { + mainPromise.setSuccess(callback.onFinish()); + } else { + mainPromise.setSuccess(null); + } } return this; } diff --git a/src/main/java/org/redisson/RedissonScript.java b/src/main/java/org/redisson/RedissonScript.java index 324dd5b74..7ad1d95b0 100644 --- a/src/main/java/org/redisson/RedissonScript.java +++ b/src/main/java/org/redisson/RedissonScript.java @@ -34,7 +34,7 @@ public class RedissonScript implements RScript { @Override public String scriptLoad(String luaScript) { - return scriptLoad(null, luaScript); + return commandExecutor.get(scriptLoadAsync(luaScript)); } @Override @@ -43,7 +43,18 @@ public class RedissonScript implements RScript { } public Future scriptLoadAsync(String luaScript) { - return scriptLoadAsync(null, luaScript); + return commandExecutor.writeAllAsync(RedisCommands.SCRIPT_LOAD, new SlotCallback() { + volatile String result; + @Override + public void onSlotResult(String result) { + this.result = result; + } + + @Override + public String onFinish() { + return result; + } + }, luaScript); } @Override @@ -119,22 +130,22 @@ public class RedissonScript implements RScript { } @Override - public boolean scriptKill() { - return commandExecutor.get(scriptKillAsync()); + public void scriptKill() { + commandExecutor.get(scriptKillAsync()); } @Override - public boolean scriptKill(String key) { - return commandExecutor.get(scriptKillAsync(key)); + public void scriptKill(String key) { + commandExecutor.get(scriptKillAsync(key)); } @Override - public Future scriptKillAsync() { + public Future scriptKillAsync() { return commandExecutor.writeAllAsync(RedisCommands.SCRIPT_KILL); } @Override - public Future scriptKillAsync(String key) { + public Future scriptKillAsync(String key) { return commandExecutor.writeAsync(key, RedisCommands.SCRIPT_KILL); } @@ -149,22 +160,22 @@ public class RedissonScript implements RScript { } @Override - public boolean scriptFlush() { - return commandExecutor.get(scriptFlushAsync()); + public void scriptFlush() { + commandExecutor.get(scriptFlushAsync()); } @Override - public boolean scriptFlush(String key) { - return commandExecutor.get(scriptFlushAsync(key)); + public void scriptFlush(String key) { + commandExecutor.get(scriptFlushAsync(key)); } @Override - public Future scriptFlushAsync() { + public Future scriptFlushAsync() { return commandExecutor.writeAllAsync(RedisCommands.SCRIPT_FLUSH); } @Override - public Future scriptFlushAsync(String key) { + public Future scriptFlushAsync(String key) { return commandExecutor.writeAsync(key, RedisCommands.SCRIPT_FLUSH); } diff --git a/src/main/java/org/redisson/core/RScript.java b/src/main/java/org/redisson/core/RScript.java index 646cae5ae..35e3484a3 100644 --- a/src/main/java/org/redisson/core/RScript.java +++ b/src/main/java/org/redisson/core/RScript.java @@ -55,15 +55,15 @@ public interface RScript extends RScriptAsync { String scriptLoad(String luaScript); - boolean scriptKill(); + void scriptKill(); List scriptExists(String key, String ... shaDigests); - boolean scriptFlush(); + void scriptFlush(); - boolean scriptFlush(String key); + void scriptFlush(String key); - boolean scriptKill(String key); + void scriptKill(String key); String scriptLoad(String key, String luaScript); diff --git a/src/main/java/org/redisson/core/RScriptAsync.java b/src/main/java/org/redisson/core/RScriptAsync.java index f1b6e9f82..67ee7be4c 100644 --- a/src/main/java/org/redisson/core/RScriptAsync.java +++ b/src/main/java/org/redisson/core/RScriptAsync.java @@ -24,7 +24,7 @@ import io.netty.util.concurrent.Future; public interface RScriptAsync { - Future scriptFlushAsync(); + Future scriptFlushAsync(); Future evalShaAsync(Mode mode, String shaDigest, ReturnType returnType, List keys, Object... values); @@ -32,13 +32,13 @@ public interface RScriptAsync { Future scriptLoadAsync(String luaScript); - Future scriptKillAsync(); + Future scriptKillAsync(); Future> scriptExistsAsync(String key, String ... shaDigests); - Future scriptFlushAsync(String key); + Future scriptFlushAsync(String key); - Future scriptKillAsync(String key); + Future scriptKillAsync(String key); Future scriptLoadAsync(String key, String luaScript); diff --git a/src/test/java/org/redisson/RedissonScriptTest.java b/src/test/java/org/redisson/RedissonScriptTest.java index 0bb39c3e6..a79139f8f 100644 --- a/src/test/java/org/redisson/RedissonScriptTest.java +++ b/src/test/java/org/redisson/RedissonScriptTest.java @@ -53,8 +53,8 @@ public class RedissonScriptTest extends BaseTest { Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", r); String r1 = redisson.getScript().evalSha(Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList()); Assert.assertEquals("bar", r1); - Boolean r2 = redisson.getScript().scriptFlush(); - Assert.assertTrue(r2); + redisson.getScript().scriptFlush(); + try { redisson.getScript().evalSha(Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList()); } catch (Exception e) { @@ -82,15 +82,21 @@ public class RedissonScriptTest extends BaseTest { @Test public void testEvalSha() { + RScript s = redisson.getScript(); + String res = s.scriptLoad(null, "return redis.call('get', 'foo')"); + Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", res); + redisson.getBucket("foo").set("bar"); - String r = redisson.getScript().eval(Mode.READ_ONLY, "return redis.call('get', 'foo')", RScript.ReturnType.VALUE); - Assert.assertEquals("bar", r); - String r1 = redisson.getScript().evalSha(Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList()); + String r1 = s.evalSha(Mode.READ_ONLY, "282297a0228f48cd3fc6a55de6316f31422f5d17", RScript.ReturnType.VALUE, Collections.emptyList()); Assert.assertEquals("bar", r1); } @Test public void testEvalshaAsync() { + RScript s = redisson.getScript(); + String res = s.scriptLoad(null, "return redis.call('get', 'foo')"); + Assert.assertEquals("282297a0228f48cd3fc6a55de6316f31422f5d17", res); + redisson.getBucket("foo").set("bar"); String r = redisson.getScript().eval(Mode.READ_ONLY, "return redis.call('get', 'foo')", RScript.ReturnType.VALUE); Assert.assertEquals("bar", r);