Class RedisAgentStateStore

java.lang.Object
io.agentscope.extensions.redis.state.RedisAgentStateStore
All Implemented Interfaces:
io.agentscope.core.state.AgentStateStore

public class RedisAgentStateStore extends Object implements io.agentscope.core.state.AgentStateStore
Redis-based session implementation supporting multiple Redis clients.

This implementation provides a unified interface for Redis-based session storage, supporting multiple Redis client implementations:

  • Jedis - Standalone, Cluster, Sentinel
  • Lettuce - Standalone, Cluster, Sentinel
  • Redisson - Standalone, Cluster, Sentinel, Master/Slave

The session state is stored in Redis with following key structure:

  • Single state: {prefix}{sessionId}:{stateKey} - Redis String containing JSON
  • List state: {prefix}{sessionId}:{stateKey}:list - Redis List containing JSON items
  • List hash: {prefix}{sessionId}:{stateKey}:list:_hash - Hash for change detection
  • AgentStateStore marker: {prefix}{sessionId}:_keys - Redis Set tracking all state keys

Jedis Usage Examples:

Jedis Standalone (using RedisClient):


 // Create Jedis RedisClient (new API)
 RedisClient redisClient = RedisClient.create("redis://localhost:6379");

 // Build RedisAgentStateStore
 AgentStateStore stateStore = RedisAgentStateStore.builder()
     .jedisClient(redisClient)
     .build();
 

Jedis Cluster (using RedisClusterClient):


 // Create Jedis RedisClusterClient
 Set<HostAndPort> nodes = new HashSet<>();
 nodes.add(new HostAndPort("localhost", 7000));
 nodes.add(new HostAndPort("localhost", 7001));
 nodes.add(new HostAndPort("localhost", 7002));
 RedisClusterClient redisClusterClient = RedisClusterClient.create(nodes);

 // Build RedisAgentStateStore
 AgentStateStore stateStore = RedisAgentStateStore.builder()
     .jedisClient(redisClusterClient)
     .build();
 

Jedis Sentinel (using RedisSentinelClient):


 // Create Jedis RedisSentinelClient
 Set<String> sentinelNodes = new HashSet<>();
 sentinelNodes.add("localhost:26379");
 sentinelNodes.add("localhost:26380");
 RedisSentinelClient redisSentinelClient = RedisSentinelClient.create("mymaster", sentinelNodes);

 // Build RedisAgentStateStore
 AgentStateStore stateStore = RedisAgentStateStore.builder()
     .jedisClient(redisSentinelClient)
     .build();
 

Lettuce Usage Examples:

Lettuce Standalone:


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

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

Lettuce Cluster:


 // Create Lettuce RedisClusterClient for cluster mode
 RedisClusterClient clusterClient = RedisClusterClient.create(
     RedisURI.create("localhost", 7000));

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

Lettuce Sentinel:


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

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

Redisson Usage Example:


 // Create RedissonClient (configure as needed for your deployment mode)
 Config config = new Config();
 config.useSingleServer().setAddress("redis://localhost:6379");
 // or for cluster: config.useClusterServers().addNodeAddress("redis://localhost:7000");
 // or for sentinel: config.useSentinelServers().setMasterName("mymaster").addSentinelAddress("redis://localhost:26379");

 RedissonClient redissonClient = Redisson.create(config);

 // Build RedisAgentStateStore
 AgentStateStore stateStore = RedisAgentStateStore.builder()
     .redissonClient(redissonClient)
     .build();
 

Custom Key Prefix Example:


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

 // Build RedisAgentStateStore with custom key prefix
 AgentStateStore stateStore = RedisAgentStateStore.builder()
     .jedisClient(redisClient)
     .keyPrefix("myapp:session:")
     .build();
 
  • Method Details

    • builder

      public static RedisAgentStateStore.Builder builder()
      Creates a new builder for RedisAgentStateStore.
      Returns:
      a new Builder instance
    • supportsVersioning

      public boolean supportsVersioning()
      Specified by:
      supportsVersioning in interface io.agentscope.core.state.AgentStateStore
    • save

      public void save(String userId, String sessionId, String key, io.agentscope.core.state.State value)
      Specified by:
      save in interface io.agentscope.core.state.AgentStateStore
    • getVersioned

      public <T extends io.agentscope.core.state.State> io.agentscope.core.state.VersionedState<T> getVersioned(String userId, String sessionId, String key, Class<T> type)
      Specified by:
      getVersioned in interface io.agentscope.core.state.AgentStateStore
    • saveIfVersion

      public long saveIfVersion(String userId, String sessionId, String key, io.agentscope.core.state.State value, long expectedVersion)
      Specified by:
      saveIfVersion in interface io.agentscope.core.state.AgentStateStore
    • save

      public void save(String userId, String sessionId, String key, List<? extends io.agentscope.core.state.State> values)
      Specified by:
      save in interface io.agentscope.core.state.AgentStateStore
    • get

      public <T extends io.agentscope.core.state.State> Optional<T> get(String userId, String sessionId, String key, Class<T> type)
      Specified by:
      get in interface io.agentscope.core.state.AgentStateStore
    • getList

      public <T extends io.agentscope.core.state.State> List<T> getList(String userId, String sessionId, String key, Class<T> itemType)
      Specified by:
      getList in interface io.agentscope.core.state.AgentStateStore
    • exists

      public boolean exists(String userId, String sessionId)
      Specified by:
      exists in interface io.agentscope.core.state.AgentStateStore
    • delete

      public void delete(String userId, String sessionId)
      Specified by:
      delete in interface io.agentscope.core.state.AgentStateStore
    • listSessionIds

      public Set<String> listSessionIds(String userId)
      Specified by:
      listSessionIds in interface io.agentscope.core.state.AgentStateStore
    • close

      public void close()
      Specified by:
      close in interface io.agentscope.core.state.AgentStateStore
    • clearAllSessions

      public reactor.core.publisher.Mono<Integer> clearAllSessions()
      Clear all sessions stored in Redis (for testing or cleanup).
      Returns:
      Mono that completes with the number of deleted session keys