Class LettuceClientAdapter

java.lang.Object
io.agentscope.extensions.redis.state.lettuce.LettuceClientAdapter
All Implemented Interfaces:
RedisClientAdapter

public class LettuceClientAdapter extends Object implements RedisClientAdapter
Adapter for Lettuce Redis client.

This adapter supports multiple Redis deployment modes through different client types:

  • RedisClient - Standalone and Sentinel modes
  • RedisClusterClient - Cluster mode

The adapter internally manages a shared connection and commands instance for efficient connection usage.

This implementation uses direct conditional checks to handle Lettuce's separate command APIs for standalone/sentinel and cluster modes. This is the simplest approach given Lettuce's lack of a unified API across deployment modes.

Usage Examples:

Standalone Mode:


 // Create standalone RedisClient
 RedisClient redisClient = RedisClient.create("redis://localhost:6379");

 // Use with RedisAgentStateStore
 AgentStateStore stateStore = RedisAgentStateStore.builder()
     .lettuceClient(redisClient)
     .build();
 

Cluster Mode:


 // Create cluster client
 RedisClusterClient clusterClient = RedisClusterClient.create("redis://localhost:7000");

 // Use with RedisAgentStateStore
 AgentStateStore stateStore = RedisAgentStateStore.builder()
     .lettuceClusterClient(clusterClient)
     .build();
 

Sentinel Mode:


 // Create Lettuce RedisClient for sentinel
 RedisURI sentinelUri = RedisURI.builder()
     .withSentinelMasterId("mymaster")
     .withSentinel("localhost", 26379)
     .withSentinel("localhost", 26380)
     .withSentinel("localhost", 26381)
     .withDatabase(0)
     .build();
 RedisClient redisClient = RedisClient.create(sentinelUri);

 // Use with RedisAgentStateStore
 AgentStateStore stateStore = RedisAgentStateStore.builder()
     .lettuceClient(redisClient)
     .build();
 
  • Method Details

    • of

      public static LettuceClientAdapter of(io.lettuce.core.RedisClient redisClient)
      Create adapter from RedisClient (standalone/sentinel mode).
      Parameters:
      redisClient - the RedisClient for standalone or sentinel mode
      Returns:
      a new LettuceClientAdapter
      Throws:
      NullPointerException - if redisClient is null
    • of

      public static LettuceClientAdapter of(io.lettuce.core.cluster.RedisClusterClient redisClusterClient)
      Create adapter from RedisClusterClient (cluster mode).
      Parameters:
      redisClusterClient - the RedisClusterClient for cluster mode
      Returns:
      a new LettuceClientAdapter
      Throws:
      NullPointerException - if redisClusterClient is null
    • set

      public void set(String key, String value)
      Description copied from interface: RedisClientAdapter
      Set a string value.
      Specified by:
      set in interface RedisClientAdapter
      Parameters:
      key - the Redis key
      value - the string value
    • get

      public String get(String key)
      Description copied from interface: RedisClientAdapter
      Get a string value.
      Specified by:
      get in interface RedisClientAdapter
      Parameters:
      key - the Redis key
      Returns:
      the string value, or null if not found
    • rightPushList

      public void rightPushList(String key, String value)
      Description copied from interface: RedisClientAdapter
      Append a value to the right end of a list.
      Specified by:
      rightPushList in interface RedisClientAdapter
      Parameters:
      key - the Redis list key
      value - the value to append
    • rangeList

      public List<String> rangeList(String key, long start, long end)
      Description copied from interface: RedisClientAdapter
      Get a range of elements from a list.
      Specified by:
      rangeList in interface RedisClientAdapter
      Parameters:
      key - the Redis list key
      start - the start index (inclusive)
      end - the end index (inclusive, -1 for all elements)
      Returns:
      list of values
    • getListLength

      public long getListLength(String key)
      Description copied from interface: RedisClientAdapter
      Get the length of a list.
      Specified by:
      getListLength in interface RedisClientAdapter
      Parameters:
      key - the Redis list key
      Returns:
      the length of the list
    • deleteKeys

      public void deleteKeys(String... keys)
      Description copied from interface: RedisClientAdapter
      Delete one or more keys.
      Specified by:
      deleteKeys in interface RedisClientAdapter
      Parameters:
      keys - the keys to delete
    • addToSet

      public void addToSet(String key, String member)
      Description copied from interface: RedisClientAdapter
      Add a member to a set.
      Specified by:
      addToSet in interface RedisClientAdapter
      Parameters:
      key - the Redis set key
      member - the member to add
    • getSetMembers

      public Set<String> getSetMembers(String key)
      Description copied from interface: RedisClientAdapter
      Get all members of a set.
      Specified by:
      getSetMembers in interface RedisClientAdapter
      Parameters:
      key - the Redis set key
      Returns:
      set of members
    • getSetSize

      public long getSetSize(String key)
      Description copied from interface: RedisClientAdapter
      Get the number of members in a set.
      Specified by:
      getSetSize in interface RedisClientAdapter
      Parameters:
      key - the Redis set key
      Returns:
      the number of members
    • keyExists

      public boolean keyExists(String key)
      Description copied from interface: RedisClientAdapter
      Check if a key exists.
      Specified by:
      keyExists in interface RedisClientAdapter
      Parameters:
      key - the Redis key
      Returns:
      true if the key exists
    • findKeysByPattern

      public Set<String> findKeysByPattern(String pattern)
      Description copied from interface: RedisClientAdapter
      Find all keys matching a pattern.
      Specified by:
      findKeysByPattern in interface RedisClientAdapter
      Parameters:
      pattern - the key pattern (e.g., "prefix:*")
      Returns:
      set of matching keys
    • evalScript

      public long evalScript(String script, List<String> keys, List<String> args)
      Description copied from interface: RedisClientAdapter
      Execute a Lua script atomically.
      Specified by:
      evalScript in interface RedisClientAdapter
      Parameters:
      script - the Lua script source
      keys - Redis keys passed as KEYS
      args - Redis arguments passed as ARGV
      Returns:
      the script return value as a long
    • close

      public void close()
      Description copied from interface: RedisClientAdapter
      Close the adapter and release resources.
      Specified by:
      close in interface RedisClientAdapter