Skip to content

Commit

Permalink
Add a evalReadOnly overload that accepts the script as a String (#…
Browse files Browse the repository at this point in the history
…2868)

* Add a evalReadOnly overload that accepts the script as a String

* Fix @SInCE annotation for 7.0
  • Loading branch information
BalmungSan committed May 31, 2024
1 parent 522abfd commit cb9bc8e
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 3 deletions.
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");
}

@Test
Expand Down

0 comments on commit cb9bc8e

Please sign in to comment.