public class LettuceConnectionFactory extends Object implements InitializingBean, DisposableBean, RedisConnectionFactory
This factory creates a new LettuceConnection
on each call to getConnection()
. Multiple
LettuceConnection
s 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.
Modifier and Type | Field and Description |
---|---|
static String |
PING_REPLY |
Constructor and Description |
---|
LettuceConnectionFactory()
Constructs a new
LettuceConnectionFactory instance with default settings. |
LettuceConnectionFactory(LettucePool pool) |
LettuceConnectionFactory(RedisClusterConfiguration clusterConfig)
Constructs a new
LettuceConnectionFactory instance using the given RedisClusterConfiguration
applied to create a RedisClusterClient . |
LettuceConnectionFactory(RedisSentinelConfiguration sentinelConfiguration)
Constructs a new
LettuceConnectionFactory instance using the given RedisSentinelConfiguration |
LettuceConnectionFactory(String host,
int port)
Constructs a new
LettuceConnectionFactory instance with default settings. |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet() |
protected com.lambdaworks.redis.api.StatefulRedisConnection<byte[],byte[]> |
createLettuceConnector() |
void |
destroy() |
com.lambdaworks.redis.resource.ClientResources |
getClientResources()
Get the
ClientResources to reuse infrastructure. |
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.
|
String |
getHostName()
Returns the current host.
|
String |
getPassword()
Returns the password used for authenticating with the Redis server.
|
int |
getPort()
Returns the current port.
|
LettuceReactiveRedisClusterConnection |
getReactiveClusterConnection() |
LettuceReactiveRedisConnection |
getReactiveConnection() |
RedisSentinelConnection |
getSentinelConnection()
Provides a suitable connection for interacting with Redis Sentinel.
|
protected com.lambdaworks.redis.api.StatefulRedisConnection<byte[],byte[]> |
getSharedConnection() |
boolean |
getShareNativeConnection()
Indicates if multiple
LettuceConnection s should share a single native connection. |
long |
getShutdownTimeout()
Returns the shutdown timeout for shutting down the RedisClient (in milliseconds).
|
long |
getTimeout()
Returns the connection timeout (in milliseconds).
|
boolean |
getValidateConnection()
Indicates if validation of the native Lettuce connection is enabled.
|
void |
initConnection() |
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 |
setClientResources(com.lambdaworks.redis.resource.ClientResources clientResources)
Sets the
ClientResources to reuse the client infrastructure. |
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 |
setHostName(String host)
Sets the host.
|
void |
setPassword(String password)
Sets the password used for authenticating with the Redis server.
|
void |
setPort(int port)
Sets the port.
|
void |
setShareNativeConnection(boolean shareNativeConnection)
Enables multiple
LettuceConnection s to share a single native connection. |
void |
setShutdownTimeout(long shutdownTimeout)
Sets the shutdown timeout for shutting down the RedisClient (in milliseconds).
|
void |
setStartTls(boolean startTls)
Sets to issue StartTLS.
|
void |
setTimeout(long timeout)
Sets the connection timeout (in milliseconds).
|
void |
setUseSsl(boolean useSsl)
Sets to use SSL connection
|
void |
setValidateConnection(boolean validateConnection)
Enables validation of the shared native Lettuce connection on calls to
getConnection() . |
void |
setVerifyPeer(boolean verifyPeer)
Sets to use verify certificate validity/hostname check when SSL is used.
|
DataAccessException |
translateExceptionIfPossible(RuntimeException ex) |
void |
validateConnection()
Validate the shared Connection and reinitialize if invalid
|
public static final String PING_REPLY
public LettuceConnectionFactory()
LettuceConnectionFactory
instance with default settings.public LettuceConnectionFactory(String host, int port)
LettuceConnectionFactory
instance with default settings.public LettuceConnectionFactory(RedisSentinelConfiguration sentinelConfiguration)
LettuceConnectionFactory
instance using the given RedisSentinelConfiguration
sentinelConfiguration
- public LettuceConnectionFactory(RedisClusterConfiguration clusterConfig)
LettuceConnectionFactory
instance using the given RedisClusterConfiguration
applied to create a RedisClusterClient
.clusterConfig
- public LettuceConnectionFactory(LettucePool pool)
public void afterPropertiesSet()
afterPropertiesSet
in interface InitializingBean
public void destroy()
destroy
in interface DisposableBean
public RedisConnection getConnection()
RedisConnectionFactory
getConnection
in interface RedisConnectionFactory
public RedisClusterConnection getClusterConnection()
RedisConnectionFactory
getClusterConnection
in interface RedisConnectionFactory
public LettuceReactiveRedisConnection getReactiveConnection()
getReactiveConnection
in interface RedisConnectionFactory
public LettuceReactiveRedisClusterConnection getReactiveClusterConnection()
getReactiveClusterConnection
in interface RedisConnectionFactory
public void initConnection()
public void resetConnection()
public void validateConnection()
public DataAccessException translateExceptionIfPossible(RuntimeException ex)
translateExceptionIfPossible
in interface PersistenceExceptionTranslator
public String getHostName()
public void setHostName(String host)
host
- the host to setpublic int getPort()
public void setPort(int port)
port
- the port to setpublic long getTimeout()
public void setTimeout(long timeout)
timeout
- connection timeoutpublic void setUseSsl(boolean useSsl)
useSsl
- true to use SSL.public boolean isUseSsl()
public void setVerifyPeer(boolean verifyPeer)
verifyPeer
- false not to verify hostname.public boolean isVerifyPeer()
public boolean isStartTls()
public void setStartTls(boolean startTls)
startTls
- true to issue StartTLS.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 validationpublic boolean getShareNativeConnection()
LettuceConnection
s should share a single native connection.public void setShareNativeConnection(boolean shareNativeConnection)
LettuceConnection
s to share a single native connection. If set to false, every operation
on LettuceConnection
will open and close a socket.shareNativeConnection
- enable connection sharingpublic int getDatabase()
public void setDatabase(int index)
index
- database indexpublic String getPassword()
public void setPassword(String password)
password
- the password to setpublic long getShutdownTimeout()
public void setShutdownTimeout(long shutdownTimeout)
shutdownTimeout
- the shutdown timeoutpublic com.lambdaworks.redis.resource.ClientResources getClientResources()
ClientResources
to reuse infrastructure.public void setClientResources(com.lambdaworks.redis.resource.ClientResources clientResources)
ClientResources
to reuse the client infrastructure. clientResources
- can be null.public boolean getConvertPipelineAndTxResults()
LettuceConnection.closePipeline()
and {LettuceConnection#exec()} will be of the type returned by the
Lettuce drivergetConvertPipelineAndTxResults
in interface RedisConnectionFactory
public void setConvertPipelineAndTxResults(boolean convertPipelineAndTxResults)
LettuceConnection.closePipeline()
and {LettuceConnection#exec()} will be of the type returned by the
Lettuce driverconvertPipelineAndTxResults
- Whether or not to convert pipeline and tx resultsprotected com.lambdaworks.redis.api.StatefulRedisConnection<byte[],byte[]> getSharedConnection()
protected com.lambdaworks.redis.api.StatefulRedisConnection<byte[],byte[]> createLettuceConnector()
public boolean isRedisSentinelAware()
RedisSentinelConfiguration
is present.public boolean isClusterAware()
public RedisSentinelConnection getSentinelConnection()
RedisConnectionFactory
getSentinelConnection
in interface RedisConnectionFactory
Copyright © 2011-2016–2016 Pivotal Software, Inc.. All rights reserved.