From 573530a7f60b2237bce972a3bfafdad92306c3c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20Mej=C3=ADa=20Su=C3=A1rez?= Date: Thu, 30 May 2024 12:56:45 -0500 Subject: [PATCH 1/2] Add a evalReadOnly overload that accepts the script as a String --- .../core/AbstractRedisAsyncCommands.java | 6 ++++++ .../core/AbstractRedisReactiveCommands.java | 6 ++++++ .../async/RedisScriptingAsyncCommands.java | 13 +++++++++++++ .../RedisScriptingReactiveCommands.java | 13 +++++++++++++ .../core/api/sync/RedisScriptingCommands.java | 13 +++++++++++++ .../NodeSelectionScriptingAsyncCommands.java | 13 +++++++++++++ .../sync/NodeSelectionScriptingCommands.java | 13 +++++++++++++ .../RedisScriptingCoroutinesCommands.kt | 19 ++++++++++++++++++- .../RedisScriptingCoroutinesCommandsImpl.kt | 8 +++++++- .../core/api/RedisScriptingCommands.java | 13 +++++++++++++ .../ScriptingCommandIntegrationTests.java | 2 +- 11 files changed, 116 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java b/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java index 4749ad984f..b7da8fb2fd 100644 --- a/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java +++ b/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java @@ -741,6 +741,12 @@ public RedisFuture eval(byte[] script, ScriptOutputType type, K[] keys, V return (RedisFuture) dispatch(commandBuilder.eval(script, type, keys, values)); } + @Override + @SuppressWarnings("unchecked") + public RedisFuture evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values) { + return evalReadOnly(encodeScript(script), type, keys, values); + } + @Override public RedisFuture evalReadOnly(byte[] script, ScriptOutputType type, K[] keys, V... values) { return (RedisFuture) dispatch(commandBuilder.eval(script, type, true, keys, values)); diff --git a/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java b/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java index fa5630ed71..38404c1ea4 100644 --- a/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java +++ b/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java @@ -797,6 +797,12 @@ public Flux eval(byte[] script, ScriptOutputType type, K[] keys, V... val return createFlux(() -> commandBuilder.eval(script, type, keys, values)); } + @Override + @SuppressWarnings("unchecked") + public Flux evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values) { + return evalReadOnly(encodeScript(script), type, keys, values); + } + @Override public Flux evalReadOnly(byte[] script, ScriptOutputType type, K[] keys, V... values) { return createFlux(() -> commandBuilder.eval(script, type, true, keys, values)); diff --git a/src/main/java/io/lettuce/core/api/async/RedisScriptingAsyncCommands.java b/src/main/java/io/lettuce/core/api/async/RedisScriptingAsyncCommands.java index 1367b489f2..2c56fa2cc7 100644 --- a/src/main/java/io/lettuce/core/api/async/RedisScriptingAsyncCommands.java +++ b/src/main/java/io/lettuce/core/api/async/RedisScriptingAsyncCommands.java @@ -85,6 +85,19 @@ public interface RedisScriptingAsyncCommands { */ RedisFuture eval(byte[] script, ScriptOutputType type, K[] keys, V... values); + /** + * This is a read-only variant of the EVAL command that cannot execute commands that modify data. + * + * @param script Lua 5.1 script. + * @param type the type. + * @param keys the keys. + * @param values the values. + * @param expected return type. + * @return script result. + * @since 6.4 + */ + RedisFuture evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); + /** * This is a read-only variant of the EVAL command that cannot execute commands that modify data. * diff --git a/src/main/java/io/lettuce/core/api/reactive/RedisScriptingReactiveCommands.java b/src/main/java/io/lettuce/core/api/reactive/RedisScriptingReactiveCommands.java index 0272f0cda0..30164f566c 100644 --- a/src/main/java/io/lettuce/core/api/reactive/RedisScriptingReactiveCommands.java +++ b/src/main/java/io/lettuce/core/api/reactive/RedisScriptingReactiveCommands.java @@ -84,6 +84,19 @@ public interface RedisScriptingReactiveCommands { */ Flux eval(byte[] script, ScriptOutputType type, K[] keys, V... values); + /** + * This is a read-only variant of the EVAL command that cannot execute commands that modify data. + * + * @param script Lua 5.1 script. + * @param type the type. + * @param keys the keys. + * @param values the values. + * @param expected return type. + * @return script result. + * @since 6.4 + */ + Flux evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); + /** * This is a read-only variant of the EVAL command that cannot execute commands that modify data. * diff --git a/src/main/java/io/lettuce/core/api/sync/RedisScriptingCommands.java b/src/main/java/io/lettuce/core/api/sync/RedisScriptingCommands.java index cc359cb42a..48da2c9c81 100644 --- a/src/main/java/io/lettuce/core/api/sync/RedisScriptingCommands.java +++ b/src/main/java/io/lettuce/core/api/sync/RedisScriptingCommands.java @@ -84,6 +84,19 @@ public interface RedisScriptingCommands { */ T eval(byte[] script, ScriptOutputType type, K[] keys, V... values); + /** + * This is a read-only variant of the EVAL command that cannot execute commands that modify data. + * + * @param script Lua 5.1 script. + * @param type output type. + * @param keys the keys. + * @param values the values. + * @param expected return type. + * @return script result. + * @since 6.4 + */ + T evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); + /** * This is a read-only variant of the EVAL command that cannot execute commands that modify data. * diff --git a/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionScriptingAsyncCommands.java b/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionScriptingAsyncCommands.java index e116ce20b4..bf3daca58f 100644 --- a/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionScriptingAsyncCommands.java +++ b/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionScriptingAsyncCommands.java @@ -84,6 +84,19 @@ public interface NodeSelectionScriptingAsyncCommands { */ AsyncExecutions eval(byte[] script, ScriptOutputType type, K[] keys, V... values); + /** + * This is a read-only variant of the EVAL command that cannot execute commands that modify data. + * + * @param script Lua 5.1 script. + * @param type the type. + * @param keys the keys. + * @param values the values. + * @param expected return type. + * @return script result. + * @since 6.4 + */ + AsyncExecutions evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); + /** * This is a read-only variant of the EVAL command that cannot execute commands that modify data. * diff --git a/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionScriptingCommands.java b/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionScriptingCommands.java index e1c2306626..9a921d18d0 100644 --- a/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionScriptingCommands.java +++ b/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionScriptingCommands.java @@ -84,6 +84,19 @@ public interface NodeSelectionScriptingCommands { */ Executions eval(byte[] script, ScriptOutputType type, K[] keys, V... values); + /** + * This is a read-only variant of the EVAL command that cannot execute commands that modify data. + * + * @param script Lua 5.1 script. + * @param type the type. + * @param keys the keys. + * @param values the values. + * @param expected return type. + * @return script result. + * @since 6.4 + */ + Executions evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); + /** * This is a read-only variant of the EVAL command that cannot execute commands that modify data. * diff --git a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommands.kt b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommands.kt index 020ac1a873..8b0d4a0aa3 100644 --- a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommands.kt +++ b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommands.kt @@ -85,6 +85,24 @@ interface RedisScriptingCoroutinesCommands { */ suspend fun eval(script: ByteArray, type: ScriptOutputType, keys: Array, vararg values: V): T? + /** + * This is a read-only variant of the EVAL command that cannot execute commands that modify data. + * + * @param script Lua 5.1 script. + * @param type the type. + * @param keys the keys. + * @param values the values. + * @param expected return type. + * @return script result. + * @since 6.4 + */ + suspend fun evalReadOnly( + script: String, + type: ScriptOutputType, + keys: Array, + vararg values: V + ): T? + /** * This is a read-only variant of the EVAL command that cannot execute commands that modify data. * @@ -214,4 +232,3 @@ interface RedisScriptingCoroutinesCommands { suspend fun digest(script: ByteArray): String? } - diff --git a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommandsImpl.kt b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommandsImpl.kt index 403cb26164..30d0625b4e 100644 --- a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommandsImpl.kt +++ b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommandsImpl.kt @@ -51,6 +51,13 @@ internal class RedisScriptingCoroutinesCommandsImpl(internal v override suspend fun eval(script: ByteArray, type: ScriptOutputType, keys: Array, vararg values: V): T? = ops.eval(script, type, keys, *values).awaitFirstOrNull() + override suspend fun evalReadOnly( + script: String, + type: ScriptOutputType, + keys: Array, + vararg values: V + ): T? = ops.evalReadOnly(script, type, keys, *values).awaitFirstOrNull() + override suspend fun evalReadOnly( script: ByteArray, type: ScriptOutputType, @@ -86,4 +93,3 @@ internal class RedisScriptingCoroutinesCommandsImpl(internal v override suspend fun digest(script: ByteArray): String = ops.digest(script) } - diff --git a/src/main/templates/io/lettuce/core/api/RedisScriptingCommands.java b/src/main/templates/io/lettuce/core/api/RedisScriptingCommands.java index ed47b21ca7..eaefbf48f6 100644 --- a/src/main/templates/io/lettuce/core/api/RedisScriptingCommands.java +++ b/src/main/templates/io/lettuce/core/api/RedisScriptingCommands.java @@ -83,6 +83,19 @@ public interface RedisScriptingCommands { */ T eval(byte[] script, ScriptOutputType type, K[] keys, V... values); + /** + * This is a read-only variant of the EVAL command that cannot execute commands that modify data. + * + * @param script Lua 5.1 script. + * @param type the type. + * @param keys the keys. + * @param values the values. + * @param expected return type. + * @return script result. + * @since 6.4 + */ + T evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); + /** * This is a read-only variant of the EVAL command that cannot execute commands that modify data. * diff --git a/src/test/java/io/lettuce/core/commands/ScriptingCommandIntegrationTests.java b/src/test/java/io/lettuce/core/commands/ScriptingCommandIntegrationTests.java index 96cd29db3f..1c7afd30e2 100644 --- a/src/test/java/io/lettuce/core/commands/ScriptingCommandIntegrationTests.java +++ b/src/test/java/io/lettuce/core/commands/ScriptingCommandIntegrationTests.java @@ -134,7 +134,7 @@ void evalWithArgs() { @EnabledOnCommand("EVAL_RO") // Redis 7.0 void evalReadOnly() { String[] keys = new String[] { "key1" }; - assertThat((String) redis.evalReadOnly("return KEYS[1]".getBytes(), STATUS, keys, "a")).isEqualTo("key1"); + assertThat((String) redis.evalReadOnly("return KEYS[1]", STATUS, keys, "a")).isEqualTo("key1"); } @Test From 8ac05aa50dcda7331ba5215c91beab3a83cc059a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20Mej=C3=ADa=20Su=C3=A1rez?= Date: Thu, 30 May 2024 13:54:37 -0500 Subject: [PATCH 2/2] Fix @since annotation for 7.0 --- .../io/lettuce/core/api/async/RedisScriptingAsyncCommands.java | 2 +- .../core/api/reactive/RedisScriptingReactiveCommands.java | 2 +- .../java/io/lettuce/core/api/sync/RedisScriptingCommands.java | 2 +- .../cluster/api/async/NodeSelectionScriptingAsyncCommands.java | 2 +- .../core/cluster/api/sync/NodeSelectionScriptingCommands.java | 2 +- .../core/api/coroutines/RedisScriptingCoroutinesCommands.kt | 2 +- .../templates/io/lettuce/core/api/RedisScriptingCommands.java | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/lettuce/core/api/async/RedisScriptingAsyncCommands.java b/src/main/java/io/lettuce/core/api/async/RedisScriptingAsyncCommands.java index 2c56fa2cc7..0158e92f78 100644 --- a/src/main/java/io/lettuce/core/api/async/RedisScriptingAsyncCommands.java +++ b/src/main/java/io/lettuce/core/api/async/RedisScriptingAsyncCommands.java @@ -94,7 +94,7 @@ public interface RedisScriptingAsyncCommands { * @param values the values. * @param expected return type. * @return script result. - * @since 6.4 + * @since 7.0 */ RedisFuture evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); diff --git a/src/main/java/io/lettuce/core/api/reactive/RedisScriptingReactiveCommands.java b/src/main/java/io/lettuce/core/api/reactive/RedisScriptingReactiveCommands.java index 30164f566c..1d9d7917c6 100644 --- a/src/main/java/io/lettuce/core/api/reactive/RedisScriptingReactiveCommands.java +++ b/src/main/java/io/lettuce/core/api/reactive/RedisScriptingReactiveCommands.java @@ -93,7 +93,7 @@ public interface RedisScriptingReactiveCommands { * @param values the values. * @param expected return type. * @return script result. - * @since 6.4 + * @since 7.0 */ Flux evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); diff --git a/src/main/java/io/lettuce/core/api/sync/RedisScriptingCommands.java b/src/main/java/io/lettuce/core/api/sync/RedisScriptingCommands.java index 48da2c9c81..d6161251e5 100644 --- a/src/main/java/io/lettuce/core/api/sync/RedisScriptingCommands.java +++ b/src/main/java/io/lettuce/core/api/sync/RedisScriptingCommands.java @@ -93,7 +93,7 @@ public interface RedisScriptingCommands { * @param values the values. * @param expected return type. * @return script result. - * @since 6.4 + * @since 7.0 */ T evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); diff --git a/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionScriptingAsyncCommands.java b/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionScriptingAsyncCommands.java index bf3daca58f..df0b551786 100644 --- a/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionScriptingAsyncCommands.java +++ b/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionScriptingAsyncCommands.java @@ -93,7 +93,7 @@ public interface NodeSelectionScriptingAsyncCommands { * @param values the values. * @param expected return type. * @return script result. - * @since 6.4 + * @since 7.0 */ AsyncExecutions evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); diff --git a/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionScriptingCommands.java b/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionScriptingCommands.java index 9a921d18d0..b7b40351b2 100644 --- a/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionScriptingCommands.java +++ b/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionScriptingCommands.java @@ -93,7 +93,7 @@ public interface NodeSelectionScriptingCommands { * @param values the values. * @param expected return type. * @return script result. - * @since 6.4 + * @since 7.0 */ Executions evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); diff --git a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommands.kt b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommands.kt index 8b0d4a0aa3..711e7569b8 100644 --- a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommands.kt +++ b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommands.kt @@ -94,7 +94,7 @@ interface RedisScriptingCoroutinesCommands { * @param values the values. * @param expected return type. * @return script result. - * @since 6.4 + * @since 7.0 */ suspend fun evalReadOnly( script: String, diff --git a/src/main/templates/io/lettuce/core/api/RedisScriptingCommands.java b/src/main/templates/io/lettuce/core/api/RedisScriptingCommands.java index eaefbf48f6..96e09c3940 100644 --- a/src/main/templates/io/lettuce/core/api/RedisScriptingCommands.java +++ b/src/main/templates/io/lettuce/core/api/RedisScriptingCommands.java @@ -92,7 +92,7 @@ public interface RedisScriptingCommands { * @param values the values. * @param expected return type. * @return script result. - * @since 6.4 + * @since 7.0 */ T evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values);