java.lang.Object
io.lettuce.core.AbstractRedisClient
io.lettuce.core.RedisClient
public class RedisClient extends AbstractRedisClient
A scalable and thread-safe Redis client supporting synchronous, asynchronous and reactive
execution models. Multiple threads may share one connection if they avoid blocking and transactional operations such as BLPOP
and MULTI/EXEC.
RedisClient can be used with:
- Redis Standalone
- Redis Pub/Sub
- Redis Sentinel, Sentinel connections
- Redis Sentinel, Upstream connections
RedisClusterClient. Upstream/Replica connections through
MasterReplica provide connections to Redis Upstream/Replica ("Master/Slave") setups which run either in a static
Upstream/Replica setup or are managed by Redis Sentinel.
RedisClient is an expensive resource. It holds a set of netty's EventLoopGroup's that use
multiple threads. Reuse this instance as much as possible or share a ClientResources instance amongst multiple client
instances.
- Author:
- Will Glozer, Mark Paluch
- See Also:
RedisURI,StatefulRedisConnection,RedisFuture,Mono,Flux,RedisCodec,ClientOptions,ClientResources,MasterReplica,RedisClusterClient
-
Field Summary
Fields inherited from class io.lettuce.core.AbstractRedisClient
channels, closeableResources, connectionEvents -
Constructor Summary
Constructors Modifier Constructor Description protectedRedisClient()Creates a uri-less RedisClient.protectedRedisClient(ClientResources clientResources, RedisURI redisURI) -
Method Summary
Modifier and Type Method Description StatefulRedisConnection<String,String>connect()Open a new connection to a Redis server that treats keys and values as UTF-8 strings.<K, V> StatefulRedisConnection<K,V>connect(RedisCodec<K,V> codec)Open a new connection to a Redis server.<K, V> StatefulRedisConnection<K,V>connect(RedisCodec<K,V> codec, RedisURI redisURI)StatefulRedisConnection<String,String>connect(RedisURI redisURI)Open a new connection to a Redis server using the suppliedRedisURIthat treats keys and values as UTF-8 strings.<K, V> ConnectionFuture<StatefulRedisConnection<K,V>>connectAsync(RedisCodec<K,V> codec, RedisURI redisURI)StatefulRedisPubSubConnection<String,String>connectPubSub()Open a new pub/sub connection to a Redis server that treats keys and values as UTF-8 strings.<K, V> StatefulRedisPubSubConnection<K,V>connectPubSub(RedisCodec<K,V> codec)<K, V> StatefulRedisPubSubConnection<K,V>connectPubSub(RedisCodec<K,V> codec, RedisURI redisURI)StatefulRedisPubSubConnection<String,String>connectPubSub(RedisURI redisURI)Open a new pub/sub connection to a Redis server using the suppliedRedisURIthat treats keys and values as UTF-8 strings.<K, V> ConnectionFuture<StatefulRedisPubSubConnection<K,V>>connectPubSubAsync(RedisCodec<K,V> codec, RedisURI redisURI)StatefulRedisSentinelConnection<String,String>connectSentinel()Open a connection to a Redis Sentinel that treats keys and values as UTF-8 strings.<K, V> StatefulRedisSentinelConnection<K,V>connectSentinel(RedisCodec<K,V> codec)Open a connection to a Redis Sentinel that treats keys and use the suppliedcodecto encode/decode keys and values.<K, V> StatefulRedisSentinelConnection<K,V>connectSentinel(RedisCodec<K,V> codec, RedisURI redisURI)StatefulRedisSentinelConnection<String,String>connectSentinel(RedisURI redisURI)Open a connection to a Redis Sentinel using the suppliedRedisURIthat treats keys and values as UTF-8 strings.<K, V> CompletableFuture<StatefulRedisSentinelConnection<K,V>>connectSentinelAsync(RedisCodec<K,V> codec, RedisURI redisURI)static RedisClientcreate()Creates a uri-less RedisClient with defaultClientResources.static RedisClientcreate(RedisURI redisURI)Create a new client that connects to the supplieduriwith defaultClientResources.static RedisClientcreate(ClientResources clientResources)Creates a uri-less RedisClient with sharedClientResources.static RedisClientcreate(ClientResources clientResources, RedisURI redisURI)Create a new client that connects to the supplieduriwith sharedClientResources.static RedisClientcreate(ClientResources clientResources, String uri)Create a new client that connects to the supplied uri with sharedClientResources.You need to shut down theClientResourcesupon shutting down your application.static RedisClientcreate(String uri)Create a new client that connects to the supplied uri with defaultClientResources.protected Mono<SocketAddress>getSocketAddress(RedisURI redisURI)protected <K, V> StatefulRedisConnectionImpl<K,V>newStatefulRedisConnection(RedisChannelWriter channelWriter, PushHandler pushHandler, RedisCodec<K,V> codec, Duration timeout)Create a new instance ofStatefulRedisConnectionImplor a subclass.protected <K, V> StatefulRedisPubSubConnectionImpl<K,V>newStatefulRedisPubSubConnection(PubSubEndpoint<K,V> endpoint, RedisChannelWriter channelWriter, RedisCodec<K,V> codec, Duration timeout)Create a new instance ofStatefulRedisPubSubConnectionImplor a subclass.protected <K, V> StatefulRedisSentinelConnectionImpl<K,V>newStatefulRedisSentinelConnection(RedisChannelWriter channelWriter, RedisCodec<K,V> codec, Duration timeout)Create a new instance ofStatefulRedisSentinelConnectionImplor a subclass.protected RedisCodec<String,String>newStringStringCodec()voidsetOptions(ClientOptions clientOptions)Set theClientOptionsfor the client.Methods inherited from class io.lettuce.core.AbstractRedisClient
addListener, addListener, channelType, connectionBuilder, createHandshake, getChannelCount, getCommandListeners, getConnection, getConnection, getDefaultTimeout, getOptions, getResourceCount, getResources, initializeChannelAsync, removeListener, removeListener, setDefaultTimeout, setDefaultTimeout, shutdown, shutdown, shutdown, shutdownAsync, shutdownAsync
-
Constructor Details
-
RedisClient
-
RedisClient
protected RedisClient()Creates a uri-less RedisClient. You can connect to different Redis servers but you must supply aRedisURIon connecting. Methods without having aRedisURIwill fail with aIllegalStateException. Non-private constructor to makeRedisClientproxyable.
-
-
Method Details
-
create
Creates a uri-less RedisClient with defaultClientResources. You can connect to different Redis servers but you must supply aRedisURIon connecting. Methods without having aRedisURIwill fail with aIllegalStateException.- Returns:
- a new instance of
RedisClient
-
create
Create a new client that connects to the supplieduriwith defaultClientResources. You can connect to different Redis servers but you must supply aRedisURIon connecting.- Parameters:
redisURI- the Redis URI, must not benull- Returns:
- a new instance of
RedisClient
-
create
Create a new client that connects to the supplied uri with defaultClientResources. You can connect to different Redis servers but you must supply aRedisURIon connecting.- Parameters:
uri- the Redis URI, must not benull- Returns:
- a new instance of
RedisClient
-
create
Creates a uri-less RedisClient with sharedClientResources. You need to shut down theClientResourcesupon shutting down your application. You can connect to different Redis servers but you must supply aRedisURIon connecting. Methods without having aRedisURIwill fail with aIllegalStateException.- Parameters:
clientResources- the client resources, must not benull- Returns:
- a new instance of
RedisClient
-
create
Create a new client that connects to the supplied uri with sharedClientResources.You need to shut down theClientResourcesupon shutting down your application. You can connect to different Redis servers but you must supply aRedisURIon connecting.- Parameters:
clientResources- the client resources, must not benulluri- the Redis URI, must not benull- Returns:
- a new instance of
RedisClient
-
create
Create a new client that connects to the supplieduriwith sharedClientResources. You need to shut down theClientResourcesupon shutting down your application.You can connect to different Redis servers but you must supply aRedisURIon connecting.- Parameters:
clientResources- the client resources, must not benullredisURI- the Redis URI, must not benull- Returns:
- a new instance of
RedisClient
-
connect
Open a new connection to a Redis server that treats keys and values as UTF-8 strings.- Returns:
- A new stateful Redis connection
-
connect
Open a new connection to a Redis server. Use the suppliedcodecto encode/decode keys and values.- Type Parameters:
K- Key typeV- Value type- Parameters:
codec- Use this codec to encode/decode keys and values, must not benull- Returns:
- A new stateful Redis connection
-
connect
Open a new connection to a Redis server using the suppliedRedisURIthat treats keys and values as UTF-8 strings.- Parameters:
redisURI- the Redis server to connect to, must not benull- Returns:
- A new connection
-
connect
Open a new connection to a Redis server using the suppliedRedisURIand the suppliedcodecto encode/decode keys.- Type Parameters:
K- Key typeV- Value type- Parameters:
codec- Use this codec to encode/decode keys and values, must not benullredisURI- the Redis server to connect to, must not benull- Returns:
- A new connection
-
connectAsync
public <K, V> ConnectionFuture<StatefulRedisConnection<K,V>> connectAsync(RedisCodec<K,V> codec, RedisURI redisURI)Open asynchronously a new connection to a Redis server using the suppliedRedisURIand the suppliedcodecto encode/decode keys.- Type Parameters:
K- Key typeV- Value type- Parameters:
codec- Use this codec to encode/decode keys and values, must not benullredisURI- the Redis server to connect to, must not benull- Returns:
ConnectionFutureto indicate success or failure to connect.- Since:
- 5.0
-
connectPubSub
Open a new pub/sub connection to a Redis server that treats keys and values as UTF-8 strings.- Returns:
- A new stateful pub/sub connection
-
connectPubSub
Open a new pub/sub connection to a Redis server using the suppliedRedisURIthat treats keys and values as UTF-8 strings.- Parameters:
redisURI- the Redis server to connect to, must not benull- Returns:
- A new stateful pub/sub connection
-
connectPubSub
Open a new pub/sub connection to the Redis server using the suppliedRedisURIand use the suppliedcodecto encode/decode keys and values.- Type Parameters:
K- Key typeV- Value type- Parameters:
codec- Use this codec to encode/decode keys and values, must not benull- Returns:
- A new stateful pub/sub connection
-
connectPubSub
public <K, V> StatefulRedisPubSubConnection<K,V> connectPubSub(RedisCodec<K,V> codec, RedisURI redisURI)Open a new pub/sub connection to the Redis server using the suppliedRedisURIand use the suppliedcodecto encode/decode keys and values.- Type Parameters:
K- Key typeV- Value type- Parameters:
codec- Use this codec to encode/decode keys and values, must not benullredisURI- the Redis server to connect to, must not benull- Returns:
- A new connection
-
connectPubSubAsync
public <K, V> ConnectionFuture<StatefulRedisPubSubConnection<K,V>> connectPubSubAsync(RedisCodec<K,V> codec, RedisURI redisURI)Open asynchronously a new pub/sub connection to the Redis server using the suppliedRedisURIand use the suppliedcodecto encode/decode keys and values.- Type Parameters:
K- Key typeV- Value type- Parameters:
codec- Use this codec to encode/decode keys and values, must not benullredisURI- the redis server to connect to, must not benull- Returns:
ConnectionFutureto indicate success or failure to connect.- Since:
- 5.0
-
connectSentinel
Open a connection to a Redis Sentinel that treats keys and values as UTF-8 strings.- Returns:
- A new stateful Redis Sentinel connection
-
connectSentinel
Open a connection to a Redis Sentinel that treats keys and use the suppliedcodecto encode/decode keys and values. The clientRedisURImust contain one or more sentinels.- Type Parameters:
K- Key typeV- Value type- Parameters:
codec- Use this codec to encode/decode keys and values, must not benull- Returns:
- A new stateful Redis Sentinel connection
-
connectSentinel
Open a connection to a Redis Sentinel using the suppliedRedisURIthat treats keys and values as UTF-8 strings. The clientRedisURImust contain one or more sentinels.- Parameters:
redisURI- the Redis server to connect to, must not benull- Returns:
- A new connection
-
connectSentinel
public <K, V> StatefulRedisSentinelConnection<K,V> connectSentinel(RedisCodec<K,V> codec, RedisURI redisURI)Open a connection to a Redis Sentinel using the suppliedRedisURIand use the suppliedcodecto encode/decode keys and values. The clientRedisURImust contain one or more sentinels.- Type Parameters:
K- Key typeV- Value type- Parameters:
codec- the Redis server to connect to, must not benullredisURI- the Redis server to connect to, must not benull- Returns:
- A new connection
-
connectSentinelAsync
public <K, V> CompletableFuture<StatefulRedisSentinelConnection<K,V>> connectSentinelAsync(RedisCodec<K,V> codec, RedisURI redisURI)Open asynchronously a connection to a Redis Sentinel using the suppliedRedisURIand use the suppliedcodecto encode/decode keys and values. The clientRedisURImust contain one or more sentinels.- Type Parameters:
K- Key typeV- Value type- Parameters:
codec- the Redis server to connect to, must not benullredisURI- the Redis server to connect to, must not benull- Returns:
- A new connection
- Since:
- 5.1
-
setOptions
Set theClientOptionsfor the client.- Overrides:
setOptionsin classAbstractRedisClient- Parameters:
clientOptions- the new client options- Throws:
IllegalArgumentException- if clientOptions is null
-
newStatefulRedisPubSubConnection
protected <K, V> StatefulRedisPubSubConnectionImpl<K,V> newStatefulRedisPubSubConnection(PubSubEndpoint<K,V> endpoint, RedisChannelWriter channelWriter, RedisCodec<K,V> codec, Duration timeout)Create a new instance ofStatefulRedisPubSubConnectionImplor a subclass.Subclasses of
RedisClientmay override that method.- Type Parameters:
K- Key-TypeV- Value Type- Parameters:
endpoint- the endpointchannelWriter- the channel writercodec- codectimeout- default timeout- Returns:
- new instance of StatefulRedisPubSubConnectionImpl
-
newStatefulRedisSentinelConnection
protected <K, V> StatefulRedisSentinelConnectionImpl<K,V> newStatefulRedisSentinelConnection(RedisChannelWriter channelWriter, RedisCodec<K,V> codec, Duration timeout)Create a new instance ofStatefulRedisSentinelConnectionImplor a subclass.Subclasses of
RedisClientmay override that method.- Type Parameters:
K- Key-TypeV- Value Type- Parameters:
channelWriter- the channel writercodec- codectimeout- default timeout- Returns:
- new instance of StatefulRedisSentinelConnectionImpl
-
newStatefulRedisConnection
protected <K, V> StatefulRedisConnectionImpl<K,V> newStatefulRedisConnection(RedisChannelWriter channelWriter, PushHandler pushHandler, RedisCodec<K,V> codec, Duration timeout)Create a new instance ofStatefulRedisConnectionImplor a subclass.Subclasses of
RedisClientmay override that method.- Type Parameters:
K- Key-TypeV- Value Type- Parameters:
channelWriter- the channel writerpushHandler- the handler for push notificationscodec- codectimeout- default timeout- Returns:
- new instance of StatefulRedisConnectionImpl
-
getSocketAddress
Get aMonothat resolvesRedisURIto aSocketAddress. Resolution is performed either using Redis Sentinel (if theRedisURIis configured with Sentinels) or via DNS resolution.Subclasses of
RedisClientmay override that method.- Parameters:
redisURI- must not benull.- Returns:
- the resolved
SocketAddress. - See Also:
ClientResources.dnsResolver(),RedisURI.getSentinels(),RedisURI.getSentinelMasterId()
-
newStringStringCodec
- Returns:
- a
Stringcodec. - See Also:
StringCodec.UTF8
-