Package io.lettuce.core
Class RedisClient
- java.lang.Object
-
- io.lettuce.core.AbstractRedisClient
-
- io.lettuce.core.RedisClient
-
- All Implemented Interfaces:
AutoCloseable
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 throughMasterReplica
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'sEventLoopGroup
's that use multiple threads. Reuse this instance as much as possible or share aClientResources
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 protected
RedisClient()
Creates a uri-less RedisClient.protected
RedisClient(ClientResources clientResources, RedisURI redisURI)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods 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 suppliedRedisURI
that 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 suppliedRedisURI
that 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 suppliedcodec
to 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 suppliedRedisURI
that treats keys and values as UTF-8 strings.<K,V>
CompletableFuture<StatefulRedisSentinelConnection<K,V>>connectSentinelAsync(RedisCodec<K,V> codec, RedisURI redisURI)
static RedisClient
create()
Creates a uri-less RedisClient with defaultClientResources
.static RedisClient
create(RedisURI redisURI)
Create a new client that connects to the supplieduri
with defaultClientResources
.static RedisClient
create(ClientResources clientResources)
Creates a uri-less RedisClient with sharedClientResources
.static RedisClient
create(ClientResources clientResources, RedisURI redisURI)
Create a new client that connects to the supplieduri
with sharedClientResources
.static RedisClient
create(ClientResources clientResources, String uri)
Create a new client that connects to the supplied uri with sharedClientResources
.You need to shut down theClientResources
upon shutting down your application.static RedisClient
create(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 ofStatefulRedisConnectionImpl
or 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 ofStatefulRedisPubSubConnectionImpl
or a subclass.protected <K,V>
StatefulRedisSentinelConnectionImpl<K,V>newStatefulRedisSentinelConnection(RedisChannelWriter channelWriter, RedisCodec<K,V> codec, Duration timeout)
Create a new instance ofStatefulRedisSentinelConnectionImpl
or a subclass.protected RedisCodec<String,String>
newStringStringCodec()
void
setOptions(ClientOptions clientOptions)
Set theClientOptions
for the client.-
Methods inherited from class io.lettuce.core.AbstractRedisClient
addListener, addListener, channelType, close, connectionBuilder, connectionBuilder, createHandshake, getChannelCount, getCommandListeners, getConnection, getConnection, getDefaultTimeout, getOptions, getResourceCount, getResources, initializeChannelAsync, removeListener, removeListener, setDefaultTimeout, setDefaultTimeout, shutdown, shutdown, shutdown, shutdownAsync, shutdownAsync
-
-
-
-
Constructor Detail
-
RedisClient
protected RedisClient(ClientResources clientResources, RedisURI redisURI)
-
RedisClient
protected RedisClient()
Creates a uri-less RedisClient. You can connect to different Redis servers but you must supply aRedisURI
on connecting. Methods without having aRedisURI
will fail with aIllegalStateException
. Non-private constructor to makeRedisClient
proxyable.
-
-
Method Detail
-
create
public static RedisClient create()
Creates a uri-less RedisClient with defaultClientResources
. You can connect to different Redis servers but you must supply aRedisURI
on connecting. Methods without having aRedisURI
will fail with aIllegalStateException
.- Returns:
- a new instance of
RedisClient
-
create
public static RedisClient create(RedisURI redisURI)
Create a new client that connects to the supplieduri
with defaultClientResources
. You can connect to different Redis servers but you must supply aRedisURI
on connecting.- Parameters:
redisURI
- the Redis URI, must not benull
- Returns:
- a new instance of
RedisClient
-
create
public static RedisClient create(String uri)
Create a new client that connects to the supplied uri with defaultClientResources
. You can connect to different Redis servers but you must supply aRedisURI
on connecting.- Parameters:
uri
- the Redis URI, must not benull
- Returns:
- a new instance of
RedisClient
-
create
public static RedisClient create(ClientResources clientResources)
Creates a uri-less RedisClient with sharedClientResources
. You need to shut down theClientResources
upon shutting down your application. You can connect to different Redis servers but you must supply aRedisURI
on connecting. Methods without having aRedisURI
will fail with aIllegalStateException
.- Parameters:
clientResources
- the client resources, must not benull
- Returns:
- a new instance of
RedisClient
-
create
public static RedisClient create(ClientResources clientResources, String uri)
Create a new client that connects to the supplied uri with sharedClientResources
.You need to shut down theClientResources
upon shutting down your application. You can connect to different Redis servers but you must supply aRedisURI
on connecting.- Parameters:
clientResources
- the client resources, must not benull
uri
- the Redis URI, must not benull
- Returns:
- a new instance of
RedisClient
-
create
public static RedisClient create(ClientResources clientResources, RedisURI redisURI)
Create a new client that connects to the supplieduri
with sharedClientResources
. You need to shut down theClientResources
upon shutting down your application.You can connect to different Redis servers but you must supply aRedisURI
on connecting.- Parameters:
clientResources
- the client resources, must not benull
redisURI
- the Redis URI, must not benull
- Returns:
- a new instance of
RedisClient
-
connect
public StatefulRedisConnection<String,String> 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
public <K,V> StatefulRedisConnection<K,V> connect(RedisCodec<K,V> codec)
Open a new connection to a Redis server. Use the suppliedcodec
to 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
public StatefulRedisConnection<String,String> connect(RedisURI redisURI)
Open a new connection to a Redis server using the suppliedRedisURI
that treats keys and values as UTF-8 strings.- Parameters:
redisURI
- the Redis server to connect to, must not benull
- Returns:
- A new connection
-
connect
public <K,V> StatefulRedisConnection<K,V> connect(RedisCodec<K,V> codec, RedisURI redisURI)
Open a new connection to a Redis server using the suppliedRedisURI
and the suppliedcodec
to encode/decode keys.- Type Parameters:
K
- Key typeV
- Value type- Parameters:
codec
- Use this codec to encode/decode keys and values, must not benull
redisURI
- 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 suppliedRedisURI
and the suppliedcodec
to encode/decode keys.- Type Parameters:
K
- Key typeV
- Value type- Parameters:
codec
- Use this codec to encode/decode keys and values, must not benull
redisURI
- the Redis server to connect to, must not benull
- Returns:
ConnectionFuture
to indicate success or failure to connect.- Since:
- 5.0
-
connectPubSub
public StatefulRedisPubSubConnection<String,String> 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
public StatefulRedisPubSubConnection<String,String> connectPubSub(RedisURI redisURI)
Open a new pub/sub connection to a Redis server using the suppliedRedisURI
that 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
public <K,V> StatefulRedisPubSubConnection<K,V> connectPubSub(RedisCodec<K,V> codec)
Open a new pub/sub connection to the Redis server using the suppliedRedisURI
and use the suppliedcodec
to 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 suppliedRedisURI
and use the suppliedcodec
to 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
redisURI
- 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 suppliedRedisURI
and use the suppliedcodec
to 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
redisURI
- the redis server to connect to, must not benull
- Returns:
ConnectionFuture
to indicate success or failure to connect.- Since:
- 5.0
-
connectSentinel
public StatefulRedisSentinelConnection<String,String> 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
public <K,V> StatefulRedisSentinelConnection<K,V> connectSentinel(RedisCodec<K,V> codec)
Open a connection to a Redis Sentinel that treats keys and use the suppliedcodec
to encode/decode keys and values. The clientRedisURI
must 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
public StatefulRedisSentinelConnection<String,String> connectSentinel(RedisURI redisURI)
Open a connection to a Redis Sentinel using the suppliedRedisURI
that treats keys and values as UTF-8 strings. The clientRedisURI
must 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 suppliedRedisURI
and use the suppliedcodec
to encode/decode keys and values. The clientRedisURI
must contain one or more sentinels.- Type Parameters:
K
- Key typeV
- Value type- Parameters:
codec
- the Redis server to connect to, must not benull
redisURI
- 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 suppliedRedisURI
and use the suppliedcodec
to encode/decode keys and values. The clientRedisURI
must contain one or more sentinels.- Type Parameters:
K
- Key typeV
- Value type- Parameters:
codec
- the Redis server to connect to, must not benull
redisURI
- the Redis server to connect to, must not benull
- Returns:
- A new connection
- Since:
- 5.1
-
setOptions
public void setOptions(ClientOptions clientOptions)
Set theClientOptions
for the client.- Overrides:
setOptions
in 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 ofStatefulRedisPubSubConnectionImpl
or a subclass.Subclasses of
RedisClient
may 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 ofStatefulRedisSentinelConnectionImpl
or a subclass.Subclasses of
RedisClient
may 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 ofStatefulRedisConnectionImpl
or a subclass.Subclasses of
RedisClient
may 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
protected Mono<SocketAddress> getSocketAddress(RedisURI redisURI)
Get aMono
that resolvesRedisURI
to aSocketAddress
. Resolution is performed either using Redis Sentinel (if theRedisURI
is configured with Sentinels) or via DNS resolution.Subclasses of
RedisClient
may override that method.- Parameters:
redisURI
- must not benull
.- Returns:
- the resolved
SocketAddress
. - See Also:
ClientResources.dnsResolver()
,RedisURI.getSentinels()
,RedisURI.getSentinelMasterId()
-
newStringStringCodec
protected RedisCodec<String,String> newStringStringCodec()
- Returns:
- a
String
codec
. - See Also:
StringCodec.UTF8
-
-