类 InMemorySession
java.lang.Object
io.agentscope.core.session.InMemorySession
- 所有已实现的接口:
Session
In-memory implementation of the Session interface.
This implementation stores session state in memory using a ConcurrentHashMap. It is suitable for single-process applications where persistence across restarts is not required.
Thread Safety: This class is thread-safe. It uses ConcurrentHashMap for session storage and creates defensive copies of state data during save operations.
Limitations:
- State is lost when the JVM exits
- Not suitable for distributed environments
- Memory usage grows with number of sessions
Example usage:
Session session = new InMemorySession();
SessionKey sessionKey = SimpleSessionKey.of("user123");
// Save state
session.save(sessionKey, "agent_meta", new AgentMetaState("id", "name", "desc", "prompt"));
// Load state
Optional<AgentMetaState> meta = session.get(sessionKey, "agent_meta", AgentMetaState.class);
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明voidclearAll()Clear all sessions from memory.voiddelete(SessionKey sessionKey) Delete a session and all its data.voiddelete(SessionKey sessionKey, String key) Delete a single state entry within a session.booleanexists(SessionKey sessionKey) Check if a session exists.get(SessionKey sessionKey, String key, Class<T> type) Get a single state value.getList(SessionKey sessionKey, String key, Class<T> itemType) Get a list of state values.intGet the number of active sessions.List all session keys.voidsave(SessionKey sessionKey, String key, State value) Save a single state value.voidsave(SessionKey sessionKey, String key, List<? extends State> values) Save a list of state values (replacement).
-
构造器详细资料
-
InMemorySession
public InMemorySession()
-
-
方法详细资料
-
save
Save a single state value. -
save
Save a list of state values (replacement).Unlike JsonSession which uses incremental append, InMemorySession replaces the entire list. Callers should pass the full list, and InMemorySession stores it as-is.
-
get
Get a single state value. -
getList
Get a list of state values. -
exists
Check if a session exists. -
delete
Delete a session and all its data. -
delete
从接口复制的说明:SessionDelete a single state entry within a session. -
listSessionKeys
List all session keys.- 指定者:
listSessionKeys在接口中Session- 返回:
- set of all session keys
-
getSessionCount
public int getSessionCount()Get the number of active sessions.- 返回:
- Number of sessions currently stored
-
clearAll
public void clearAll()Clear all sessions from memory.This is useful for testing or when you want to reset all state.
-