From 9b0d405fdf2b4bb96c5ee28f00c12b9e7a8f2e7a Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 9 Mar 2020 10:57:39 +0100 Subject: [PATCH] DATAREDIS-1011 - Polishing. Fix copyright headers and update documentation. Original Pull Request: #511 --- src/main/asciidoc/reference/pipelining.adoc | 14 ++++++++- .../connection/lettuce/LettuceConnection.java | 30 +++++++++++++------ .../lettuce/LettuceConnectionFactory.java | 5 ++-- ...ionPipelineFlushOnEndIntegrationTests.java | 2 +- ...nPipelineTxFlushOnEndIntegrationTests.java | 2 +- 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/main/asciidoc/reference/pipelining.adoc b/src/main/asciidoc/reference/pipelining.adoc index 3cec6dfcd..8f5e61a37 100644 --- a/src/main/asciidoc/reference/pipelining.adoc +++ b/src/main/asciidoc/reference/pipelining.adoc @@ -22,6 +22,18 @@ List results = stringRedisTemplate.executePipelined( The preceding example runs a bulk right pop of items from a queue in a pipeline. The `results` `List` contains all of the popped items. `RedisTemplate` uses its value, hash key, and hash value serializers to deserialize all results before returning, so the returned items in the preceding example are Strings. There are additional `executePipelined` methods that let you pass a custom serializer for pipelined results. -Note that the value returned from the `RedisCallback` is required to be null, as this value is discarded in favor of returning the results of the pipelined commands. +Note that the value returned from the `RedisCallback` is required to be `null`, as this value is discarded in favor of returning the results of the pipelined commands. + +[TIP] +==== +The Lettuce driver supports fine grained flush control that allows to either flush commands as they appear, buffer or send them at connection close. + +[source,java] +---- +LettuceConnectionFactory factory = // ... +factory.setPipeliningFlushPolicy(PipeliningFlushPolicy.buffered(3)); <1> +---- +<1> Buffer locally and flush after every 3rd command. +==== include::version-note.adoc[] diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java index 62f34906a..d9959c547 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java @@ -65,9 +65,9 @@ import org.springframework.data.redis.ExceptionTranslationStrategy; import org.springframework.data.redis.FallbackExceptionTranslationStrategy; import org.springframework.data.redis.connection.*; import org.springframework.data.redis.connection.convert.TransactionResultConverter; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionProvider.TargetAware; import org.springframework.data.redis.connection.lettuce.LettuceResult.LettuceResultBuilder; import org.springframework.data.redis.connection.lettuce.LettuceResult.LettuceStatusResult; -import org.springframework.data.redis.connection.lettuce.LettuceConnectionProvider.TargetAware; import org.springframework.data.redis.core.RedisCommand; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -518,6 +518,7 @@ public class LettuceConnection extends AbstractRedisConnection { */ @Override public void openPipeline() { + if (!isPipelined) { isPipelined = true; ppline = new ArrayList<>(); @@ -877,6 +878,7 @@ public class LettuceConnection extends AbstractRedisConnection { * @see PipeliningFlushPolicy#flushEachCommand() * @see #openPipeline() * @see StatefulRedisConnection#flushCommands() + * @since 2.3 */ public void setPipeliningFlushPolicy(PipeliningFlushPolicy pipeliningFlushPolicy) { @@ -923,15 +925,14 @@ public class LettuceConnection extends AbstractRedisConnection { } void pipeline(LettuceResult result) { + + if (flushState != null) { + flushState.onCommand(getOrCreateDedicatedConnection()); + } + if (isQueueing()) { - - if (flushState != null) { - flushState.onCommand(getOrCreateDedicatedConnection()); - } - transaction(result); } else { - flushState.onCommand(getOrCreateDedicatedConnection()); ppline.add(result); } } @@ -1361,6 +1362,8 @@ public class LettuceConnection extends AbstractRedisConnection { * * @see StatefulRedisConnection#setAutoFlushCommands(boolean) * @see StatefulRedisConnection#flushCommands() + * @author Mark Paluch + * @since 2.3 */ public interface PipeliningFlushPolicy { @@ -1401,6 +1404,9 @@ public class LettuceConnection extends AbstractRedisConnection { /** * State object associated with flushing of the currently ongoing pipeline. + * + * @author Mark Paluch + * @since 2.3 */ public interface PipeliningFlushState { @@ -1431,6 +1437,8 @@ public class LettuceConnection extends AbstractRedisConnection { /** * Implementation to flush on each command. + * @author Mark Paluch + * @since 2.3 */ private enum FlushEachCommand implements PipeliningFlushPolicy, PipeliningFlushState { @@ -1453,6 +1461,8 @@ public class LettuceConnection extends AbstractRedisConnection { /** * Implementation to flush on closing the pipeline. + * @author Mark Paluch + * @since 2.3 */ private enum FlushOnClose implements PipeliningFlushPolicy, PipeliningFlushState { @@ -1475,13 +1485,15 @@ public class LettuceConnection extends AbstractRedisConnection { @Override public void onClose(StatefulConnection connection) { - connection.setAutoFlushCommands(true); connection.flushCommands(); + connection.setAutoFlushCommands(true); } } /** * Pipeline state for buffered flushing. + * @author Mark Paluch + * @since 2.3 */ private static class BufferedFlushing implements PipeliningFlushState { @@ -1507,8 +1519,8 @@ public class LettuceConnection extends AbstractRedisConnection { @Override public void onClose(StatefulConnection connection) { - connection.setAutoFlushCommands(true); connection.flushCommands(); + connection.setAutoFlushCommands(true); } } } diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java index d3ee7c7c5..556516340 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java @@ -560,12 +560,13 @@ public class LettuceConnectionFactory } /** - * Configures the flushing policy when using pipelining. Defaults to {@link PipeliningFlushPolicy#flushEachCommand() - * flush on each command}. + * Configures the flushing policy when using pipelining. If not set, defaults to + * {@link PipeliningFlushPolicy#flushEachCommand() flush on each command}. * * @param pipeliningFlushPolicy the flushing policy to control when commands get written to the Redis connection. * @see LettuceConnection#openPipeline() * @see StatefulRedisConnection#flushCommands() + * @since 2.3 */ public void setPipeliningFlushPolicy(PipeliningFlushPolicy pipeliningFlushPolicy) { diff --git a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionPipelineFlushOnEndIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionPipelineFlushOnEndIntegrationTests.java index 952c61c3a..f4b515f90 100644 --- a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionPipelineFlushOnEndIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionPipelineFlushOnEndIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2020 the original author or authors. + * Copyright 2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionPipelineTxFlushOnEndIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionPipelineTxFlushOnEndIntegrationTests.java index 909a20971..2b83823cd 100644 --- a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionPipelineTxFlushOnEndIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionPipelineTxFlushOnEndIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2020 the original author or authors. + * Copyright 2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.