InMemoryAgentStateStore
public class InMemoryAgentStateStore implements AgentStateStore
io/agentscope/core/state/InMemoryAgentStateStore.java:46–267In 记忆智能体状态存储,负责持久化与读取数据。位于 io.agentscope.core.state 包,实现 AgentStateStore。核心成员包括 save、supportsVersioning、getVersioned、saveIfVersion、get、getList。
In-memory implementation of the AgentStateStore interface.
This implementation stores agent state in memory using a ConcurrentHashMap. It is
suitable for single-process applications where persistence across restarts is not required.
Slot key: the (userId, sessionId) pair is the storage slot. null
userId is mapped to the literal "__anon__" bucket so anonymous sessions don't
collide with users actually named "".
Thread Safety: this class is thread-safe.
Limitations:
-
State is lost when the JVM exits
-
Not suitable for distributed environments
-
Memory usage grows with the number of sessions
方法摘要Method Summary
方法详情Method Detail
save
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/core/state/InMemoryAgentStateStore.java:54–58supportsVersioning
public boolean supportsVersioning()
返回 supportsVersioning 结果。
Returns the supports versioning.
booleanio/agentscope/core/state/InMemoryAgentStateStore.java:60–63getVersioned
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/core/state/InMemoryAgentStateStore.java:65–87saveIfVersion
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/core/state/InMemoryAgentStateStore.java:89–101save
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/core/state/InMemoryAgentStateStore.java:103–107get
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/core/state/InMemoryAgentStateStore.java:109–130getList
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/core/state/InMemoryAgentStateStore.java:132–145exists
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/core/state/InMemoryAgentStateStore.java:147–150delete
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/core/state/InMemoryAgentStateStore.java:152–158delete
public void delete(String userId, String sessionId, String key)
删除数据。
Performs the delete operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
key | String | 键(类型 `String`)。The key. |
io/agentscope/core/state/InMemoryAgentStateStore.java:160–166listSessionIds
public Set<String> listSessionIds(String userId)
列出 listSessionIds。
Lists the session ids.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
Set<String>io/agentscope/core/state/InMemoryAgentStateStore.java:168–175getSessionCount
public int getSessionCount()
获取会话数量。
Total number of (userId, sessionId) slots currently stored.
intio/agentscope/core/state/InMemoryAgentStateStore.java:178–180clearAll
public void clearAll()
清空所有。
Wipe everything (useful for testing).
io/agentscope/core/state/InMemoryAgentStateStore.java:183–185