diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java index e84bcc244..38f439348 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java @@ -71,10 +71,11 @@ import org.springframework.util.StringUtils; * {@link LettuceConnection}s share a single thread-safe native connection by default. *

* 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. *

* {@link LettuceConnectionFactory} should be configured using an environmental configuration and the * {@link LettuceConnectionFactory client configuration}. Lettuce supports the following environmental configurations: diff --git a/src/main/java/org/springframework/data/redis/core/RedisOperations.java b/src/main/java/org/springframework/data/redis/core/RedisOperations.java index 0e51aff09..a013ca3a4 100644 --- a/src/main/java/org/springframework/data/redis/core/RedisOperations.java +++ b/src/main/java/org/springframework/data/redis/core/RedisOperations.java @@ -195,7 +195,6 @@ public interface RedisOperations { @Nullable Long countExistingKeys(Collection keys); - /** * Delete given {@code key}. * @@ -367,6 +366,26 @@ public interface RedisOperations { @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 Redis Documentation: TTL + */ + @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 { */ 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 Redis Documentation: TTL - */ - @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}. * diff --git a/src/main/java/org/springframework/data/redis/core/RedisTemplate.java b/src/main/java/org/springframework/data/redis/core/RedisTemplate.java index 30e491b68..fe665c648 100644 --- a/src/main/java/org/springframework/data/redis/core/RedisTemplate.java +++ b/src/main/java/org/springframework/data/redis/core/RedisTemplate.java @@ -562,6 +562,23 @@ public class RedisTemplate 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 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 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 keys) { + @SuppressWarnings("unchecked") + public Set keys(K pattern) { - Assert.notNull(keys, "Keys must not be null!"); + byte[] rawKey = rawKey(pattern); + Set rawKeys = doWithKeys(connection -> connection.keys(rawKey)); - byte[][] rawKeys = rawKeys(keys); - return doWithKeys(connection -> connection.exists(rawKeys)); + return keySerializer != null ? SerializationUtils.deserialize(rawKeys, keySerializer) : (Set) rawKeys; + } + + @Override + public Cursor scan(ScanOptions options) { + Assert.notNull(options, "ScanOptions must not be null!"); + + return executeWithStickyConnection( + (RedisCallback>) 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 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 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 extends RedisAccessor implements RedisOperation }); } - @Override - @SuppressWarnings("unchecked") - public Set keys(K pattern) { - - byte[] rawKey = rawKey(pattern); - Set rawKeys = doWithKeys(connection -> connection.keys(rawKey)); - - return keySerializer != null ? SerializationUtils.deserialize(rawKeys, keySerializer) : (Set) rawKeys; - } - - @Override - public Cursor scan(ScanOptions options) { - Assert.notNull(options, "ScanOptions must not be null!"); - - return executeWithStickyConnection( - (RedisCallback>) 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 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. diff --git a/src/main/java/org/springframework/data/redis/stream/StreamMessageListenerContainer.java b/src/main/java/org/springframework/data/redis/stream/StreamMessageListenerContainer.java index 62a9efa77..99991b83f 100644 --- a/src/main/java/org/springframework/data/redis/stream/StreamMessageListenerContainer.java +++ b/src/main/java/org/springframework/data/redis/stream/StreamMessageListenerContainer.java @@ -445,8 +445,7 @@ public interface StreamMessageListenerContainer> 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.