Skip to content

DATAREDIS-1030 - Refine RedisScript creation. #473

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAREDIS-1030-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public DefaultRedisScript(String script, @Nullable Class<T> resultType) {
* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {
Assert.state(this.scriptSource != null, "Either script, script location," + " or script source is required");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@
*/
package org.springframework.data.redis.core.script;

import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
* A script to be executed using the <a href="https://redis.io/commands/eval">Redis scripting support</a> available as of
* version 2.6
* A script to be executed using the <a href="https://redis.io/commands/eval">Redis scripting support</a> available as
* of version 2.6
*
* @author Jennifer Hickey
* @author Christoph Strobl
* @author Mark Paluch
* @param <T> The script result type. Should be one of Long, Boolean, List, or deserialized value type. Can be
* {@litearl null} if the script returns a throw-away status (i.e "OK")
* {@literal null} if the script returns a throw-away status (i.e "OK")
*/
public interface RedisScript<T> {

/**
* @return The SHA1 of the script, used for executing Redis evalsha command.
* @return The SHA1 of the script, used for executing Redis {@code evalsha} command.
*/
String getSha1();

Expand Down Expand Up @@ -62,6 +64,9 @@ default boolean returnsRawValue() {
* @since 2.0
*/
static <T> RedisScript<T> of(String script) {

Assert.notNull(script, "Script must not be null!");

return new DefaultRedisScript<>(script);
}

Expand All @@ -73,11 +78,50 @@ static <T> RedisScript<T> of(String script) {
* @return new instance of {@link RedisScript}.
* @since 2.0
*/
static <T> RedisScript of(String script, Class<T> resultType) {
static <T> RedisScript<T> of(String script, Class<T> resultType) {

Assert.notNull(script, "Script must not be null!");
Assert.notNull(resultType, "ResultType must not be null!");

return new DefaultRedisScript(script, resultType);
return new DefaultRedisScript<>(script, resultType);
}

/**
* Creates new {@link RedisScript} from {@link Resource}.
*
* @param resource must not be {@literal null}.
* @return new instance of {@link RedisScript}.
* @since 2.2
*/
static <T> RedisScript<T> of(Resource resource) {

Assert.notNull(resource, "Resource must not be null!");

DefaultRedisScript<T> script = new DefaultRedisScript<>();
script.setLocation(resource);
script.afterPropertiesSet();

return script;
}

/**
* Creates new {@link RedisScript} from {@link Resource}.
*
* @param resource must not be {@literal null}.
* @param resultType must not be {@literal null}.
* @return new instance of {@link RedisScript}.
* @since 2.2
*/
static <T> RedisScript<T> of(Resource resource, Class<T> resultType) {

Assert.notNull(resource, "Resource must not be null!");
Assert.notNull(resultType, "ResultType must not be null!");

DefaultRedisScript<T> script = new DefaultRedisScript<>();
script.setResultType(resultType);
script.setLocation(resource);
script.afterPropertiesSet();

return script;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*
* @author Jennifer Hickey
* @author Christoph Strobl
* @author Mark Paluch
*/
public class DefaultRedisScriptTests {

Expand Down Expand Up @@ -55,6 +56,14 @@ public void testGetScriptAsString() {
assertThat(redisScript.getScriptAsString()).isEqualTo("return ARGS[1]");
}

@Test // DATAREDIS-1030
public void testGetScriptAsStringFromResource() {

RedisScript<String> redisScript = RedisScript
.of(new ClassPathResource("org/springframework/data/redis/core/script/cas.lua"));
assertThat(redisScript.getScriptAsString()).startsWith("local current = redis.call('GET', KEYS[1])");
}

@Test(expected = ScriptingException.class)
public void testGetScriptAsStringError() {

Expand Down