Class LettuceConnection

java.lang.Object
org.springframework.data.redis.connection.AbstractRedisConnection
org.springframework.data.redis.connection.lettuce.LettuceConnection
All Implemented Interfaces:
AutoCloseable, DefaultedRedisConnection, RedisCommands, RedisCommandsProvider, RedisConnection, RedisConnectionCommands, RedisGeoCommands, RedisHashCommands, RedisHyperLogLogCommands, RedisKeyCommands, RedisListCommands, RedisPubSubCommands, RedisScriptingCommands, RedisServerCommands, RedisSetCommands, RedisStreamCommands, RedisStringCommands, RedisTxCommands, RedisZSetCommands
Direct Known Subclasses:
LettuceClusterConnection

public class LettuceConnection extends AbstractRedisConnection
RedisConnection implementation on top of Lettuce Redis client.
Author:
Costin Leau, Jennifer Hickey, Christoph Strobl, Thomas Darimont, David Liu, Mark Paluch, Ninad Divadkar, Tamil Selvan, ihaohong
  • Constructor Details

    • LettuceConnection

      public LettuceConnection(long timeout, io.lettuce.core.RedisClient client)
      Instantiates a new lettuce connection.
      Parameters:
      timeout - The connection timeout (in milliseconds)
      client - The RedisClient to use when instantiating a native connection
    • LettuceConnection

      public LettuceConnection(@Nullable io.lettuce.core.api.StatefulRedisConnection<byte[],byte[]> sharedConnection, long timeout, io.lettuce.core.RedisClient client)
      Instantiates a new lettuce connection.
      Parameters:
      sharedConnection - A native connection that is shared with other LettuceConnections. Will not be used for transactions or blocking operations
      timeout - The connection timeout (in milliseconds)
      client - The RedisClient to use when making pub/sub, blocking, and tx connections
    • LettuceConnection

      public LettuceConnection(@Nullable io.lettuce.core.api.StatefulRedisConnection<byte[],byte[]> sharedConnection, long timeout, @Nullable io.lettuce.core.AbstractRedisClient client, int defaultDbIndex)
      Parameters:
      sharedConnection - A native connection that is shared with other LettuceConnections. Should not be used for transactions or blocking operations.
      timeout - The connection timeout (in milliseconds)
      client - The RedisClient to use when making pub/sub connections.
      defaultDbIndex - The db index to use along with RedisClient when establishing a dedicated connection.
      Since:
      1.7
    • LettuceConnection

      public LettuceConnection(@Nullable io.lettuce.core.api.StatefulRedisConnection<byte[],byte[]> sharedConnection, LettuceConnectionProvider connectionProvider, long timeout, int defaultDbIndex)
      Parameters:
      sharedConnection - A native connection that is shared with other LettuceConnections. Should not be used for transactions or blocking operations.
      connectionProvider - connection provider to obtain and release native connections.
      timeout - The connection timeout (in milliseconds)
      defaultDbIndex - The db index to use along with RedisClient when establishing a dedicated connection.
      Since:
      2.0
  • Method Details

    • convertLettuceAccessException

      protected DataAccessException convertLettuceAccessException(Exception ex)
    • commands

      public RedisCommands commands()
      Description copied from interface: RedisCommandsProvider
      Returns:
      never null.
    • geoCommands

      public RedisGeoCommands geoCommands()
      Description copied from interface: RedisCommandsProvider
      Returns:
      never null.
    • hashCommands

      public RedisHashCommands hashCommands()
      Description copied from interface: RedisCommandsProvider
      Returns:
      never null.
    • hyperLogLogCommands

      public RedisHyperLogLogCommands hyperLogLogCommands()
      Description copied from interface: RedisCommandsProvider
      Returns:
      never null.
    • keyCommands

      public RedisKeyCommands keyCommands()
      Description copied from interface: RedisCommandsProvider
      Returns:
      never null.
    • listCommands

      public RedisListCommands listCommands()
      Description copied from interface: RedisCommandsProvider
      Returns:
      never null.
    • scriptingCommands

      public RedisScriptingCommands scriptingCommands()
      Description copied from interface: RedisCommandsProvider
      Returns:
      never null.
    • setCommands

      public RedisSetCommands setCommands()
      Description copied from interface: RedisCommandsProvider
      Returns:
      never null.
    • serverCommands

      public RedisServerCommands serverCommands()
      Description copied from interface: RedisCommandsProvider
      Returns:
      never null.
    • streamCommands

      public RedisStreamCommands streamCommands()
      Description copied from interface: RedisCommandsProvider
      Returns:
      never null.
    • stringCommands

      public RedisStringCommands stringCommands()
      Description copied from interface: RedisCommandsProvider
      Returns:
      never null.
    • zSetCommands

      public RedisZSetCommands zSetCommands()
      Description copied from interface: RedisCommandsProvider
      Returns:
      never null.
    • execute

      public Object execute(String command, byte[]... args)
      Description copied from interface: RedisCommands
      'Native' or 'raw' execution of the given command along-side the given arguments. The command is executed as is, with as little 'interpretation' as possible - it is up to the caller to take care of any processing of arguments or the result.
      Parameters:
      command - Command to execute. must not be null.
      args - Possible command arguments (may be empty).
      Returns:
      execution result. Can be null.
    • execute

      @Nullable public Object execute(String command, @Nullable io.lettuce.core.output.CommandOutput commandOutputTypeHint, byte[]... args)
      'Native' or 'raw' execution of the given command along-side the given arguments.
      Parameters:
      command - Command to execute
      commandOutputTypeHint - Type of Output to use, may be (may be null).
      args - Possible command arguments (may be null)
      Returns:
      execution result.
      See Also:
    • close

      public void close()
      Description copied from interface: RedisConnection
      Closes (or quits) the connection.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface RedisConnection
      Overrides:
      close in class AbstractRedisConnection
    • isClosed

      public boolean isClosed()
      Description copied from interface: RedisConnection
      Indicates whether the underlying connection is closed or not.
      Returns:
      true if the connection is closed, false otherwise.
    • getNativeConnection

      public io.lettuce.core.cluster.api.async.RedisClusterAsyncCommands<byte[],byte[]> getNativeConnection()
      Description copied from interface: RedisConnection
      Returns the native connection (the underlying library/driver object).
      Returns:
      underlying, native object
    • isQueueing

      public boolean isQueueing()
      Description copied from interface: RedisConnection
      Indicates whether the connection is in "queue"(or "MULTI") mode or not. When queueing, all commands are postponed until EXEC or DISCARD commands are issued. Since in queueing no results are returned, the connection will return NULL on all operations that interact with the data.
      Returns:
      true if the connection is in queue/MULTI mode, false otherwise
    • isPipelined

      public boolean isPipelined()
      Description copied from interface: RedisConnection
      Indicates whether the connection is currently pipelined or not.
      Returns:
      true if the connection is pipelined, false otherwise
      See Also:
    • openPipeline

      public void openPipeline()
      Description copied from interface: RedisConnection
      Activates the pipeline mode for this connection. When pipelined, all commands return null (the reply is read at the end through RedisConnection.closePipeline(). Calling this method when the connection is already pipelined has no effect. Pipelining is used for issuing commands without requesting the response right away but rather at the end of the batch. While somewhat similar to MULTI, pipelining does not guarantee atomicity - it only tries to improve performance when issuing a lot of commands (such as in batching scenarios).

      Note:

      Consider doing some performance testing before using this feature since in many cases the performance benefits are minimal yet the impact on usage are not.
      See Also:
    • closePipeline

      public List<Object> closePipeline()
      Description copied from interface: RedisConnection
      Executes the commands in the pipeline and returns their result. If the connection is not pipelined, an empty collection is returned.
      Returns:
      the result of the executed commands.
    • echo

      public byte[] echo(byte[] message)
      Description copied from interface: RedisConnectionCommands
      Returns message via server roundtrip.
      Parameters:
      message - the message to echo.
      Returns:
      the message or null when used in pipeline / transaction.
      See Also:
    • ping

      public String ping()
      Description copied from interface: RedisConnectionCommands
      Test connection.
      Returns:
      Server response message - usually PONG. null when used in pipeline / transaction.
      See Also:
    • discard

      public void discard()
      Description copied from interface: RedisTxCommands
      Discard all commands issued after RedisTxCommands.multi().
      See Also:
    • exec

      public List<Object> exec()
      Description copied from interface: RedisTxCommands
      Executes all queued commands in a transaction started with RedisTxCommands.multi().
      If used along with RedisTxCommands.watch(byte[]...) the operation will fail if any of watched keys has been modified.
      Returns:
      List of replies for each executed command.
      See Also:
    • multi

      public void multi()
      Description copied from interface: RedisTxCommands
      Mark the start of a transaction block.
      Commands will be queued and can then be executed by calling RedisTxCommands.exec() or rolled back using RedisTxCommands.discard()
      See Also:
    • select

      public void select(int dbIndex)
      Description copied from interface: RedisConnectionCommands
      Select the DB with given positive dbIndex.
      Parameters:
      dbIndex - the database index.
      See Also:
    • unwatch

      public void unwatch()
      Description copied from interface: RedisTxCommands
      Flushes all the previously RedisTxCommands.watch(byte[]...) keys.
      See Also:
    • watch

      public void watch(byte[]... keys)
      Description copied from interface: RedisTxCommands
      Watch given keys for modifications during transaction started with RedisTxCommands.multi().
      Parameters:
      keys - must not be null.
      See Also:
    • publish

      public Long publish(byte[] channel, byte[] message)
      Description copied from interface: RedisPubSubCommands
      Publishes the given message to the given channel.
      Parameters:
      channel - the channel to publish to. Must not be null.
      message - message to publish. Must not be null.
      Returns:
      the number of clients that received the message or null when used in pipeline / transaction.
      See Also:
    • getSubscription

      public Subscription getSubscription()
      Description copied from interface: RedisPubSubCommands
      Returns the current subscription for this connection or null if the connection is not subscribed.
      Returns:
      the current subscription, null if none is available.
    • isSubscribed

      public boolean isSubscribed()
      Description copied from interface: RedisPubSubCommands
      Indicates whether the current connection is subscribed (to at least one channel) or not.
      Returns:
      true if the connection is subscribed, false otherwise
    • pSubscribe

      public void pSubscribe(MessageListener listener, byte[]... patterns)
      Description copied from interface: RedisPubSubCommands
      Subscribes the connection to all channels matching the given patterns. Once subscribed, a connection enters listening mode and can only subscribe to other channels or unsubscribe. No other commands are accepted until the connection is unsubscribed.

      Note that this operation is blocking and the current thread starts waiting for new messages immediately.

      Parameters:
      listener - message listener, must not be null.
      patterns - channel name patterns, must not be null.
      See Also:
    • subscribe

      public void subscribe(MessageListener listener, byte[]... channels)
      Description copied from interface: RedisPubSubCommands
      Subscribes the connection to the given channels. Once subscribed, a connection enters listening mode and can only subscribe to other channels or unsubscribe. No other commands are accepted until the connection is unsubscribed.

      Note that this operation is blocking and the current thread starts waiting for new messages immediately.

      Parameters:
      listener - message listener, must not be null.
      channels - channel names, must not be null.
      See Also:
    • setConvertPipelineAndTxResults

      public void setConvertPipelineAndTxResults(boolean convertPipelineAndTxResults)
      Specifies if pipelined and transaction results should be converted to the expected data type. If false, results of closePipeline() and exec() will be of the type returned by the Lettuce driver
      Parameters:
      convertPipelineAndTxResults - Whether or not to convert pipeline and tx results
    • setPipeliningFlushPolicy

      public void setPipeliningFlushPolicy(LettuceConnection.PipeliningFlushPolicy pipeliningFlushPolicy)
      Configures the flushing policy when using pipelining.
      Parameters:
      pipeliningFlushPolicy - the flushing policy to control when commands get written to the Redis connection.
      Since:
      2.3
      See Also:
    • switchToPubSub

      protected io.lettuce.core.pubsub.StatefulRedisPubSubConnection<byte[],byte[]> switchToPubSub()
      close() the current connection and open a new pub/sub connection to the Redis server.
      Returns:
      never null.
    • doCreateSubscription

      protected LettuceSubscription doCreateSubscription(MessageListener listener, io.lettuce.core.pubsub.StatefulRedisPubSubConnection<byte[],byte[]> connection, LettuceConnectionProvider connectionProvider)
      Customization hook to create a LettuceSubscription.
      Parameters:
      listener - the MessageListener to notify.
      connection - Pub/Sub connection.
      connectionProvider - the LettuceConnectionProvider for connection release.
      Returns:
      a LettuceSubscription.
      Since:
      2.2
    • getConnection

      protected io.lettuce.core.cluster.api.sync.RedisClusterCommands<byte[],byte[]> getConnection()
    • getAsyncDedicatedConnection

      protected io.lettuce.core.cluster.api.async.RedisClusterAsyncCommands<byte[],byte[]> getAsyncDedicatedConnection()
    • doGetAsyncDedicatedConnection

      protected io.lettuce.core.api.StatefulConnection<byte[],byte[]> doGetAsyncDedicatedConnection()
    • isActive

      protected boolean isActive(RedisNode node)
      Description copied from class: AbstractRedisConnection
      Check if node is active by sending ping.
      Overrides:
      isActive in class AbstractRedisConnection
      Returns:
    • getSentinelConnection

      protected RedisSentinelConnection getSentinelConnection(RedisNode sentinel)
      Description copied from class: AbstractRedisConnection
      Get RedisSentinelCommands connected to given node.
      Overrides:
      getSentinelConnection in class AbstractRedisConnection
      Returns: