类 InMemorySession

java.lang.Object
io.agentscope.core.session.InMemorySession
所有已实现的接口:
Session

public class InMemorySession extends Object implements 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);
 
  • 构造器详细资料

    • InMemorySession

      public InMemorySession()
  • 方法详细资料

    • save

      public void save(SessionKey sessionKey, String key, State value)
      Save a single state value.
      指定者:
      save 在接口中 Session
      参数:
      sessionKey - the session identifier
      key - the state key (e.g., "agent_meta", "toolkit_activeGroups")
      value - the state value to save
    • save

      public void save(SessionKey sessionKey, String key, List<? extends State> values)
      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.

      指定者:
      save 在接口中 Session
      参数:
      sessionKey - the session identifier
      key - the state key (e.g., "memory_messages")
      values - the full list of state values to store
    • get

      public <T extends State> Optional<T> get(SessionKey sessionKey, String key, Class<T> type)
      Get a single state value.
      指定者:
      get 在接口中 Session
      类型参数:
      T - the state type
      参数:
      sessionKey - the session identifier
      key - the state key
      type - the expected state type
      返回:
      the state value, or empty if not found
    • getList

      public <T extends State> List<T> getList(SessionKey sessionKey, String key, Class<T> itemType)
      Get a list of state values.
      指定者:
      getList 在接口中 Session
      类型参数:
      T - the item type
      参数:
      sessionKey - the session identifier
      key - the state key
      itemType - the expected item type
      返回:
      the list of state values, or empty list if not found
    • exists

      public boolean exists(SessionKey sessionKey)
      Check if a session exists.
      指定者:
      exists 在接口中 Session
      参数:
      sessionKey - the session identifier
      返回:
      true if the session exists
    • delete

      public void delete(SessionKey sessionKey)
      Delete a session and all its data.
      指定者:
      delete 在接口中 Session
      参数:
      sessionKey - the session identifier
    • delete

      public void delete(SessionKey sessionKey, String key)
      从接口复制的说明: Session
      Delete a single state entry within a session.
      指定者:
      delete 在接口中 Session
      参数:
      sessionKey - the session identifier
      key - the state key to delete
    • listSessionKeys

      public Set<SessionKey> 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.