From 577694f91b5f681393fb9a13fa1fd5f405d04f94 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 2 Sep 2019 15:13:09 +0200 Subject: [PATCH 1/3] DATAREDIS-1030 - Prepare issue branch. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6e446970fc..693f250ad8 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-redis - 2.2.0.BUILD-SNAPSHOT + 2.2.0.DATAREDIS-1030-SNAPSHOT Spring Data Redis From 3bf0a008f5b9137b734aa6db2a61b5e9f5f55606 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 2 Sep 2019 15:18:29 +0200 Subject: [PATCH 2/3] DATAREDIS-1030 - Refine RedisScript creation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allow RedisScript.of(…) creation from a Resource. --- .../redis/core/script/DefaultRedisScript.java | 2 +- .../data/redis/core/script/RedisScript.java | 45 ++++++++++++++++++- .../core/script/DefaultRedisScriptTests.java | 9 ++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/core/script/DefaultRedisScript.java b/src/main/java/org/springframework/data/redis/core/script/DefaultRedisScript.java index 43374983fe..c78a26a29e 100644 --- a/src/main/java/org/springframework/data/redis/core/script/DefaultRedisScript.java +++ b/src/main/java/org/springframework/data/redis/core/script/DefaultRedisScript.java @@ -75,7 +75,7 @@ public DefaultRedisScript(String script, @Nullable Class 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"); } diff --git a/src/main/java/org/springframework/data/redis/core/script/RedisScript.java b/src/main/java/org/springframework/data/redis/core/script/RedisScript.java index 0716fc100c..67c16e204f 100644 --- a/src/main/java/org/springframework/data/redis/core/script/RedisScript.java +++ b/src/main/java/org/springframework/data/redis/core/script/RedisScript.java @@ -15,15 +15,17 @@ */ 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 Redis scripting support available as of - * version 2.6 + * A script to be executed using the Redis scripting support available as + * of version 2.6 * * @author Jennifer Hickey * @author Christoph Strobl + * @author Mark Paluch * @param 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") */ @@ -80,4 +82,43 @@ static RedisScript of(String script, Class 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 RedisScript of(Resource resource) { + + Assert.notNull(resource, "Resource must not be null!"); + + DefaultRedisScript 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 RedisScript of(Resource resource, Class resultType) { + + Assert.notNull(resource, "Resource must not be null!"); + Assert.notNull(resultType, "ResultType must not be null!"); + + DefaultRedisScript script = new DefaultRedisScript<>(); + script.setResultType(resultType); + script.setLocation(resource); + script.afterPropertiesSet(); + + return script; + } } diff --git a/src/test/java/org/springframework/data/redis/core/script/DefaultRedisScriptTests.java b/src/test/java/org/springframework/data/redis/core/script/DefaultRedisScriptTests.java index 6db812600c..72730c267b 100644 --- a/src/test/java/org/springframework/data/redis/core/script/DefaultRedisScriptTests.java +++ b/src/test/java/org/springframework/data/redis/core/script/DefaultRedisScriptTests.java @@ -28,6 +28,7 @@ * * @author Jennifer Hickey * @author Christoph Strobl + * @author Mark Paluch */ public class DefaultRedisScriptTests { @@ -55,6 +56,14 @@ public void testGetScriptAsString() { assertThat(redisScript.getScriptAsString()).isEqualTo("return ARGS[1]"); } + @Test // DATAREDIS-1030 + public void testGetScriptAsStringFromResource() { + + RedisScript 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() { From ad9ec54c66712929fcaf54c09e365edcfe20d90e Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 2 Sep 2019 15:19:54 +0200 Subject: [PATCH 3/3] DATAREDIS-1030 - Polishing. Add assertions. Fix generics and typos. --- .../data/redis/core/script/RedisScript.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/core/script/RedisScript.java b/src/main/java/org/springframework/data/redis/core/script/RedisScript.java index 67c16e204f..a4b53d9f68 100644 --- a/src/main/java/org/springframework/data/redis/core/script/RedisScript.java +++ b/src/main/java/org/springframework/data/redis/core/script/RedisScript.java @@ -27,12 +27,12 @@ * @author Christoph Strobl * @author Mark Paluch * @param 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 { /** - * @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(); @@ -64,6 +64,9 @@ default boolean returnsRawValue() { * @since 2.0 */ static RedisScript of(String script) { + + Assert.notNull(script, "Script must not be null!"); + return new DefaultRedisScript<>(script); } @@ -75,12 +78,12 @@ static RedisScript of(String script) { * @return new instance of {@link RedisScript}. * @since 2.0 */ - static RedisScript of(String script, Class resultType) { + static RedisScript of(String script, Class 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); } /**