public class LettuceConnectionFactory extends Object implements InitializingBean, DisposableBean, RedisConnectionFactory, ReactiveRedisConnectionFactory
This factory creates a new LettuceConnection on each call to getConnection(). Multiple
LettuceConnections share a single thread-safe native connection by default.
The shared native connection is never closed by LettuceConnection, therefore it is not validated by default
on getConnection(). Use setValidateConnection(boolean) to change this behavior if necessary. Inject
a 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.
LettuceConnectionFactory should be configured using an environmental configuration and the
client configuration. Lettuce supports the following environmental configurations:
| Modifier and Type | Method and Description |
|---|---|
void |
afterPropertiesSet() |
protected io.lettuce.core.AbstractRedisClient |
createClient() |
void |
destroy() |
protected LettuceConnectionProvider |
doCreateConnectionProvider(io.lettuce.core.AbstractRedisClient client,
io.lettuce.core.codec.RedisCodec<?,?> codec)
|
protected LettuceClusterConnection |
doCreateLettuceClusterConnection(io.lettuce.core.cluster.api.StatefulRedisClusterConnection<byte[],byte[]> sharedConnection,
LettuceConnectionProvider connectionProvider,
ClusterTopologyProvider topologyProvider,
ClusterCommandExecutor clusterCommandExecutor,
Duration commandTimeout)
Customization hook for
LettuceClusterConnection creation. |
protected LettuceConnection |
doCreateLettuceConnection(io.lettuce.core.api.StatefulRedisConnection<byte[],byte[]> sharedConnection,
LettuceConnectionProvider connectionProvider,
long timeout,
int database)
Customization hook for
LettuceConnection creation. |
LettuceClientConfiguration |
getClientConfiguration() |
String |
getClientName()
Returns the client name.
|
io.lettuce.core.resource.ClientResources |
getClientResources()
Get the
ClientResources to reuse infrastructure. |
RedisClusterConfiguration |
getClusterConfiguration() |
RedisClusterConnection |
getClusterConnection()
Provides a suitable connection for interacting with Redis Cluster.
|
RedisConnection |
getConnection()
Provides a suitable connection for interacting with Redis.
|
boolean |
getConvertPipelineAndTxResults()
Specifies if pipelined results should be converted to the expected data type.
|
int |
getDatabase()
Returns the index of the database.
|
boolean |
getEagerInitialization()
Indicates
shared connections should be eagerly initialized. |
String |
getHostName()
Returns the current host.
|
String |
getPassword()
Returns the password used for authenticating with the Redis server.
|
int |
getPort()
Returns the current port.
|
org.springframework.data.redis.connection.lettuce.LettuceReactiveRedisClusterConnection |
getReactiveClusterConnection() |
org.springframework.data.redis.connection.lettuce.LettuceReactiveRedisConnection |
getReactiveConnection() |
RedisSentinelConfiguration |
getSentinelConfiguration() |
RedisSentinelConnection |
getSentinelConnection()
Provides a suitable connection for interacting with Redis Sentinel.
|
protected io.lettuce.core.api.StatefulRedisConnection<byte[],byte[]> |
getSharedConnection() |
protected io.lettuce.core.api.StatefulConnection<ByteBuffer,ByteBuffer> |
getSharedReactiveConnection() |
boolean |
getShareNativeConnection()
Indicates if multiple
LettuceConnections should share a single native connection. |
long |
getShutdownTimeout()
Returns the shutdown timeout for shutting down the RedisClient (in milliseconds).
|
RedisSocketConfiguration |
getSocketConfiguration() |
RedisStandaloneConfiguration |
getStandaloneConfiguration() |
long |
getTimeout()
Returns the connection timeout (in milliseconds).
|
boolean |
getValidateConnection()
Indicates if validation of the native Lettuce connection is enabled.
|
void |
initConnection()
Initialize the shared connection if
native connection sharing is enabled and
reset any previously existing connection. |
boolean |
isClusterAware() |
boolean |
isRedisSentinelAware() |
boolean |
isStartTls()
Returns whether to issue a StartTLS.
|
boolean |
isUseSsl()
Returns whether to use SSL.
|
boolean |
isVerifyPeer()
Returns whether to verify certificate validity/hostname check when SSL is used.
|
void |
resetConnection()
Reset the underlying shared Connection, to be reinitialized on next access.
|
void |
setClientName(String clientName)
Deprecated.
configure the client name using
LettuceClientConfiguration. |
void |
setClientResources(io.lettuce.core.resource.ClientResources clientResources)
Deprecated.
since 2.0, configure
ClientResources using LettuceClientConfiguration. |
void |
setConvertPipelineAndTxResults(boolean convertPipelineAndTxResults)
Specifies if pipelined and transaction results should be converted to the expected data type.
|
void |
setDatabase(int index)
Sets the index of the database used by this connection factory.
|
void |
setEagerInitialization(boolean eagerInitialization)
Enables eager initialization of
shared connections. |
void |
setHostName(String hostName)
Deprecated.
since 2.0, configure the hostname using
RedisStandaloneConfiguration. |
void |
setPassword(String password)
Deprecated.
since 2.0, configure the password using
RedisStandaloneConfiguration,
RedisSentinelConfiguration or RedisClusterConfiguration. |
void |
setPipeliningFlushPolicy(LettuceConnection.PipeliningFlushPolicy pipeliningFlushPolicy)
Configures the flushing policy when using pipelining.
|
void |
setPort(int port)
Deprecated.
since 2.0, configure the port using
RedisStandaloneConfiguration. |
void |
setShareNativeConnection(boolean shareNativeConnection)
Enables multiple
LettuceConnections to share a single native connection. |
void |
setShutdownTimeout(long shutdownTimeout)
Deprecated.
since 2.0, configure the shutdown timeout using
LettuceClientConfiguration. |
void |
setStartTls(boolean startTls)
Deprecated.
since 2.0, configure StartTLS using
LettuceClientConfiguration. |
void |
setTimeout(long timeout)
Deprecated.
since 2.0, configure the timeout using
LettuceClientConfiguration. |
void |
setUseSsl(boolean useSsl)
Deprecated.
since 2.0, configure SSL usage using
LettuceClientConfiguration. |
void |
setValidateConnection(boolean validateConnection)
Enables validation of the shared native Lettuce connection on calls to
getConnection(). |
void |
setVerifyPeer(boolean verifyPeer)
Deprecated.
since 2.0, configure peer verification using
LettuceClientConfiguration. |
DataAccessException |
translateExceptionIfPossible(RuntimeException ex) |
void |
validateConnection()
Validate the shared connections and reinitialize if invalid.
|
public LettuceConnectionFactory()
LettuceConnectionFactory instance with default settings.public LettuceConnectionFactory(RedisStandaloneConfiguration configuration)
LettuceConnectionFactory instance with default settings.public LettuceConnectionFactory(String host, int port)
LettuceConnectionFactory instance with default settings.public LettuceConnectionFactory(RedisConfiguration redisConfiguration)
LettuceConnectionFactory instance using the given RedisSocketConfiguration.redisConfiguration - must not be null.public LettuceConnectionFactory(RedisSentinelConfiguration sentinelConfiguration)
LettuceConnectionFactory instance using the given RedisSentinelConfiguration.sentinelConfiguration - must not be null.public LettuceConnectionFactory(RedisClusterConfiguration clusterConfiguration)
LettuceConnectionFactory instance using the given RedisClusterConfiguration
applied to create a RedisClusterClient.clusterConfiguration - must not be null.@Deprecated public LettuceConnectionFactory(LettucePool pool)
LettucePoolingClientConfiguration.pool - public LettuceConnectionFactory(RedisStandaloneConfiguration standaloneConfig, LettuceClientConfiguration clientConfig)
LettuceConnectionFactory instance using the given RedisStandaloneConfiguration and
LettuceClientConfiguration.standaloneConfig - must not be null.clientConfig - must not be null.public LettuceConnectionFactory(RedisConfiguration redisConfiguration, LettuceClientConfiguration clientConfig)
LettuceConnectionFactory instance using the given
RedisStaticMasterReplicaConfiguration and LettuceClientConfiguration.redisConfiguration - must not be null.clientConfig - must not be null.public LettuceConnectionFactory(RedisSentinelConfiguration sentinelConfiguration, LettuceClientConfiguration clientConfig)
LettuceConnectionFactory instance using the given RedisSentinelConfiguration and
LettuceClientConfiguration.sentinelConfiguration - must not be null.clientConfig - must not be null.public LettuceConnectionFactory(RedisClusterConfiguration clusterConfiguration, LettuceClientConfiguration clientConfig)
LettuceConnectionFactory instance using the given RedisClusterConfiguration and
LettuceClientConfiguration.clusterConfiguration - must not be null.clientConfig - must not be null.public void afterPropertiesSet()
afterPropertiesSet in interface InitializingBeanpublic void destroy()
destroy in interface DisposableBeanpublic RedisConnection getConnection()
RedisConnectionFactorygetConnection in interface RedisConnectionFactorypublic RedisClusterConnection getClusterConnection()
RedisConnectionFactorygetClusterConnection in interface RedisConnectionFactoryprotected LettuceConnection doCreateLettuceConnection(@Nullable io.lettuce.core.api.StatefulRedisConnection<byte[],byte[]> sharedConnection, LettuceConnectionProvider connectionProvider, long timeout, int database)
LettuceConnection creation.sharedConnection - the shared StatefulRedisConnection if getShareNativeConnection() is
true; null otherwise.connectionProvider - the LettuceConnectionProvider to release connections.timeout - command timeout in TimeUnit.MILLISECONDS.database - database index to operate on.LettuceConnection.IllegalArgumentException - if a required parameter is null.protected LettuceClusterConnection doCreateLettuceClusterConnection(@Nullable io.lettuce.core.cluster.api.StatefulRedisClusterConnection<byte[],byte[]> sharedConnection, LettuceConnectionProvider connectionProvider, ClusterTopologyProvider topologyProvider, ClusterCommandExecutor clusterCommandExecutor, Duration commandTimeout)
LettuceClusterConnection creation.sharedConnection - the shared StatefulRedisConnection if getShareNativeConnection() is
true; null otherwise.connectionProvider - the LettuceConnectionProvider to release connections.topologyProvider - the ClusterTopologyProvider.clusterCommandExecutor - the ClusterCommandExecutor to release connections.commandTimeout - command timeout Duration.LettuceConnection.IllegalArgumentException - if a required parameter is null.public org.springframework.data.redis.connection.lettuce.LettuceReactiveRedisConnection getReactiveConnection()
getReactiveConnection in interface ReactiveRedisConnectionFactorypublic org.springframework.data.redis.connection.lettuce.LettuceReactiveRedisClusterConnection getReactiveClusterConnection()
getReactiveClusterConnection in interface ReactiveRedisConnectionFactorypublic void initConnection()
native connection sharing is enabled and
reset any previously existing connection.public void resetConnection()
public void validateConnection()
public DataAccessException translateExceptionIfPossible(RuntimeException ex)
translateExceptionIfPossible in interface PersistenceExceptionTranslatorpublic String getHostName()
@Deprecated public void setHostName(String hostName)
RedisStandaloneConfiguration.hostName - the hostname to set.public int getPort()
@Deprecated public void setPort(int port)
RedisStandaloneConfiguration.port - the port to set.public void setPipeliningFlushPolicy(LettuceConnection.PipeliningFlushPolicy pipeliningFlushPolicy)
flush on each command.pipeliningFlushPolicy - the flushing policy to control when commands get written to the Redis connection.LettuceConnection.openPipeline(),
StatefulConnection.flushCommands()public long getTimeout()
@Deprecated public void setTimeout(long timeout)
LettuceClientConfiguration.timeout - the timeout.IllegalStateException - if LettuceClientConfiguration is immutable.public boolean isUseSsl()
@Deprecated public void setUseSsl(boolean useSsl)
LettuceClientConfiguration.useSsl - true to use SSL.IllegalStateException - if LettuceClientConfiguration is immutable.public boolean isVerifyPeer()
@Deprecated public void setVerifyPeer(boolean verifyPeer)
LettuceClientConfiguration.verifyPeer - false not to verify hostname.IllegalStateException - if LettuceClientConfiguration is immutable.public boolean isStartTls()
@Deprecated public void setStartTls(boolean startTls)
LettuceClientConfiguration.startTls - true to issue StartTLS.IllegalStateException - if LettuceClientConfiguration is immutable.public boolean getValidateConnection()
public void setValidateConnection(boolean validateConnection)
getConnection(). A new connection
will be created and used if validation fails.
Lettuce will automatically reconnect until close is called, which should never happen through
LettuceConnection if a shared native connection is used, therefore the default is false.
Setting this to true will result in a round-trip call to the server on each new connection, so this setting should only be used if connection sharing is enabled and there is code that is actively closing the native Lettuce connection.
validateConnection - enable connection validation.public boolean getShareNativeConnection()
LettuceConnections should share a single native connection.public void setShareNativeConnection(boolean shareNativeConnection)
LettuceConnections to share a single native connection. If set to false, every
operation on LettuceConnection will open and close a socket.shareNativeConnection - enable connection sharing.public boolean getEagerInitialization()
shared connections should be eagerly initialized. Eager
initialization requires a running Redis instance during application startup to allow early validation of connection
factory configuration. Eager initialization also prevents blocking connect while using reactive API and is
recommended for reactive API usage.true if the shared connection is initialized upon afterPropertiesSet().public void setEagerInitialization(boolean eagerInitialization)
shared connections.eagerInitialization - enable eager connection shared connection initialization upon
afterPropertiesSet().public int getDatabase()
public void setDatabase(int index)
index - database index@Nullable public String getClientName()
@Deprecated public void setClientName(@Nullable String clientName)
LettuceClientConfiguration.clientName - the client name. Can be null.IllegalStateException - if LettuceClientConfiguration is immutable.@Nullable public String getPassword()
@Deprecated public void setPassword(String password)
RedisStandaloneConfiguration,
RedisSentinelConfiguration or RedisClusterConfiguration.password - the password to setpublic long getShutdownTimeout()
@Deprecated public void setShutdownTimeout(long shutdownTimeout)
LettuceClientConfiguration.shutdownTimeout - the shutdown timeout.IllegalStateException - if LettuceClientConfiguration is immutable.public io.lettuce.core.resource.ClientResources getClientResources()
ClientResources to reuse infrastructure.@Deprecated public void setClientResources(io.lettuce.core.resource.ClientResources clientResources)
ClientResources using LettuceClientConfiguration.ClientResources to reuse the client infrastructure. clientResources - can be null.IllegalStateException - if LettuceClientConfiguration is immutable.public LettuceClientConfiguration getClientConfiguration()
LettuceClientConfiguration.public RedisStandaloneConfiguration getStandaloneConfiguration()
RedisStandaloneConfiguration.@Nullable public RedisSocketConfiguration getSocketConfiguration()
RedisSocketConfiguration or null if not set.@Nullable public RedisSentinelConfiguration getSentinelConfiguration()
RedisSentinelConfiguration, may be null.@Nullable public RedisClusterConfiguration getClusterConfiguration()
RedisClusterConfiguration, may be null.public boolean getConvertPipelineAndTxResults()
LettuceConnection.closePipeline() and {LettuceConnection#exec()} will be of the type returned by the
Lettuce driver.getConvertPipelineAndTxResults in interface RedisConnectionFactorypublic void setConvertPipelineAndTxResults(boolean convertPipelineAndTxResults)
LettuceConnection.closePipeline() and {LettuceConnection#exec()} will be of the type returned by the
Lettuce driver.convertPipelineAndTxResults - Whether or not to convert pipeline and tx results.public boolean isRedisSentinelAware()
RedisSentinelConfiguration is present.public boolean isClusterAware()
RedisClusterConfiguration is present.@Nullable protected io.lettuce.core.api.StatefulRedisConnection<byte[],byte[]> getSharedConnection()
connection sharing is disabled.@Nullable protected io.lettuce.core.api.StatefulConnection<ByteBuffer,ByteBuffer> getSharedReactiveConnection()
ByteBuffer encoding for reactive API use. null if
connection sharing is disabled.protected LettuceConnectionProvider doCreateConnectionProvider(io.lettuce.core.AbstractRedisClient client, io.lettuce.core.codec.RedisCodec<?,?> codec)
LettuceConnectionProvider given AbstractRedisClient and RedisCodec. Configuration
of this connection factory specifies the type of the created connection provider. This method creates either a
LettuceConnectionProvider for either RedisClient or RedisClusterClient. Subclasses may
override this method to decorate the connection provider.client - either RedisClient or RedisClusterClient, must not be null.codec - used for connection creation, must not be null. By default, a byte[] codec.
Reactive connections require a ByteBuffer codec.protected io.lettuce.core.AbstractRedisClient createClient()
public RedisSentinelConnection getSentinelConnection()
RedisConnectionFactorygetSentinelConnection in interface RedisConnectionFactoryCopyright © 2011–2021 Pivotal Software, Inc.. All rights reserved.