Skip to content

Commit b1e07f5

Browse files
committed
Polishing.
Replace subclass per command usage with lambdas. See #2897 Original pull request: #2900
1 parent 12d97be commit b1e07f5

File tree

2 files changed

+32
-56
lines changed

2 files changed

+32
-56
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.List;
2424
import java.util.Map;
2525
import java.util.Set;
26+
import java.util.function.BiFunction;
2627

2728
import org.springframework.data.geo.GeoResults;
2829
import org.springframework.data.redis.connection.RedisConnection;
@@ -51,7 +52,7 @@ abstract class AbstractOperations<K, V> {
5152

5253
// utility methods for the template internal methods
5354
abstract class ValueDeserializingRedisCallback implements RedisCallback<V> {
54-
private Object key;
55+
private final Object key;
5556

5657
public ValueDeserializingRedisCallback(Object key) {
5758
this.key = key;
@@ -66,12 +67,31 @@ public final V doInRedis(RedisConnection connection) {
6667
protected abstract byte[] inRedis(byte[] rawKey, RedisConnection connection);
6768
}
6869

70+
private class FunctionalValueDeserializingRedisCallback extends ValueDeserializingRedisCallback {
71+
72+
private final BiFunction<RedisConnection, byte[], byte[]> function;
73+
74+
public FunctionalValueDeserializingRedisCallback(Object key, BiFunction<RedisConnection, byte[], byte[]> function) {
75+
super(key);
76+
this.function = function;
77+
}
78+
79+
@Nullable
80+
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
81+
return function.apply(connection, rawKey);
82+
}
83+
}
84+
6985
final RedisTemplate<K, V> template;
7086

7187
AbstractOperations(RedisTemplate<K, V> template) {
7288
this.template = template;
7389
}
7490

91+
ValueDeserializingRedisCallback valueCallbackFor(Object key, BiFunction<RedisConnection, byte[], byte[]> function) {
92+
return new FunctionalValueDeserializingRedisCallback(key, function);
93+
}
94+
7595
RedisSerializer keySerializer() {
7696
return template.getKeySerializer();
7797
}

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

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.concurrent.TimeUnit;
25+
2526
import org.springframework.data.redis.connection.BitFieldSubCommands;
26-
import org.springframework.data.redis.connection.RedisConnection;
27+
import org.springframework.data.redis.connection.DefaultedRedisConnection;
2728
import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
2829
import org.springframework.data.redis.core.types.Expiration;
2930
import org.springframework.lang.Nullable;
@@ -45,79 +46,39 @@ class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements V
4546

4647
@Override
4748
public V get(Object key) {
48-
49-
return execute(new ValueDeserializingRedisCallback(key) {
50-
51-
@Override
52-
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
53-
return connection.get(rawKey);
54-
}
55-
});
49+
return execute(valueCallbackFor(key, DefaultedRedisConnection::get));
5650
}
5751

5852
@Nullable
5953
@Override
6054
public V getAndDelete(K key) {
61-
62-
return execute(new ValueDeserializingRedisCallback(key) {
63-
64-
@Override
65-
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
66-
return connection.getDel(rawKey);
67-
}
68-
});
55+
return execute(valueCallbackFor(key, DefaultedRedisConnection::getDel));
6956
}
7057

7158
@Nullable
7259
@Override
7360
public V getAndExpire(K key, long timeout, TimeUnit unit) {
74-
75-
return execute(new ValueDeserializingRedisCallback(key) {
76-
77-
@Override
78-
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
79-
return connection.getEx(rawKey, Expiration.from(timeout, unit));
80-
}
81-
});
61+
return execute(
62+
valueCallbackFor(key, (connection, rawKey) -> connection.getEx(rawKey, Expiration.from(timeout, unit))));
8263
}
8364

8465
@Nullable
8566
@Override
8667
public V getAndExpire(K key, Duration timeout) {
87-
88-
return execute(new ValueDeserializingRedisCallback(key) {
89-
90-
@Override
91-
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
92-
return connection.getEx(rawKey, Expiration.from(timeout));
93-
}
94-
});
68+
return execute(valueCallbackFor(key, (connection, rawKey) -> connection.getEx(rawKey, Expiration.from(timeout))));
9569
}
9670

9771
@Nullable
9872
@Override
9973
public V getAndPersist(K key) {
100-
101-
return execute(new ValueDeserializingRedisCallback(key) {
102-
103-
@Override
104-
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
105-
return connection.getEx(rawKey, Expiration.persistent());
106-
}
107-
});
74+
return execute(valueCallbackFor(key, (connection, rawKey) -> connection.getEx(rawKey, Expiration.persistent())));
10875
}
10976

11077
@Override
11178
public V getAndSet(K key, V newValue) {
11279

11380
byte[] rawValue = rawValue(newValue);
114-
return execute(new ValueDeserializingRedisCallback(key) {
115-
116-
@Override
117-
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
118-
return connection.getSet(rawKey, rawValue);
119-
}
120-
});
81+
return execute(valueCallbackFor(key, (connection, rawKey) -> connection.getSet(rawKey, rawValue)));
12182
}
12283

12384
@Override
@@ -232,15 +193,10 @@ public Boolean multiSetIfAbsent(Map<? extends K, ? extends V> m) {
232193
@Override
233194
public void set(K key, V value) {
234195

196+
byte[] rawKey = rawKey(key);
235197
byte[] rawValue = rawValue(value);
236-
execute(new ValueDeserializingRedisCallback(key) {
237198

238-
@Override
239-
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
240-
connection.set(rawKey, rawValue);
241-
return null;
242-
}
243-
});
199+
execute(connection -> connection.set(rawKey, rawValue));
244200
}
245201

246202
@Override

0 commit comments

Comments
 (0)