Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Replace subclass per command usage with lambdas.

See #2897
Original pull request: #2900
  • Loading branch information
mp911de committed Oct 11, 2024
1 parent 12d97be commit b1e07f5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;

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

// utility methods for the template internal methods
abstract class ValueDeserializingRedisCallback implements RedisCallback<V> {
private Object key;
private final Object key;

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

private class FunctionalValueDeserializingRedisCallback extends ValueDeserializingRedisCallback {

private final BiFunction<RedisConnection, byte[], byte[]> function;

public FunctionalValueDeserializingRedisCallback(Object key, BiFunction<RedisConnection, byte[], byte[]> function) {
super(key);
this.function = function;
}

@Nullable
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return function.apply(connection, rawKey);
}
}

final RedisTemplate<K, V> template;

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

ValueDeserializingRedisCallback valueCallbackFor(Object key, BiFunction<RedisConnection, byte[], byte[]> function) {
return new FunctionalValueDeserializingRedisCallback(key, function);
}

RedisSerializer keySerializer() {
return template.getKeySerializer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.connection.BitFieldSubCommands;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.DefaultedRedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.lang.Nullable;
Expand All @@ -45,79 +46,39 @@ class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements V

@Override
public V get(Object key) {

return execute(new ValueDeserializingRedisCallback(key) {

@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.get(rawKey);
}
});
return execute(valueCallbackFor(key, DefaultedRedisConnection::get));
}

@Nullable
@Override
public V getAndDelete(K key) {

return execute(new ValueDeserializingRedisCallback(key) {

@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.getDel(rawKey);
}
});
return execute(valueCallbackFor(key, DefaultedRedisConnection::getDel));
}

@Nullable
@Override
public V getAndExpire(K key, long timeout, TimeUnit unit) {

return execute(new ValueDeserializingRedisCallback(key) {

@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.getEx(rawKey, Expiration.from(timeout, unit));
}
});
return execute(
valueCallbackFor(key, (connection, rawKey) -> connection.getEx(rawKey, Expiration.from(timeout, unit))));
}

@Nullable
@Override
public V getAndExpire(K key, Duration timeout) {

return execute(new ValueDeserializingRedisCallback(key) {

@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.getEx(rawKey, Expiration.from(timeout));
}
});
return execute(valueCallbackFor(key, (connection, rawKey) -> connection.getEx(rawKey, Expiration.from(timeout))));
}

@Nullable
@Override
public V getAndPersist(K key) {

return execute(new ValueDeserializingRedisCallback(key) {

@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.getEx(rawKey, Expiration.persistent());
}
});
return execute(valueCallbackFor(key, (connection, rawKey) -> connection.getEx(rawKey, Expiration.persistent())));
}

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

byte[] rawValue = rawValue(newValue);
return execute(new ValueDeserializingRedisCallback(key) {

@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.getSet(rawKey, rawValue);
}
});
return execute(valueCallbackFor(key, (connection, rawKey) -> connection.getSet(rawKey, rawValue)));
}

@Override
Expand Down Expand Up @@ -232,15 +193,10 @@ public Boolean multiSetIfAbsent(Map<? extends K, ? extends V> m) {
@Override
public void set(K key, V value) {

byte[] rawKey = rawKey(key);
byte[] rawValue = rawValue(value);
execute(new ValueDeserializingRedisCallback(key) {

@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
connection.set(rawKey, rawValue);
return null;
}
});
execute(connection -> connection.set(rawKey, rawValue));
}

@Override
Expand Down

0 comments on commit b1e07f5

Please sign in to comment.