RedisAgentStateStore
public class RedisAgentStateStore implements AgentStateStore
io/agentscope/extensions/redis/state/RedisAgentStateStore.java:178–565Redis 智能体状态存储,负责持久化与读取数据。位于 io.agentscope.extensions.redis.state 包,实现 AgentStateStore。核心成员包括 builder、supportsVersioning、save、getVersioned、saveIfVersion、get。
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
| 类Class | 类型Kind | 说明Description |
|---|---|---|
Builder | 类class | `RedisAgentStateStore`的构建器。 Builder for `RedisAgentStateStore`. |
方法摘要Method Summary
方法详情Method Detail
builder
public static Builder builder()
创建新的构建器用于RedisAgentStateStore。
Creates a new builder for RedisAgentStateStore.
Builderio/agentscope/extensions/redis/state/RedisAgentStateStore.java:208–210supportsVersioning
public boolean supportsVersioning()
返回 supportsVersioning 结果。
Returns the supports versioning.
booleanio/agentscope/extensions/redis/state/RedisAgentStateStore.java:212–215save
public void save(String userId, String sessionId, String key, State value)
保存数据到持久化存储。
Performs the save operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
key | String | 键(类型 `String`)。The key. |
value | State | 值(类型 `State`)。The value. |
io/agentscope/extensions/redis/state/RedisAgentStateStore.java:217–220getVersioned
public T extends State VersionedState<T> getVersioned(String userId, String sessionId, String key, Class<T> type)
获取 getVersioned。
Returns the versioned.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
key | String | 键(类型 `String`)。The key. |
type | Class<T> | 类型(类型 `Class`)。The type. |
VersionedState<T>io/agentscope/extensions/redis/state/RedisAgentStateStore.java:251–267saveIfVersion
public long saveIfVersion(String userId, String sessionId, String key, State value, long expectedVersion)
保存If 版本。
Saves the if version.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
key | String | 键(类型 `String`)。The key. |
value | State | 值(类型 `State`)。The value. |
expectedVersion | long | expected 版本(类型 `long`)。The expected version. |
longio/agentscope/extensions/redis/state/RedisAgentStateStore.java:269–293save
public void save(String userId, String sessionId, String key, List<? extends State> values)
保存数据到持久化存储。
Performs the save operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
key | String | 键(类型 `String`)。The key. |
values | List<? extends State> | 值(类型 `List`)。The values. |
io/agentscope/extensions/redis/state/RedisAgentStateStore.java:295–334get
public T extends State Optional<T> get(String userId, String sessionId, String key, Class<T> type)
按条件读取数据。
Performs the get operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
key | String | 键(类型 `String`)。The key. |
type | Class<T> | 类型(类型 `Class`)。The type. |
Optional<T>io/agentscope/extensions/redis/state/RedisAgentStateStore.java:336–350getList
public T extends State List<T> getList(String userId, String sessionId, String key, Class<T> itemType)
获取列表。
Returns the list.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
key | String | 键(类型 `String`)。The key. |
itemType | Class<T> | 条目类型(类型 `Class`)。The item type. |
List<T>io/agentscope/extensions/redis/state/RedisAgentStateStore.java:352–371exists
public boolean exists(String userId, String sessionId)
判断数据是否存在。
Performs the exists operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
booleanio/agentscope/extensions/redis/state/RedisAgentStateStore.java:373–382delete
public void delete(String userId, String sessionId)
删除数据。
Performs the delete operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
io/agentscope/extensions/redis/state/RedisAgentStateStore.java:384–415listSessionIds
public Set<String> listSessionIds(String userId)
列出 listSessionIds。
Lists the session ids.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
Set<String>io/agentscope/extensions/redis/state/RedisAgentStateStore.java:417–436close
public void close()
关闭连接并释放相关资源。
Releases resources and connections.
io/agentscope/extensions/redis/state/RedisAgentStateStore.java:453–456clearAllSessions
public Mono<Integer> clearAllSessions()
清空所有会话。
Clear all sessions stored in Redis (for testing or cleanup).
Mono<Integer>io/agentscope/extensions/redis/state/RedisAgentStateStore.java:463–477嵌套类详情Nested Class Detail
Builder
public static class BuilderRedisAgentStateStore的构建器。位于 io.agentscope.extensions.redis.state 包。核心成员包括 keyPrefix、jedisClient、lettuceClient、lettuceClusterClient、redissonClient、clientAdapter。
Builder for RedisAgentStateStore.
The builder supports multiple Redis client types. Only one client type should be set.
Supported client types:
-
Jedis:
#jedisClient(UnifiedJedis) -
Lettuce Standalone/Sentinel:
#lettuceClient(RedisClient) -
Lettuce Cluster:
#lettuceClusterClient(RedisClusterClient) -
Redisson:
#redissonClient(RedissonClient) -
Custom:
#clientAdapter(RedisClientAdapter)