Skip to content

Commit 3191def

Browse files
schnapstermp911de
authored andcommitted
Add support for flush modes using FLUSHDB and FLUSHALL commands.
Closes #2187 Original pull request: #2190.
1 parent b895229 commit 3191def

24 files changed

+737
-8
lines changed

src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
* @author Andrey Shlykov
7676
* @author dengliming
7777
* @author ihaohong
78+
* @author Dennis Neufeld
7879
*/
7980
public class DefaultStringRedisConnection implements StringRedisConnection, DecoratedRedisConnection {
8081

@@ -313,11 +314,21 @@ public void flushAll() {
313314
delegate.flushAll();
314315
}
315316

317+
@Override
318+
public void flushAll(FlushOption option) {
319+
delegate.flushAll(option);
320+
}
321+
316322
@Override
317323
public void flushDb() {
318324
delegate.flushDb();
319325
}
320326

327+
@Override
328+
public void flushDb(FlushOption option) {
329+
delegate.flushDb(option);
330+
}
331+
321332
@Override
322333
public byte[] get(byte[] key) {
323334
return convertAndReturn(delegate.get(key), Converters.identityConverter());

src/main/java/org/springframework/data/redis/connection/DefaultedRedisClusterConnection.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
/**
2828
* @author Christoph Strobl
2929
* @author Mark Paluch
30+
* @author Dennis Neufeld
3031
* @since 2.0
3132
*/
3233
public interface DefaultedRedisClusterConnection extends RedisClusterConnection, DefaultedRedisConnection {
@@ -73,13 +74,27 @@ default void flushDb(RedisClusterNode node) {
7374
serverCommands().flushDb(node);
7475
}
7576

77+
/** @deprecated in favor of {@link RedisConnection#serverCommands()}. */
78+
@Override
79+
@Deprecated
80+
default void flushDb(RedisClusterNode node, FlushOption option) {
81+
serverCommands().flushDb(node, option);
82+
}
83+
7684
/** @deprecated in favor of {@link RedisConnection#serverCommands()}. */
7785
@Override
7886
@Deprecated
7987
default void flushAll(RedisClusterNode node) {
8088
serverCommands().flushAll(node);
8189
}
8290

91+
/** @deprecated in favor of {@link RedisConnection#serverCommands()}. */
92+
@Override
93+
@Deprecated
94+
default void flushAll(RedisClusterNode node, FlushOption option) {
95+
serverCommands().flushAll(node, option);
96+
}
97+
8398
/** @deprecated in favor of {@link RedisConnection#serverCommands()}. */
8499
@Override
85100
@Deprecated

src/main/java/org/springframework/data/redis/connection/DefaultedRedisConnection.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
* @author Andrey Shlykov
6161
* @author dengliming
6262
* @author ihaohong
63+
* @author Dennis Neufeld
6364
* @since 2.0
6465
*/
6566
public interface DefaultedRedisConnection extends RedisConnection {
@@ -1632,13 +1633,27 @@ default void flushDb() {
16321633
serverCommands().flushDb();
16331634
}
16341635

1636+
/** @deprecated in favor of {@link RedisConnection#serverCommands()}. */
1637+
@Override
1638+
@Deprecated
1639+
default void flushDb(FlushOption option) {
1640+
serverCommands().flushDb(option);
1641+
}
1642+
16351643
/** @deprecated in favor of {@link RedisConnection#serverCommands()}. */
16361644
@Override
16371645
@Deprecated
16381646
default void flushAll() {
16391647
serverCommands().flushAll();
16401648
}
16411649

1650+
/** @deprecated in favor of {@link RedisConnection#serverCommands()}. */
1651+
@Override
1652+
@Deprecated
1653+
default void flushAll(FlushOption option) {
1654+
serverCommands().flushAll(option);
1655+
}
1656+
16421657
/** @deprecated in favor of {@link RedisConnection#serverCommands()}. */
16431658
@Override
16441659
@Deprecated

src/main/java/org/springframework/data/redis/connection/ReactiveClusterServerCommands.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020

2121
import java.util.Properties;
2222

23+
import org.springframework.data.redis.connection.RedisServerCommands.FlushOption;
2324
import org.springframework.data.redis.core.types.RedisClientInfo;
2425

2526
/**
2627
* Redis Server commands executed in cluster environment using reactive infrastructure.
2728
*
2829
* @author Mark Paluch
2930
* @author Christoph Strobl
31+
* @author Dennis Neufeld
3032
* @since 2.0
3133
*/
3234
public interface ReactiveClusterServerCommands extends ReactiveServerCommands {
@@ -91,6 +93,17 @@ public interface ReactiveClusterServerCommands extends ReactiveServerCommands {
9193
*/
9294
Mono<String> flushDb(RedisClusterNode node);
9395

96+
/**
97+
* Delete all keys of the currently selected database using the specified flush option.
98+
*
99+
* @param node must not be {@literal null}. {@link Mono} indicating command completion.
100+
* @param option
101+
* @throws IllegalArgumentException when {@code node} is {@literal null}.
102+
* @see RedisServerCommands#flushDb(FlushOption)
103+
* @since 2.6
104+
*/
105+
Mono<String> flushDb(RedisClusterNode node, FlushOption option);
106+
94107
/**
95108
* Delete all <b>all keys</b> from <b>all databases</b>.
96109
*
@@ -101,6 +114,18 @@ public interface ReactiveClusterServerCommands extends ReactiveServerCommands {
101114
*/
102115
Mono<String> flushAll(RedisClusterNode node);
103116

117+
/**
118+
* Delete all <b>all keys</b> from <b>all databases</b> using the specified flush option.
119+
*
120+
* @param node must not be {@literal null}.
121+
* @param option
122+
* @return {@link Mono} indicating command completion.
123+
* @throws IllegalArgumentException when {@code node} is {@literal null}.
124+
* @see RedisServerCommands#flushAll(FlushOption)
125+
* @since 2.6
126+
*/
127+
Mono<String> flushAll(RedisClusterNode node, FlushOption option);
128+
104129
/**
105130
* Load {@literal default} server information like
106131
* <ul>

src/main/java/org/springframework/data/redis/connection/ReactiveServerCommands.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
import java.util.Properties;
2222
import java.util.concurrent.TimeUnit;
2323

24+
import org.springframework.data.redis.connection.RedisServerCommands.FlushOption;
2425
import org.springframework.data.redis.core.types.RedisClientInfo;
2526

2627
/**
2728
* Redis Server commands executed using reactive infrastructure.
2829
*
2930
* @author Mark Paluch
3031
* @author Christoph Strobl
32+
* @author Dennis Neufeld
3133
* @since 2.0
3234
*/
3335
public interface ReactiveServerCommands {
@@ -81,6 +83,16 @@ public interface ReactiveServerCommands {
8183
*/
8284
Mono<String> flushDb();
8385

86+
/**
87+
* Delete all keys of the currently selected database using the specified flush option.
88+
*
89+
* @param option
90+
* @return {@link Mono} indicating command completion.
91+
* @see <a href="https://redis.io/commands/flushdb">Redis Documentation: FLUSHDB</a>
92+
* @since 2.6
93+
*/
94+
Mono<String> flushDb(FlushOption option);
95+
8496
/**
8597
* Delete all <b>all keys</b> from <b>all databases</b>.
8698
*
@@ -89,6 +101,16 @@ public interface ReactiveServerCommands {
89101
*/
90102
Mono<String> flushAll();
91103

104+
/**
105+
* Delete all <b>all keys</b> from <b>all databases</b> using the specified flush option.
106+
*
107+
* @param option
108+
* @return {@link Mono} indicating command completion.
109+
* @see <a href="https://redis.io/commands/flushall">Redis Documentation: FLUSHALL</a>
110+
* @since 2.6
111+
*/
112+
Mono<String> flushAll(FlushOption option);
113+
92114
/**
93115
* Load {@literal default} server information like
94116
* <ul>

src/main/java/org/springframework/data/redis/connection/RedisClusterServerCommands.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
/**
2525
* @author Mark Paluch
26+
* @author Dennis Neufeld
2627
* @since 2.0
2728
*/
2829
public interface RedisClusterServerCommands extends RedisServerCommands {
@@ -65,12 +66,28 @@ public interface RedisClusterServerCommands extends RedisServerCommands {
6566
*/
6667
void flushDb(RedisClusterNode node);
6768

69+
/**
70+
* @param node must not be {@literal null}.
71+
* @param option
72+
* @see RedisServerCommands#flushDb(FlushOption)
73+
* @since 2.6
74+
*/
75+
void flushDb(RedisClusterNode node, FlushOption option);
76+
6877
/**
6978
* @param node must not be {@literal null}.
7079
* @see RedisServerCommands#flushAll()
7180
*/
7281
void flushAll(RedisClusterNode node);
7382

83+
/**
84+
* @param node must not be {@literal null}.
85+
* @param option
86+
* @see RedisServerCommands#flushAll(FlushOption)
87+
* @since 2.6
88+
*/
89+
void flushAll(RedisClusterNode node, FlushOption option);
90+
7491
/**
7592
* @param node must not be {@literal null}.
7693
* @return

src/main/java/org/springframework/data/redis/connection/RedisServerCommands.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* @author Christoph Strobl
3030
* @author Thomas Darimont
3131
* @author Mark Paluch
32+
* @author Dennis Neufeld
3233
*/
3334
public interface RedisServerCommands {
3435

@@ -43,6 +44,13 @@ enum MigrateOption {
4344
COPY, REPLACE
4445
}
4546

47+
/**
48+
* @since 2.6
49+
*/
50+
enum FlushOption {
51+
SYNC, ASYNC
52+
}
53+
4654
/**
4755
* Start an {@literal Append Only File} rewrite process on server.
4856
*
@@ -101,13 +109,31 @@ default void bgWriteAof() {
101109
*/
102110
void flushDb();
103111

112+
/**
113+
* Delete all keys of the currently selected database using the specified flush option.
114+
*
115+
* @param option
116+
* @see <a href="https://redis.io/commands/flushdb">Redis Documentation: FLUSHDB</a>
117+
* @since 2.6
118+
*/
119+
void flushDb(FlushOption option);
120+
104121
/**
105122
* Delete all <b>all keys</b> from <b>all databases</b>.
106123
*
107124
* @see <a href="https://redis.io/commands/flushall">Redis Documentation: FLUSHALL</a>
108125
*/
109126
void flushAll();
110127

128+
/**
129+
* Delete all <b>all keys</b> from <b>all databases</b> using the specified flush option.
130+
*
131+
* @param option
132+
* @see <a href="https://redis.io/commands/flushall">Redis Documentation: FLUSHALL</a>
133+
* @since 2.6
134+
*/
135+
void flushAll(FlushOption option);
136+
111137
/**
112138
* Load {@literal default} server information like
113139
* <ul>

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterServerCommands.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import redis.clients.jedis.BinaryJedis;
1919
import redis.clients.jedis.Jedis;
20+
import redis.clients.jedis.args.FlushMode;
2021

2122
import java.util.ArrayList;
2223
import java.util.Collection;
@@ -41,6 +42,7 @@
4142

4243
/**
4344
* @author Mark Paluch
45+
* @author Dennis Neufeld
4446
* @since 2.0
4547
*/
4648
class JedisClusterServerCommands implements RedisClusterServerCommands {
@@ -127,22 +129,43 @@ public void flushDb() {
127129
executeCommandOnAllNodes(BinaryJedis::flushDB);
128130
}
129131

132+
@Override
133+
public void flushDb(FlushOption option) {
134+
executeCommandOnAllNodes(it -> it.flushDB(toFlushMode(option)));
135+
}
136+
130137
@Override
131138
public void flushDb(RedisClusterNode node) {
132139
executeCommandOnSingleNode(BinaryJedis::flushDB, node);
133140
}
134141

142+
@Override
143+
public void flushDb(RedisClusterNode node, FlushOption option) {
144+
executeCommandOnSingleNode(it -> it.flushDB(toFlushMode(option)), node);
145+
}
146+
135147
@Override
136148
public void flushAll() {
137149
connection.getClusterCommandExecutor()
138150
.executeCommandOnAllNodes((JedisClusterCommandCallback<String>) BinaryJedis::flushAll);
139151
}
140152

153+
@Override
154+
public void flushAll(FlushOption option) {
155+
connection.getClusterCommandExecutor()
156+
.executeCommandOnAllNodes((JedisClusterCommandCallback<String>) it -> it.flushAll(toFlushMode(option)));
157+
}
158+
141159
@Override
142160
public void flushAll(RedisClusterNode node) {
143161
executeCommandOnSingleNode(BinaryJedis::flushAll, node);
144162
}
145163

164+
@Override
165+
public void flushAll(RedisClusterNode node, FlushOption option) {
166+
executeCommandOnSingleNode(it -> it.flushAll(toFlushMode(option)), node);
167+
}
168+
146169
@Override
147170
public Properties info() {
148171

@@ -395,4 +418,19 @@ private <T> NodeResult<T> executeCommandOnSingleNode(JedisClusterCommandCallback
395418
private <T> MultiNodeResult<T> executeCommandOnAllNodes(JedisClusterCommandCallback<T> cmd) {
396419
return connection.getClusterCommandExecutor().executeCommandOnAllNodes(cmd);
397420
}
421+
422+
static FlushMode toFlushMode(@Nullable FlushOption option) {
423+
424+
if (option == null) {
425+
return FlushMode.SYNC;
426+
}
427+
428+
switch (option) {
429+
case ASYNC:
430+
return FlushMode.ASYNC;
431+
case SYNC:
432+
return FlushMode.SYNC;
433+
}
434+
throw new UnsupportedOperationException("Flush option " + option + " is not implemented.");
435+
}
398436
}

0 commit comments

Comments
 (0)