Skip to content

Commit ab47236

Browse files
committed
Implement flush modes in flushdb
See spring-projects#2187
1 parent bc50822 commit ab47236

22 files changed

+438
-0
lines changed

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

Lines changed: 10 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

@@ -410,6 +411,15 @@ public void flushDb() {
410411
delegate.flushDb();
411412
}
412413

414+
/*
415+
* (non-Javadoc)
416+
* @see org.springframework.data.redis.connection.RedisServerCommands#flushDb(org.springframework.data.redis.connection.RedisServerCommands.FlushOption)
417+
*/
418+
@Override
419+
public void flushDb(FlushOption option) {
420+
delegate.flushDb(option);
421+
}
422+
413423
/*
414424
* (non-Javadoc)
415425
* @see org.springframework.data.redis.connection.RedisStringCommands#get(byte[])

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

Lines changed: 8 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,6 +74,13 @@ 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

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

Lines changed: 8 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,6 +1633,13 @@ 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

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

Lines changed: 13 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
*

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

Lines changed: 12 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
*

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

Lines changed: 9 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,6 +66,14 @@ 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()

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

Lines changed: 17 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,6 +109,15 @@ 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
*

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

Lines changed: 35 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 {
@@ -171,6 +173,15 @@ public void flushDb() {
171173
executeCommandOnAllNodes(BinaryJedis::flushDB);
172174
}
173175

176+
/*
177+
* (non-Javadoc)
178+
* @see org.springframework.data.redis.connection.RedisServerCommands#flushDb(org.springframework.data.redis.connection.RedisServerCommands.FlushOption)
179+
*/
180+
@Override
181+
public void flushDb(FlushOption option) {
182+
executeCommandOnAllNodes(it -> it.flushDB(toFlushMode(option)));
183+
}
184+
174185
/*
175186
* (non-Javadoc)
176187
* @see org.springframework.data.redis.connection.RedisClusterServerCommands#flushDb(org.springframework.data.redis.connection.RedisClusterNode)
@@ -180,6 +191,15 @@ public void flushDb(RedisClusterNode node) {
180191
executeCommandOnSingleNode(BinaryJedis::flushDB, node);
181192
}
182193

194+
/*
195+
* (non-Javadoc)
196+
* @see org.springframework.data.redis.connection.RedisClusterServerCommands#flushDb(org.springframework.data.redis.connection.RedisClusterNode, org.springframework.data.redis.connection.RedisServerCommands.FlushOption)
197+
*/
198+
@Override
199+
public void flushDb(RedisClusterNode node, FlushOption option) {
200+
executeCommandOnSingleNode(it -> it.flushDB(toFlushMode(option)), node);
201+
}
202+
183203
/*
184204
* (non-Javadoc)
185205
* @see org.springframework.data.redis.connection.RedisServerCommands#flushAll()
@@ -555,4 +575,19 @@ private <T> NodeResult<T> executeCommandOnSingleNode(JedisClusterCommandCallback
555575
private <T> MultiNodeResult<T> executeCommandOnAllNodes(JedisClusterCommandCallback<T> cmd) {
556576
return connection.getClusterCommandExecutor().executeCommandOnAllNodes(cmd);
557577
}
578+
579+
static FlushMode toFlushMode(@Nullable FlushOption option) {
580+
581+
if (option == null) {
582+
return FlushMode.SYNC;
583+
}
584+
585+
switch (option) {
586+
case ASYNC:
587+
return FlushMode.ASYNC;
588+
case SYNC:
589+
return FlushMode.SYNC;
590+
}
591+
throw new UnsupportedOperationException("Flush option " + option + " is not implemented.");
592+
}
558593
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import redis.clients.jedis.BinaryJedis;
1919
import redis.clients.jedis.Jedis;
2020
import redis.clients.jedis.MultiKeyPipelineBase;
21+
import redis.clients.jedis.args.FlushMode;
2122
import redis.clients.jedis.args.SaveMode;
2223

2324
import java.util.List;
@@ -33,6 +34,7 @@
3334

3435
/**
3536
* @author Mark Paluch
37+
* @author Dennis Neufeld
3638
* @since 2.0
3739
*/
3840
class JedisServerCommands implements RedisServerCommands {
@@ -99,6 +101,17 @@ public void flushDb() {
99101
connection.invokeStatus().just(BinaryJedis::flushDB, MultiKeyPipelineBase::flushDB);
100102
}
101103

104+
/*
105+
* (non-Javadoc)
106+
* @see org.springframework.data.redis.connection.RedisServerCommands#flushDb(org.springframework.data.redis.connection.RedisServerCommands.FlushOption)
107+
*/
108+
@Override
109+
public void flushDb(FlushOption option) {
110+
111+
FlushMode flushMode = JedisClusterServerCommands.toFlushMode(option);
112+
connection.invokeStatus().just(it -> it.flushDB(flushMode), it -> it.flushDB(flushMode));
113+
}
114+
102115
/*
103116
* (non-Javadoc)
104117
* @see org.springframework.data.redis.connection.RedisServerCommands#flushAll()

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceClusterServerCommands.java

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

3939
/**
4040
* @author Mark Paluch
41+
* @author Dennis Neufeld
4142
* @since 2.0
4243
*/
4344
class LettuceClusterServerCommands extends LettuceServerCommands implements RedisClusterServerCommands {
@@ -124,6 +125,15 @@ public void flushDb() {
124125
executeCommandOnAllNodes(RedisServerCommands::flushdb);
125126
}
126127

128+
/*
129+
* (non-Javadoc)
130+
* @see org.springframework.data.redis.connection.lettuce.LettuceServerCommands#flushDb(org.springframework.data.redis.connection.RedisServerCommands.FlushOption)
131+
*/
132+
@Override
133+
public void flushDb(FlushOption option) {
134+
executeCommandOnAllNodes(it -> it.flushdb(toFlushMode(option)));
135+
}
136+
127137
/*
128138
* (non-Javadoc)
129139
* @see org.springframework.data.redis.connection.RedisClusterServerCommands#flushDb(org.springframework.data.redis.connection.RedisClusterNode)
@@ -133,6 +143,15 @@ public void flushDb(RedisClusterNode node) {
133143
executeCommandOnSingleNode(RedisServerCommands::flushdb, node);
134144
}
135145

146+
/*
147+
* (non-Javadoc)
148+
* @see org.springframework.data.redis.connection.RedisClusterServerCommands#flushDb(org.springframework.data.redis.connection.RedisClusterNode, org.springframework.data.redis.connection.RedisServerCommands.FlushOption)
149+
*/
150+
@Override
151+
public void flushDb(RedisClusterNode node, FlushOption option) {
152+
executeCommandOnSingleNode(it -> it.flushdb(toFlushMode(option)), node);
153+
}
154+
136155
/*
137156
* (non-Javadoc)
138157
* @see org.springframework.data.redis.connection.lettuce.LettuceServerCommands#flushAll()

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveClusterServerCommands.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.data.redis.connection.ClusterTopologyProvider;
4242
import org.springframework.data.redis.connection.ReactiveClusterServerCommands;
4343
import org.springframework.data.redis.connection.RedisClusterNode;
44+
import org.springframework.data.redis.connection.RedisServerCommands.FlushOption;
4445
import org.springframework.data.redis.core.types.RedisClientInfo;
4546
import org.springframework.data.redis.util.ByteUtils;
4647
import org.springframework.util.Assert;
@@ -50,6 +51,7 @@
5051
*
5152
* @author Mark Paluch
5253
* @author Christoph Strobl
54+
* @author Dennis Neufeld
5355
* @since 2.0
5456
*/
5557
class LettuceReactiveClusterServerCommands extends LettuceReactiveServerCommands
@@ -129,6 +131,15 @@ public Mono<String> flushDb(RedisClusterNode node) {
129131
return connection.execute(node, RedisServerReactiveCommands::flushdb).next();
130132
}
131133

134+
/*
135+
* (non-Javadoc)
136+
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#flushDb(org.springframework.data.redis.connection.RedisClusterNode, org.springframework.data.redis.connection.RedisServerCommands.FlushOption)
137+
*/
138+
@Override
139+
public Mono<String> flushDb(RedisClusterNode node, FlushOption option) {
140+
return connection.execute(node, it -> it.flushdb(LettuceServerCommands.toFlushMode(option))).next();
141+
}
142+
132143
/*
133144
* (non-Javadoc)
134145
* @see org.springframework.data.redis.connection.ReactiveClusterServerCommands#flushAll(org.springframework.data.redis.connection.RedisClusterNode)

0 commit comments

Comments
 (0)