diff --git a/src/main/java/org/springframework/data/redis/core/DefaultReactiveHashOperations.java b/src/main/java/org/springframework/data/redis/core/DefaultReactiveHashOperations.java index 24355dee6a..24adf3c6eb 100644 --- a/src/main/java/org/springframework/data/redis/core/DefaultReactiveHashOperations.java +++ b/src/main/java/org/springframework/data/redis/core/DefaultReactiveHashOperations.java @@ -51,6 +51,7 @@ * @author Mark Paluch * @author Christoph Strobl * @author John Blum + * @author Jewoo Shin * @since 2.0 */ class DefaultReactiveHashOperations implements ReactiveHashOperations { @@ -215,6 +216,15 @@ public Flux keys(H key) { .map(this::readRequiredHashKey)); } + @Override + public Mono lengthOfValue(H key, HK hashKey) { + + Assert.notNull(key, "Key must not be null"); + Assert.notNull(hashKey, "Hash key must not be null"); + + return createMono(hashCommands -> hashCommands.hStrLen(rawKey(key), rawHashKey(hashKey))); + } + @Override public Mono size(H key) { diff --git a/src/main/java/org/springframework/data/redis/core/ReactiveHashOperations.java b/src/main/java/org/springframework/data/redis/core/ReactiveHashOperations.java index 3015848284..b0f9ab685b 100644 --- a/src/main/java/org/springframework/data/redis/core/ReactiveHashOperations.java +++ b/src/main/java/org/springframework/data/redis/core/ReactiveHashOperations.java @@ -45,6 +45,7 @@ * @author Mark Paluch * @author Christoph Strobl * @author Viktoriya Kutsarova + * @author Jewoo Shin * @since 2.0 */ public interface ReactiveHashOperations { @@ -200,6 +201,18 @@ Mono putAndExpire(H key, Map map, */ Flux keys(H key); + /** + * Returns the length of the value associated with {@code hashKey}. If either the {@code key} or the {@code hashKey} + * do not exist, {@code 0} is emitted. + * + * @param key must not be {@literal null}. + * @param hashKey must not be {@literal null}. + * @return {@link Mono} emitting the length of the value associated with {@code hashKey}. + * @since 4.2 + * @see Redis Documentation: HSTRLEN + */ + Mono lengthOfValue(H key, HK hashKey); + /** * Get size of hash at {@code key}. * diff --git a/src/test/java/org/springframework/data/redis/core/DefaultReactiveHashOperationsIntegrationTests.java b/src/test/java/org/springframework/data/redis/core/DefaultReactiveHashOperationsIntegrationTests.java index 84a32a8fc3..35a9a17350 100644 --- a/src/test/java/org/springframework/data/redis/core/DefaultReactiveHashOperationsIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/core/DefaultReactiveHashOperationsIntegrationTests.java @@ -58,6 +58,7 @@ * * @author Mark Paluch * @author Christoph Strobl + * @author Jewoo Shin */ @ParameterizedClass @MethodSource("testParams") @@ -364,6 +365,26 @@ void keys() { .verifyComplete(); } + @Test // GH-3376 + void lengthOfValue() { + + assumeThat(hashValueFactory instanceof StringObjectFactory).isTrue(); + + K key = keyFactory.instance(); + HK hashkey1 = hashKeyFactory.instance(); + HV hashvalue1 = hashValueFactory.instance(); + + HK hashkey2 = hashKeyFactory.instance(); + HV hashvalue2 = hashValueFactory.instance(); + + putAll(key, hashkey1, hashvalue1, hashkey2, hashvalue2); + + hashOperations.lengthOfValue(key, hashkey1) // + .as(StepVerifier::create) // + .expectNext((long) hashvalue1.toString().length()) // + .verifyComplete(); + } + @Test // DATAREDIS-602 void size() {