Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a evalReadOnly overload that accepts the script as a String #2868

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,12 @@ public <T> RedisFuture<T> eval(byte[] script, ScriptOutputType type, K[] keys, V
return (RedisFuture<T>) dispatch(commandBuilder.eval(script, type, keys, values));
}

@Override
@SuppressWarnings("unchecked")
public <T> RedisFuture<T> evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values) {
return evalReadOnly(encodeScript(script), type, keys, values);
}

@Override
public <T> RedisFuture<T> evalReadOnly(byte[] script, ScriptOutputType type, K[] keys, V... values) {
return (RedisFuture<T>) dispatch(commandBuilder.eval(script, type, true, keys, values));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,12 @@ public <T> Flux<T> eval(byte[] script, ScriptOutputType type, K[] keys, V... val
return createFlux(() -> commandBuilder.eval(script, type, keys, values));
}

@Override
@SuppressWarnings("unchecked")
public <T> Flux<T> evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values) {
return evalReadOnly(encodeScript(script), type, keys, values);
}

@Override
public <T> Flux<T> evalReadOnly(byte[] script, ScriptOutputType type, K[] keys, V... values) {
return createFlux(() -> commandBuilder.eval(script, type, true, keys, values));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ public interface RedisScriptingAsyncCommands<K, V> {
*/
<T> RedisFuture<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 <T> expected return type.
* @return script result.
* @since 7.0
*/
<T> RedisFuture<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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ public interface RedisScriptingReactiveCommands<K, V> {
*/
<T> Flux<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 <T> expected return type.
* @return script result.
* @since 7.0
*/
<T> Flux<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.
*
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/io/lettuce/core/api/sync/RedisScriptingCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ public interface RedisScriptingCommands<K, V> {
*/
<T> 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 <T> expected return type.
* @return script result.
* @since 7.0
*/
<T> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ public interface NodeSelectionScriptingAsyncCommands<K, V> {
*/
<T> AsyncExecutions<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 <T> expected return type.
* @return script result.
* @since 7.0
*/
<T> AsyncExecutions<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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ public interface NodeSelectionScriptingCommands<K, V> {
*/
<T> Executions<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 <T> expected return type.
* @return script result.
* @since 7.0
*/
<T> Executions<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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ interface RedisScriptingCoroutinesCommands<K : Any, V : Any> {
*/
suspend fun <T> eval(script: ByteArray, type: ScriptOutputType, keys: Array<K>, 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 <T> expected return type.
* @return script result.
* @since 7.0
*/
suspend fun <T> evalReadOnly(
script: String,
type: ScriptOutputType,
keys: Array<K>,
vararg values: V
): T?

/**
* This is a read-only variant of the EVAL command that cannot execute commands that modify data.
*
Expand Down Expand Up @@ -214,4 +232,3 @@ interface RedisScriptingCoroutinesCommands<K : Any, V : Any> {
suspend fun digest(script: ByteArray): String?

}

Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ internal class RedisScriptingCoroutinesCommandsImpl<K : Any, V : Any>(internal v

override suspend fun <T> eval(script: ByteArray, type: ScriptOutputType, keys: Array<K>, vararg values: V): T? = ops.eval<T>(script, type, keys, *values).awaitFirstOrNull()

override suspend fun <T> evalReadOnly(
script: String,
type: ScriptOutputType,
keys: Array<K>,
vararg values: V
): T? = ops.evalReadOnly<T>(script, type, keys, *values).awaitFirstOrNull()

override suspend fun <T> evalReadOnly(
script: ByteArray,
type: ScriptOutputType,
Expand Down Expand Up @@ -86,4 +93,3 @@ internal class RedisScriptingCoroutinesCommandsImpl<K : Any, V : Any>(internal v
override suspend fun digest(script: ByteArray): String = ops.digest(script)

}

13 changes: 13 additions & 0 deletions src/main/templates/io/lettuce/core/api/RedisScriptingCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ public interface RedisScriptingCommands<K, V> {
*/
<T> 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 <T> expected return type.
* @return script result.
* @since 7.0
*/
<T> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
BalmungSan marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
Expand Down
Loading