Skip to content

Commit 4fdb5fd

Browse files
mp911dechristophstrobl
authored andcommitted
Introduce executeWithoutResult(…) method.
Original Pull Request: #2276
1 parent f571544 commit 4fdb5fd

File tree

2 files changed

+18
-56
lines changed

2 files changed

+18
-56
lines changed

src/main/java/org/springframework/data/redis/core/RedisTemplate.java

Lines changed: 16 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Map;
2727
import java.util.Set;
2828
import java.util.concurrent.TimeUnit;
29+
import java.util.function.Consumer;
2930
import java.util.function.Function;
3031

3132
import org.springframework.beans.factory.BeanClassLoaderAware;
@@ -552,20 +553,6 @@ protected <T> T postProcessResult(@Nullable T result, RedisConnection conn, bool
552553
// Methods dealing with Redis Keys
553554
// -------------------------------------------------------------------------
554555

555-
@Override
556-
public void convertAndSend(String channel, Object message) {
557-
558-
Assert.hasText(channel, "a non-empty channel is required");
559-
560-
byte[] rawChannel = rawString(channel);
561-
byte[] rawMessage = rawValue(message);
562-
563-
execute(connection -> {
564-
connection.publish(rawChannel, rawMessage);
565-
return null;
566-
}, true);
567-
}
568-
569556
@Override
570557
public Boolean copy(K source, K target, boolean replace) {
571558

@@ -801,7 +788,6 @@ private <T> T doWithKeys(Function<RedisKeyCommands, T> action) {
801788
return execute((RedisCallback<? extends T>) connection -> action.apply(connection.keyCommands()), true);
802789
}
803790

804-
805791
// -------------------------------------------------------------------------
806792
// Methods dealing with sorting
807793
// -------------------------------------------------------------------------
@@ -875,48 +861,31 @@ public void watch(K key) {
875861

876862
byte[] rawKey = rawKey(key);
877863

878-
execute(connection -> {
879-
connection.watch(rawKey);
880-
return null;
881-
}, true);
864+
executeWithoutResult(connection -> connection.watch(rawKey));
882865
}
883866

884867
@Override
885868
public void watch(Collection<K> keys) {
886869

887870
byte[][] rawKeys = rawKeys(keys);
888871

889-
execute(connection -> {
890-
connection.watch(rawKeys);
891-
return null;
892-
}, true);
872+
executeWithoutResult(connection -> connection.watch(rawKeys));
893873
}
894874

895875
@Override
896876
public void unwatch() {
897877

898-
execute(connection -> {
899-
connection.unwatch();
900-
return null;
901-
}, true);
878+
executeWithoutResult(RedisTxCommands::unwatch);
902879
}
903880

904-
905881
@Override
906882
public void multi() {
907-
execute(connection -> {
908-
connection.multi();
909-
return null;
910-
}, true);
883+
executeWithoutResult(RedisTxCommands::multi);
911884
}
912885

913886
@Override
914887
public void discard() {
915-
916-
execute(connection -> {
917-
connection.discard();
918-
return null;
919-
}, true);
888+
executeWithoutResult(RedisTxCommands::discard);
920889
}
921890

922891
/**
@@ -960,33 +929,20 @@ public List<RedisClientInfo> getClientList() {
960929

961930
@Override
962931
public void killClient(String host, int port) {
963-
964-
execute((RedisCallback<Void>) connection -> {
965-
connection.killClient(host, port);
966-
return null;
967-
});
932+
executeWithoutResult(connection -> connection.killClient(host, port));
968933
}
969934

970935
/*
971936
* @see org.springframework.data.redis.core.RedisOperations#replicaOf(java.lang.String, int)
972937
*/
973938
@Override
974939
public void replicaOf(String host, int port) {
975-
976-
execute((RedisCallback<Void>) connection -> {
977-
978-
connection.replicaOf(host, port);
979-
return null;
980-
});
940+
executeWithoutResult(connection -> connection.replicaOf(host, port));
981941
}
982942

983943
@Override
984944
public void replicaOfNoOne() {
985-
986-
execute((RedisCallback<Void>) connection -> {
987-
connection.replicaOfNoOne();
988-
return null;
989-
});
945+
executeWithoutResult(RedisServerCommands::replicaOfNoOne);
990946
}
991947

992948
@Override
@@ -997,8 +953,13 @@ public void convertAndSend(String channel, Object message) {
997953
byte[] rawChannel = rawString(channel);
998954
byte[] rawMessage = rawValue(message);
999955

1000-
execute(connection -> {
1001-
connection.publish(rawChannel, rawMessage);
956+
executeWithoutResult(connection -> connection.publish(rawChannel, rawMessage));
957+
}
958+
959+
private void executeWithoutResult(Consumer<RedisConnection> action) {
960+
execute(it -> {
961+
962+
action.accept(it);
1002963
return null;
1003964
}, true);
1004965
}

src/test/java/org/springframework/data/redis/support/collections/AbstractRedisCollectionUnitTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.jupiter.api.BeforeEach;
2525
import org.junit.jupiter.api.Test;
2626
import org.junit.jupiter.api.extension.ExtendWith;
27+
import org.mockito.Answers;
2728
import org.mockito.Mock;
2829
import org.mockito.junit.jupiter.MockitoExtension;
2930
import org.mockito.junit.jupiter.MockitoSettings;
@@ -46,7 +47,7 @@ class AbstractRedisCollectionUnitTests {
4647
@SuppressWarnings("rawtypes")//
4748
private RedisTemplate redisTemplateSpy;
4849
private @Mock RedisConnectionFactory connectionFactoryMock;
49-
private @Mock RedisConnection connectionMock;
50+
private @Mock(answer = Answers.RETURNS_MOCKS) RedisConnection connectionMock;
5051

5152
@SuppressWarnings({ "unchecked", "rawtypes" })
5253
@BeforeEach

0 commit comments

Comments
 (0)