Class ValkeyGlideConnectionFactory

java.lang.Object
io.valkey.springframework.data.valkey.connection.valkeyglide.ValkeyGlideConnectionFactory
All Implemented Interfaces:
ValkeyConnectionFactory, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.context.Lifecycle, org.springframework.context.Phased, org.springframework.context.SmartLifecycle, org.springframework.dao.support.PersistenceExceptionTranslator

public class ValkeyGlideConnectionFactory extends Object implements ValkeyConnectionFactory, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean, org.springframework.context.SmartLifecycle
Connection factory creating Valkey Glide based connections. This is the central class for connecting to Valkey using Valkey-Glide.

This factory creates a new ValkeyGlideConnection on each call to getConnection(). The underlying client instance is shared among all connections.

This class implements the InitializingBean interface, triggering the creation of the client instance on afterPropertiesSet(). It also implements DisposableBean for closing the client on application shutdown.

The Valkey Glide connection factory can be used both with a standalone Valkey server and with a Valkey cluster. For standalone mode, use the ValkeyGlideConnectionFactory() constructor or ValkeyGlideConnectionFactory(ValkeyGlideClientConfiguration) constructor. For cluster mode, ensure the ValkeyGlideClientConfiguration is configured for cluster mode.

Since:
2.0
Author:
Ilia Kolominsky
  • Constructor Details

  • Method Details

    • afterPropertiesSet

      public void afterPropertiesSet()
      Initialize the factory by pre-creating a pool of client instances if early startup is enabled.
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
    • getConnection

      public ValkeyConnection getConnection()
      Description copied from interface: ValkeyConnectionFactory
      Returns a suitable connection for interacting with Valkey.
      Specified by:
      getConnection in interface ValkeyConnectionFactory
      Returns:
      connection for interacting with Valkey.
    • getClusterConnection

      public ValkeyClusterConnection getClusterConnection()
      Description copied from interface: ValkeyConnectionFactory
      Returns a suitable connection for interacting with Valkey Cluster.
      Specified by:
      getClusterConnection in interface ValkeyConnectionFactory
      Returns:
      a connection for interacting with Valkey Cluster.
    • getConvertPipelineAndTxResults

      public boolean getConvertPipelineAndTxResults()
      Description copied from interface: ValkeyConnectionFactory
      Specifies if pipelined results should be converted to the expected data type.

      If false, results of ValkeyConnection.closePipeline() and ValkeyTxCommands.exec() will be of the type returned by the underlying driver. This method is mostly for backwards compatibility with 1.0. It is generally always a good idea to allow results to be converted and deserialized. In fact, this is now the default behavior.

      Specified by:
      getConvertPipelineAndTxResults in interface ValkeyConnectionFactory
      Returns:
      true to convert pipeline and transaction results; false otherwise.
    • getSentinelConnection

      public ValkeySentinelConnection getSentinelConnection()
      Description copied from interface: ValkeyConnectionFactory
      Returns a suitable connection for interacting with Valkey Sentinel.
      Specified by:
      getSentinelConnection in interface ValkeyConnectionFactory
      Returns:
      a connection for interacting with Valkey Sentinel.
    • destroy

      public void destroy()
      Shut down the factory when this factory is destroyed. Closes all pooled client instances.
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean
    • start

      public void start()
      Initializes the factory, starting it.
      Specified by:
      start in interface org.springframework.context.Lifecycle
    • stop

      public void stop()
      Stops the factory.
      Specified by:
      stop in interface org.springframework.context.Lifecycle
    • isRunning

      public boolean isRunning()
      Returns if the client is running.
      Specified by:
      isRunning in interface org.springframework.context.Lifecycle
      Returns:
      true if running
    • isClusterAware

      public boolean isClusterAware()
      Returns:
      true if cluster mode is enabled.
    • getClientConfiguration

      public ValkeyGlideClientConfiguration getClientConfiguration()
      Returns:
      The client configuration used.
    • getStandaloneConfiguration

      @Nullable public ValkeyStandaloneConfiguration getStandaloneConfiguration()
      Returns:
      the ValkeyStandaloneConfiguration, may be null.
      Since:
      3.0
    • getClusterConfiguration

      @Nullable public ValkeyClusterConfiguration getClusterConfiguration()
      Returns:
      the ValkeyClusterConfiguration, may be null.
      Since:
      3.0
    • getSentinelConfiguration

      @Nullable public ValkeySentinelConfiguration getSentinelConfiguration()
      Returns:
      the ValkeySentinelConfiguration, may be null.
      Throws:
      UnsupportedOperationException - as Sentinel connections are not supported with Valkey-Glide.
      Since:
      3.0
    • setExecutor

      public void setExecutor(org.springframework.core.task.AsyncTaskExecutor executor)
      Set the AsyncTaskExecutor to use for asynchronous command execution.
      Parameters:
      executor - executor used to execute commands asynchronously.
      Since:
      3.2
    • getHostName

      public String getHostName()
      Returns the current host.
      Returns:
      the host.
    • getPort

      public int getPort()
      Returns the current port.
      Returns:
      the port.
    • getDatabase

      public int getDatabase()
      Returns the index of the database.
      Returns:
      the database index.
    • getClientName

      @Nullable public String getClientName()
      Returns the client name.
      Returns:
      the client name or null if not set.
    • isUseSsl

      public boolean isUseSsl()
      Returns whether to use SSL.
      Returns:
      use of SSL.
    • getPassword

      @Nullable public String getPassword()
      Returns the password used for authenticating with the Valkey server.
      Returns:
      password for authentication or null if not set.
    • isAutoStartup

      public boolean isAutoStartup()
      Specified by:
      isAutoStartup in interface org.springframework.context.SmartLifecycle
      Returns:
      whether this lifecycle component should get started automatically by the container
    • setAutoStartup

      public void setAutoStartup(boolean autoStartup)
      Configure if this Lifecycle connection factory should get started automatically by the container.
      Parameters:
      autoStartup - true to automatically start() the connection factory; false otherwise.
    • isEarlyStartup

      public boolean isEarlyStartup()
      Returns:
      whether to start() the component during afterPropertiesSet().
    • setEarlyStartup

      public void setEarlyStartup(boolean earlyStartup)
      Configure if this InitializingBean's component Lifecycle should get started early by afterPropertiesSet() at the time that the bean is initialized. The component defaults to auto-startup.
      Parameters:
      earlyStartup - true to early start() the component; false otherwise.
    • getPhase

      public int getPhase()
      Specified by:
      getPhase in interface org.springframework.context.Phased
      Specified by:
      getPhase in interface org.springframework.context.SmartLifecycle
      Returns:
      the phase value for this lifecycle component
    • setPhase

      public void setPhase(int phase)
      Specify the lifecycle phase for pausing and resuming this executor.
      Parameters:
      phase - the phase value to set
    • translateExceptionIfPossible

      @Nullable public org.springframework.dao.DataAccessException translateExceptionIfPossible(RuntimeException ex)
      Translates a Valkey-Glide exception to a Spring DAO exception.
      Specified by:
      translateExceptionIfPossible in interface org.springframework.dao.support.PersistenceExceptionTranslator
      Parameters:
      ex - The exception to translate
      Returns:
      The translated exception, or null if the exception cannot be translated