Skip to content

Commit 85669e6

Browse files
mp911dechristophstrobl
authored andcommitted
DATAREDIS-1030 - Refine RedisScript creation.
Allow RedisScript.of(…) creation from a Resource. Original Pull Request: #473
1 parent ac4909f commit 85669e6

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

src/main/java/org/springframework/data/redis/core/script/DefaultRedisScript.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public DefaultRedisScript(String script, @Nullable Class<T> resultType) {
7575
* (non-Javadoc)
7676
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
7777
*/
78-
public void afterPropertiesSet() throws Exception {
78+
public void afterPropertiesSet() {
7979
Assert.state(this.scriptSource != null, "Either script, script location," + " or script source is required");
8080
}
8181

src/main/java/org/springframework/data/redis/core/script/RedisScript.java

+43-2
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
*/
1616
package org.springframework.data.redis.core.script;
1717

18+
import org.springframework.core.io.Resource;
1819
import org.springframework.lang.Nullable;
1920
import org.springframework.util.Assert;
2021

2122
/**
22-
* A script to be executed using the <a href="https://redis.io/commands/eval">Redis scripting support</a> available as of
23-
* version 2.6
23+
* A script to be executed using the <a href="https://redis.io/commands/eval">Redis scripting support</a> available as
24+
* of version 2.6
2425
*
2526
* @author Jennifer Hickey
2627
* @author Christoph Strobl
28+
* @author Mark Paluch
2729
* @param <T> The script result type. Should be one of Long, Boolean, List, or deserialized value type. Can be
2830
* {@litearl null} if the script returns a throw-away status (i.e "OK")
2931
*/
@@ -80,4 +82,43 @@ static <T> RedisScript of(String script, Class<T> resultType) {
8082

8183
return new DefaultRedisScript(script, resultType);
8284
}
85+
86+
/**
87+
* Creates new {@link RedisScript} from {@link Resource}.
88+
*
89+
* @param resource must not be {@literal null}.
90+
* @return new instance of {@link RedisScript}.
91+
* @since 2.2
92+
*/
93+
static <T> RedisScript<T> of(Resource resource) {
94+
95+
Assert.notNull(resource, "Resource must not be null!");
96+
97+
DefaultRedisScript<T> script = new DefaultRedisScript<>();
98+
script.setLocation(resource);
99+
script.afterPropertiesSet();
100+
101+
return script;
102+
}
103+
104+
/**
105+
* Creates new {@link RedisScript} from {@link Resource}.
106+
*
107+
* @param resource must not be {@literal null}.
108+
* @param resultType must not be {@literal null}.
109+
* @return new instance of {@link RedisScript}.
110+
* @since 2.2
111+
*/
112+
static <T> RedisScript<T> of(Resource resource, Class<T> resultType) {
113+
114+
Assert.notNull(resource, "Resource must not be null!");
115+
Assert.notNull(resultType, "ResultType must not be null!");
116+
117+
DefaultRedisScript<T> script = new DefaultRedisScript<>();
118+
script.setResultType(resultType);
119+
script.setLocation(resource);
120+
script.afterPropertiesSet();
121+
122+
return script;
123+
}
83124
}

src/test/java/org/springframework/data/redis/core/script/DefaultRedisScriptTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*
2929
* @author Jennifer Hickey
3030
* @author Christoph Strobl
31+
* @author Mark Paluch
3132
*/
3233
public class DefaultRedisScriptTests {
3334

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

59+
@Test // DATAREDIS-1030
60+
public void testGetScriptAsStringFromResource() {
61+
62+
RedisScript<String> redisScript = RedisScript
63+
.of(new ClassPathResource("org/springframework/data/redis/core/script/cas.lua"));
64+
assertThat(redisScript.getScriptAsString()).startsWith("local current = redis.call('GET', KEYS[1])");
65+
}
66+
5867
@Test(expected = ScriptingException.class)
5968
public void testGetScriptAsStringError() {
6069

0 commit comments

Comments
 (0)