`Class InMemorySessionService`

java.lang.Object
com.google.adk.sessions.InMemorySessionService
All Implemented Interfaces:
BaseSessionService

public final class InMemorySessionService extends Object implements BaseSessionService
一种基于内存的实现方式(in-memory implementation) BaseSessionService 假设…… Session 这些对象的状态映射(state map)、事件列表(events list)以及最后更新时间(last update time)都是可变的(即可被修改的)。

这种实现方式将会话数据、用户状态以及应用程序状态直接存储在内存中,并使用“并发映射”(concurrent maps)来确保线程安全。它适用于测试环境或单节点部署场景,在这些场景中并不需要数据的持久化存储。

注意:状态合并(应用程序/用户状态前缀为“app/”或“user/”) _app_ / _user_这种情况发生在数据检索过程中。)getSession, createSession).

  • 构造函数详细信息

    • InMemorySessionService

      public InMemorySessionService()
      创建一个新的内存会话服务实例,该实例的存储空间为空。如果使用相同的会话 ID,新的会话会覆盖已存储的会话数据;此时,会话的默认设置将继续生效,直到调用者完成数据迁移。
    • InMemorySessionService

      public InMemorySessionService(InMemorySessionService.DuplicateSessionIdBehavior duplicateSessionIdBehavior)
      创建一个新的内存会话服务实例,该实例的存储空间为空(即不包含任何数据)。
      Parameters:
      duplicateSessionIdBehavior - What createSession does when the session ID is already in use.
    • InMemorySessionService

      public InMemorySessionService(InMemorySessionService.DuplicateSessionIdBehavior duplicateSessionIdBehavior, InstantSource instantSource, UuidProvider uuidProvider)
      创建一个新的内存会话服务实例,该实例的存储空间为空(即不包含任何数据)。
      Parameters:
      duplicateSessionIdBehavior - What createSession does when the session ID is already in use.
      instantSource - Supplies the lastUpdateTime of a newly created session.
      uuidProvider - Supplies the session ID when createSession is not given one.
  • 方法详细信息(Method Details)

    • createSession

      public io.reactivex.rxjava3.core.Single<Session> createSession(String appName, String userId, @Nullable ConcurrentMap<String,Object> state, @Nullable String sessionId)
      描述内容复制自接口文档: BaseSessionService
      使用指定的参数创建一个新的会话。
      Specified by:
      createSession in interface BaseSessionService
      Parameters:
      appName - The name of the application associated with the session.
      userId - The identifier for the user associated with the session.
      state - An optional map representing the initial state of the session. Can be null or empty.
      sessionId - An optional client-provided identifier for the session. If empty or null, the service should generate a unique ID.
      Returns:
      The newly created Session instance.
    • createSession

      public io.reactivex.rxjava3.core.Single<Session> createSession(String appName, String userId, @Nullable Map<String,Object> state, @Nullable String sessionId)
      描述内容复制自接口文档: BaseSessionService
      使用指定的参数创建一个新的会话。
      Specified by:
      createSession in interface BaseSessionService
      Parameters:
      appName - The name of the application associated with the session.
      userId - The identifier for the user associated with the session.
      state - An optional map representing the initial state of the session. Can be null or empty.
      sessionId - An optional client-provided identifier for the session. If empty or null, the service should generate a unique ID.
      Returns:
      The newly created Session instance.
    • getSession

      public io.reactivex.rxjava3.core.Maybe<Session> getSession(String appName, String userId, String sessionId, Optional<GetSessionConfig> configOpt)
      描述内容复制自接口文档: BaseSessionService
      检索特定的会话(session),并可选择性地过滤其中包含的事件(events)。
      Specified by:
      getSession in interface BaseSessionService
      Parameters:
      appName - The name of the application.
      userId - The identifier of the user.
      sessionId - The unique identifier of the session to retrieve.
      configOpt - Optional configuration to filter the events returned within the session (e.g., limit number of recent events, filter by timestamp). If empty, default retrieval behavior is used (potentially all events or a service-defined limit).
      Returns:
      An Optional containing the Session if found, otherwise Optional.empty().
    • listSessions

      public io.reactivex.rxjava3.core.Single<ListSessionsResponse> listSessions(String appName, String userId)
      描述内容复制自接口文档: BaseSessionService
      列出与特定应用程序和用户关联的会话(session)。

      The Session 响应中的对象通常只包含元数据(如 ID、创建时间等),而不包含完整的事件列表或系统状态,这样有助于优化性能。

      Specified by:
      listSessions in interface BaseSessionService
      Parameters:
      appName - The name of the application.
      userId - The identifier of the user whose sessions are to be listed.
      Returns:
      A ListSessionsResponse containing a list of matching sessions.
    • deleteSession

      public io.reactivex.rxjava3.core.Completable deleteSession(String appName, String userId, String sessionId)
      描述内容复制自接口文档: BaseSessionService
      删除特定的会话。
      Specified by:
      deleteSession in interface BaseSessionService
      Parameters:
      appName - The name of the application.
      userId - The identifier of the user.
      sessionId - The unique identifier of the session to delete.
    • listEvents

      public io.reactivex.rxjava3.core.Single<ListEventsResponse> listEvents(String appName, String userId, String sessionId)
      描述内容复制自接口文档: BaseSessionService
      列出特定会话中的所有事件。支持通过响应对象实现分页功能。
      Specified by:
      listEvents in interface BaseSessionService
      Parameters:
      appName - The name of the application.
      userId - The identifier of the user.
      sessionId - The unique identifier of the session whose events are to be listed.
      Returns:
      A ListEventsResponse containing a list of events and an optional token for retrieving the next page.
    • appendEvent

      @CanIgnoreReturnValue public io.reactivex.rxjava3.core.Single<Event> appendEvent(Session session, Event event)
      描述内容复制自接口文档: BaseSessionService
      将一个事件添加到内存中的会话对象中,并根据该事件的状态变化(如果适用的话)更新会话的状态。

      此方法主要修改传入的参数(即 `passed` 参数)。 session 这些数据存储在内存中。要使这些更改持久化(即让它们在系统重启后仍然存在),通常需要调用特定服务提供的 `update` 或 `save` 方法;不过,根据该服务的实现方式,这一过程也可能被隐式地完成(即无需用户进行任何操作)。

      如果该事件被标记为“部分完成”(例如:……), event.isPartial() == true当数据被返回时,它会直接被发送出去,而不会修改会话状态或事件列表。以 “State delta keys starting with” 开头的键用于表示状态变化的相关信息。 State.TEMP_PREFIX 在状态更新过程中,这些内容会被忽略。

      Specified by:
      appendEvent in interface BaseSessionService
      Parameters:
      session - The Session object to which the event should be appended (will be mutated).
      event - The Event to append.
      Returns:
      The appended Event instance (or the original event if it was partial).