Class RedisManager

java.lang.Object
net.blockhost.commons.database.redis.RedisManager

public final class RedisManager extends Object

Redis connection manager with Jedis connection pooling.

This class provides a complete solution for managing Redis connections in plugins, including connection pooling, configuration reloads, and reconnection handling.

Features

  • Connection Pooling: Uses JedisPool for efficient connection management
  • Config Reloads: Supports hot-reloading Redis configuration
  • Auto-Reconnect: Automatically attempts to reconnect on connection loss
  • Thread Safety: All operations are thread-safe

Example Usage

RedisManager redisManager = RedisManager.builder()
    .config(redisConfig)
    .logger(plugin.getLogger())
    .build();

// Connect to Redis
redisManager.connect();

// Use connections with callback pattern
redisManager.withConnection(jedis -> {
    jedis.set("key", "value");
});

// Get values
String value = redisManager.withConnectionResult(jedis -> jedis.get("key"));

// Reload configuration
redisManager.reload(newConfig);

// Shutdown when done
redisManager.shutdown();
See Also:
  • Method Details

    • builder

      public static RedisManager.Builder builder()
      Creates a new builder for RedisManager.
      Returns:
      a new builder instance
    • connect

      public void connect()

      Connects to Redis and initializes the connection pool.

      This method will create the Jedis connection pool using the current configuration. If already connected, this method does nothing.

    • getConnection

      public @Nullable redis.clients.jedis.Jedis getConnection()

      Gets a Jedis connection from the pool.

      This method returns null instead of throwing an exception when unable to get a connection. This allows for easier error handling in calling code.

      Important: The returned connection must be closed after use. Use try-with-resources for automatic cleanup.

      Returns:
      a Jedis connection, or null if unavailable
    • withConnection

      public boolean withConnection(Consumer<redis.clients.jedis.Jedis> action)

      Executes an action with a Redis connection.

      This is a convenience method that handles connection retrieval and automatic cleanup. The connection is automatically closed after the action completes.

      Parameters:
      action - the action to execute with the connection
      Returns:
      true if the action executed successfully, false if no connection was available
    • withConnectionResult

      public <T> @Nullable T withConnectionResult(Function<redis.clients.jedis.Jedis, T> action)
      Executes an action with a Redis connection and returns a result.
      Type Parameters:
      T - the type of result
      Parameters:
      action - the action to execute with the connection
      Returns:
      the result, or null if no connection was available or an error occurred
    • reconnect

      public void reconnect()

      Attempts to reconnect to Redis.

      This method closes the existing connection pool (if any) and creates a new one with the current configuration.

    • reload

      public void reload(RedisConfig newConfig)

      Reloads the Redis configuration and reconnects.

      This method allows hot-reloading of Redis configuration. The existing connection pool is closed and a new one is created with the new configuration.

      Parameters:
      newConfig - the new Redis configuration
    • shutdown

      public void shutdown()

      Shuts down the connection pool.

      This method should be called when the plugin is disabled to properly release Redis resources.

    • isConnected

      public boolean isConnected()
      Checks if the manager is currently connected to Redis.
      Returns:
      true if connected, false otherwise
    • getConfig

      public RedisConfig getConfig()
      Gets the current Redis configuration.
      Returns:
      the current configuration