Class RedisConnectionManager

java.lang.Object
org.gcube.informationsystem.resourceregistry.redis.RedisConnectionManager

public class RedisConnectionManager extends Object
Singleton manager for the Lettuce Redis connection. Provides graceful degradation: if Redis is unavailable, isAvailable() returns false and callers must fall back to the database.
Author:
Luca Frosini (ISTI - CNR)
  • Method Details

    • getInstance

      public static RedisConnectionManager getInstance()
      Returns the singleton instance of RedisConnectionManager.
      Returns:
      the singleton instance
    • isAvailable

      public boolean isAvailable()
      Returns true if the Redis connection is established and open. It never waits: a lost connection is established again in the background.
      Returns:
      true if the connection is available, false otherwise
    • getCommands

      public io.lettuce.core.api.sync.RedisCommands<String,String> getCommands()
      Returns synchronous Redis commands if available, otherwise null. Callers must check isAvailable() or handle a null return.
      Returns:
      synchronous Redis commands interface, or null if unavailable
    • getAsyncCommands

      public io.lettuce.core.api.async.RedisAsyncCommands<String,String> getAsyncCommands()
      Returns asynchronous Redis commands if available, otherwise null.

      Intended for bulk writes that would otherwise pay one network round trip per command. Disable auto-flushing on the returned handle, issue the commands, flush once, then await the futures — and always restore auto-flushing afterwards, since the handle is backed by the shared connection every other caller uses.

      Returns:
      asynchronous Redis commands interface, or null if unavailable
    • setAutoFlushCommands

      public void setAutoFlushCommands(boolean autoFlush)
      Enables or disables automatic flushing on the shared connection.

      Disabling it lets a caller queue many commands and pay a single network round trip for all of them. The setting is a property of the connection every other caller shares, so a caller that disables it must re-enable it before returning, including on failure.

      Parameters:
      autoFlush - true to flush every command as it is issued, false to queue until flushCommands()
    • flushCommands

      public void flushCommands()
      Writes every command queued since auto-flushing was disabled.
    • deleteAllKeys

      public long deleteAllKeys()
      Deletes every key of this registry (RedisKeySpace.ALL_SCAN_PATTERN) with a SCAN loop, leaving any other key of the same Redis untouched.
      Returns:
      the number of deleted keys
      Throws:
      IllegalStateException - if Redis is not reachable
    • publish

      public void publish(String channel, String message)
      Publishes a message on the given Redis pub/sub channel, broadcasting it to every subscribed service instance. A no-op (silently) if Redis is unavailable.
      Parameters:
      channel - the pub/sub channel name
      message - the message payload
    • subscribe

      public void subscribe(String channel, Consumer<String> onMessage)
      Subscribes to the given Redis pub/sub channel, invoking onMessage for every message received on it for as long as the process runs. All subscriptions on this manager share a single dedicated pub/sub connection (a connection in subscriber mode cannot also issue regular commands, so it must be separate from connection). The subscription outlives the connection: it is established as soon as Redis is reachable and again after every reconnection.
      Parameters:
      channel - the pub/sub channel name to subscribe to
      onMessage - callback invoked with the message payload for every message received
    • close

      public void close()
      Closes the active Redis connection and releases client resources.