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();
-
Nested Class Summary
Nested Classes -
Field Summary
Fields inherited from interface io.agentscope.core.state.AgentStateStore
UNVERSIONED -
Method Summary
Modifier and TypeMethodDescriptionstatic RedisAgentStateStore.Builderbuilder()Creates a new builder forRedisAgentStateStore.reactor.core.publisher.Mono<Integer>Clear all sessions stored in Redis (for testing or cleanup).voidclose()voidboolean<T extends io.agentscope.core.state.State>
Optional<T><T extends io.agentscope.core.state.State>
List<T><T extends io.agentscope.core.state.State>
io.agentscope.core.state.VersionedState<T>getVersioned(String userId, String sessionId, String key, Class<T> type) listSessionIds(String userId) voidvoidsave(String userId, String sessionId, String key, List<? extends io.agentscope.core.state.State> values) longsaveIfVersion(String userId, String sessionId, String key, io.agentscope.core.state.State value, long expectedVersion) booleanMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.agentscope.core.state.AgentStateStore
delete
-
Method Details
-
builder
Creates a new builder forRedisAgentStateStore.- Returns:
- a new Builder instance
-
supportsVersioning
public boolean supportsVersioning()- Specified by:
supportsVersioningin interfaceio.agentscope.core.state.AgentStateStore
-
save
- Specified by:
savein interfaceio.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:
getVersionedin interfaceio.agentscope.core.state.AgentStateStore
-
saveIfVersion
public long saveIfVersion(String userId, String sessionId, String key, io.agentscope.core.state.State value, long expectedVersion) - Specified by:
saveIfVersionin interfaceio.agentscope.core.state.AgentStateStore
-
save
public void save(String userId, String sessionId, String key, List<? extends io.agentscope.core.state.State> values) - Specified by:
savein interfaceio.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:
getin interfaceio.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:
getListin interfaceio.agentscope.core.state.AgentStateStore
-
exists
- Specified by:
existsin interfaceio.agentscope.core.state.AgentStateStore
-
delete
- Specified by:
deletein interfaceio.agentscope.core.state.AgentStateStore
-
listSessionIds
- Specified by:
listSessionIdsin interfaceio.agentscope.core.state.AgentStateStore
-
close
public void close()- Specified by:
closein interfaceio.agentscope.core.state.AgentStateStore
-
clearAllSessions
Clear all sessions stored in Redis (for testing or cleanup).- Returns:
- Mono that completes with the number of deleted session keys
-