Polishing.

Tweak Javadoc. Make tests less strict for random values test.

Original Pull Request: #2276
This commit is contained in:
Mark Paluch
2022-02-24 15:25:04 +01:00
committed by Christoph Strobl
parent 4fdb5fd50f
commit d76c5be7da
4 changed files with 93 additions and 99 deletions

View File

@@ -71,10 +71,11 @@ import org.springframework.util.StringUtils;
* {@link LettuceConnection}s share a single thread-safe native connection by default.
* <p>
* The shared native connection is never closed by {@link LettuceConnection}, therefore it is not validated by default
* on {@link #getConnection()}. Use {@link #setValidateConnection(boolean)} to change this behavior if necessary. Inject
* a {@link Pool} to pool dedicated connections. If shareNativeConnection is true, the pool will be used to select a
* connection for blocking and tx operations only, which should not share a connection. If native connection sharing is
* disabled, the selected connection will be used for all operations.
* on {@link #getConnection()}. Use {@link #setValidateConnection(boolean)} to change this behavior if necessary. If
* {@code shareNativeConnection} is {@literal true}, a shared connection will be used for regular operations and a
* {@link LettuceConnectionProvider} will be used to select a connection for blocking and tx operations only, which
* should not share a connection. If native connection sharing is disabled, new (or pooled) connections will be used for
* all operations.
* <p>
* {@link LettuceConnectionFactory} should be configured using an environmental configuration and the
* {@link LettuceConnectionFactory client configuration}. Lettuce supports the following environmental configurations:

View File

@@ -195,7 +195,6 @@ public interface RedisOperations<K, V> {
@Nullable
Long countExistingKeys(Collection<K> keys);
/**
* Delete given {@code key}.
*
@@ -367,6 +366,26 @@ public interface RedisOperations<K, V> {
@Nullable
Boolean persist(K key);
/**
* Get the time to live for {@code key} in seconds.
*
* @param key must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @see <a href="https://redis.io/commands/ttl">Redis Documentation: TTL</a>
*/
@Nullable
Long getExpire(K key);
/**
* Get the time to live for {@code key} in and convert it to the given {@link TimeUnit}.
*
* @param key must not be {@literal null}.
* @param timeUnit must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @since 1.8
*/
@Nullable
Long getExpire(K key, TimeUnit timeUnit);
/**
* Move given {@code key} to database with {@code index}.
*
@@ -414,27 +433,6 @@ public interface RedisOperations<K, V> {
*/
void restore(K key, byte[] value, long timeToLive, TimeUnit unit, boolean replace);
/**
* Get the time to live for {@code key} in seconds.
*
* @param key must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @see <a href="https://redis.io/commands/ttl">Redis Documentation: TTL</a>
*/
@Nullable
Long getExpire(K key);
/**
* Get the time to live for {@code key} in and convert it to the given {@link TimeUnit}.
*
* @param key must not be {@literal null}.
* @param timeUnit must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @since 1.8
*/
@Nullable
Long getExpire(K key, TimeUnit timeUnit);
/**
* Sort the elements for {@code query}.
*

View File

@@ -562,6 +562,23 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return doWithKeys(connection -> connection.copy(sourceKey, targetKey, replace));
}
@Override
public Boolean hasKey(K key) {
byte[] rawKey = rawKey(key);
return doWithKeys(connection -> connection.exists(rawKey));
}
@Override
public Long countExistingKeys(Collection<K> keys) {
Assert.notNull(keys, "Keys must not be null!");
byte[][] rawKeys = rawKeys(keys);
return doWithKeys(connection -> connection.exists(rawKeys));
}
@Override
public Boolean delete(K key) {
@@ -606,20 +623,56 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public Boolean hasKey(K key) {
public DataType type(K key) {
byte[] rawKey = rawKey(key);
return doWithKeys(connection -> connection.exists(rawKey));
return doWithKeys(connection -> connection.type(rawKey));
}
@Override
public Long countExistingKeys(Collection<K> keys) {
@SuppressWarnings("unchecked")
public Set<K> keys(K pattern) {
Assert.notNull(keys, "Keys must not be null!");
byte[] rawKey = rawKey(pattern);
Set<byte[]> rawKeys = doWithKeys(connection -> connection.keys(rawKey));
byte[][] rawKeys = rawKeys(keys);
return doWithKeys(connection -> connection.exists(rawKeys));
return keySerializer != null ? SerializationUtils.deserialize(rawKeys, keySerializer) : (Set<K>) rawKeys;
}
@Override
public Cursor<K> scan(ScanOptions options) {
Assert.notNull(options, "ScanOptions must not be null!");
return executeWithStickyConnection(
(RedisCallback<Cursor<K>>) connection -> new ConvertingCursor<>(connection.scan(options),
this::deserializeKey));
}
@Override
public K randomKey() {
byte[] rawKey = doWithKeys(RedisKeyCommands::randomKey);
return deserializeKey(rawKey);
}
@Override
public void rename(K oldKey, K newKey) {
byte[] rawOldKey = rawKey(oldKey);
byte[] rawNewKey = rawKey(newKey);
doWithKeys(connection -> {
connection.rename(rawOldKey, rawNewKey);
return null;
});
}
@Override
public Boolean renameIfAbsent(K oldKey, K newKey) {
byte[] rawOldKey = rawKey(oldKey);
byte[] rawNewKey = rawKey(newKey);
return doWithKeys(connection -> connection.renameNX(rawOldKey, rawNewKey));
}
@Override
@@ -652,9 +705,12 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
});
}
// -------------------------------------------------------------------------
// Methods dealing with value operations
// -------------------------------------------------------------------------
@Override
public Boolean persist(K key) {
byte[] rawKey = rawKey(key);
return doWithKeys(connection -> connection.persist(rawKey));
}
@Override
public Long getExpire(K key) {
@@ -664,7 +720,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public Long getExpire(K key, final TimeUnit timeUnit) {
public Long getExpire(K key, TimeUnit timeUnit) {
byte[] rawKey = rawKey(key);
return doWithKeys(connection -> {
@@ -677,32 +733,6 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
});
}
@Override
@SuppressWarnings("unchecked")
public Set<K> keys(K pattern) {
byte[] rawKey = rawKey(pattern);
Set<byte[]> rawKeys = doWithKeys(connection -> connection.keys(rawKey));
return keySerializer != null ? SerializationUtils.deserialize(rawKeys, keySerializer) : (Set<K>) rawKeys;
}
@Override
public Cursor<K> scan(ScanOptions options) {
Assert.notNull(options, "ScanOptions must not be null!");
return executeWithStickyConnection(
(RedisCallback<Cursor<K>>) connection -> new ConvertingCursor<>(connection.scan(options),
this::deserializeKey));
}
@Override
public Boolean persist(K key) {
byte[] rawKey = rawKey(key);
return doWithKeys(connection -> connection.persist(rawKey));
}
@Override
public Boolean move(K key, final int dbIndex) {
@@ -710,40 +740,6 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return doWithKeys(connection -> connection.move(rawKey, dbIndex));
}
@Override
public K randomKey() {
byte[] rawKey = doWithKeys(RedisKeyCommands::randomKey);
return deserializeKey(rawKey);
}
@Override
public void rename(K oldKey, K newKey) {
byte[] rawOldKey = rawKey(oldKey);
byte[] rawNewKey = rawKey(newKey);
doWithKeys(connection -> {
connection.rename(rawOldKey, rawNewKey);
return null;
});
}
@Override
public Boolean renameIfAbsent(K oldKey, K newKey) {
byte[] rawOldKey = rawKey(oldKey);
byte[] rawNewKey = rawKey(newKey);
return doWithKeys(connection -> connection.renameNX(rawOldKey, rawNewKey));
}
@Override
public DataType type(K key) {
byte[] rawKey = rawKey(key);
return doWithKeys(connection -> connection.type(rawKey));
}
/**
* Executes the Redis dump command and returns the results. Redis uses a non-standard serialization mechanism and
* includes checksum information, thus the raw bytes are returned as opposed to deserializing with valueSerializer.

View File

@@ -445,8 +445,7 @@ public interface StreamMessageListenerContainer<K, V extends Record<K, ?>> exten
}
/**
* Configure auto-acknowledgement for stream message consumption. This method is an alias for
* {@link #autoAck(boolean)} for improved readability.
* Configure auto-acknowledgement for stream message consumption.
*
* @param autoAck {@literal true} (default) to auto-acknowledge received messages or {@literal false} for external
* acknowledgement.