MysqlSession
public class MysqlSession implements Session
io/agentscope/core/session/mysql/MysqlSession.java:70–846MySQL 会话,在多次调用之间保存和恢复会话与记忆数据。位于 io.agentscope.core.session.mysql 包,实现 Session。核心成员包括 save、get、getList、exists、delete、listSessionKeys。
MySQL database-based session implementation.
This implementation stores session state in MySQL database tables with the following structure:
-
Single state: stored as JSON with item_index = 0
-
List state: each item stored in a separate row with item_index = 0, 1, 2, ...
Table Schema (auto-created if createIfNotExist=true):
CREATE TABLE IF NOT EXISTS agentscope_sessions (
session_id VARCHAR(255) NOT NULL,
state_key VARCHAR(255) NOT NULL,
item_index INT NOT NULL DEFAULT 0,
state_data LONGTEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (session_id, state_key, item_index)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Features:
-
True incremental list storage (only INSERTs new items, no read-modify-write)
-
Type-safe state serialization using Jackson
-
Automatic table creation
-
SQL injection prevention through parameterized queries
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public MysqlSession(DataSource dataSource) | 创建MysqlSession 使用默认设置。 Create a MysqlSession with default settings. |
public MysqlSession(DataSource dataSource, boolean createIfNotExist) | 初始化 `MysqlSession` 实例。 Create a MysqlSession with optional auto-creation of database and table. |
public MysqlSession(DataSource dataSource, String databaseName, String tableName, boolean createIfNotExist) | 初始化 `MysqlSession` 实例。 Create a MysqlSession with custom database name, table name, and optional auto-creation. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public void save(SessionKey sessionKey, String key, State value) | 保存数据到持久化存储。 Performs the save operation. |
public void save(SessionKey sessionKey, String key, List<? extends State> values) | 保存数据到持久化存储。 Save a list of state values with hash-based change detection. |
public T extends State Optional<T> get(SessionKey sessionKey, String key, Class<T> type) | 按条件读取数据。 Performs the get operation. |
public T extends State List<T> getList(SessionKey sessionKey, String key, Class<T> itemType) | 获取列表。 Returns the list. |
public boolean exists(SessionKey sessionKey) | 判断数据是否存在。 Performs the exists operation. |
public void delete(SessionKey sessionKey) | 删除数据。 Performs the delete operation. |
public Set<SessionKey> listSessionKeys() | 列出会话密钥。 Lists the session keys. |
public void close() | 关闭连接并释放相关资源。 Close the session and release any resources. |
public String getDatabaseName() | 获取数据库名称。 Get the database name used for storing sessions. |
public String getTableName() | 获取 `getTableName`。 Get the table name used for storing sessions. |
public DataSource getDataSource() | 获取数据来源。 Get the DataSource used for database connections. |
public int clearAllSessions()deprecated | 清空所有会话。 Clear all sessions from the database (for testing or cleanup). |
public int truncateAllSessions() | 返回 `truncateAllSessions` 结果。 Truncate session table from the database (for testing or cleanup). |
protected void validateSessionId(String sessionId) | 校验会话标识。 Validate a session ID format. |
构造器详情Constructor Detail
MysqlSession
public MysqlSession(DataSource dataSource)
创建MysqlSession 使用默认设置。
Create a MysqlSession with default settings.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
dataSource | DataSource | 数据来源(类型 `DataSource`)。DataSource for database connections |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if dataSource is nullIllegalStateException可能抛出 `IllegalStateException`。if database or table does not exist
io/agentscope/core/session/mysql/MysqlSession.java:112–114MysqlSession
public MysqlSession(DataSource dataSource, boolean createIfNotExist)
初始化 MysqlSession 实例。
Create a MysqlSession with optional auto-creation of database and table.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
dataSource | DataSource | 数据来源(类型 `DataSource`)。DataSource for database connections |
createIfNotExist | boolean | 创建 If 不 Exist(类型 `boolean`)。If true, auto-create database and table; if false, require existing |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if dataSource is nullIllegalStateException可能抛出 `IllegalStateException`。if createIfNotExist is false and database/table does not exist
io/agentscope/core/session/mysql/MysqlSession.java:129–131MysqlSession
public MysqlSession(DataSource dataSource, String databaseName, String tableName, boolean createIfNotExist)
初始化 MysqlSession 实例。
Create a MysqlSession with custom database name, table name, and optional auto-creation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
dataSource | DataSource | 数据来源(类型 `DataSource`)。DataSource for database connections |
databaseName | String | 数据库名称(类型 `String`)。Custom database name (uses default if null or empty) |
tableName | String | table 名称(类型 `String`)。Custom table name (uses default if null or empty) |
createIfNotExist | boolean | 创建 If 不 Exist(类型 `boolean`)。If true, auto-create database and table; if false, require existing |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if dataSource is nullIllegalStateException可能抛出 `IllegalStateException`。if createIfNotExist is false and database/table does not exist
io/agentscope/core/session/mysql/MysqlSession.java:147–179方法详情Method Detail
save
public void save(SessionKey sessionKey, String key, State value)
保存数据到持久化存储。
Performs the save operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
sessionKey | SessionKey | 会话键(类型 `SessionKey`)。The session key. |
key | String | 键(类型 `String`)。The key. |
value | State | 值(类型 `State`)。The value. |
io/agentscope/core/session/mysql/MysqlSession.java:325–356save
public void save(SessionKey sessionKey, String key, List<? extends State> values)
保存数据到持久化存储。
Save a list of state values with hash-based change detection.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
sessionKey | SessionKey | 会话键(类型 `SessionKey`)。the session identifier |
key | String | 键(类型 `String`)。the state key (e.g., "memory_messages") |
values | List<? extends State> | 状态值到保存的列出。the list of state values to save |
io/agentscope/core/session/mysql/MysqlSession.java:374–418get
public T extends State Optional<T> get(SessionKey sessionKey, String key, Class<T> type)
按条件读取数据。
Performs the get operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
sessionKey | SessionKey | 会话键(类型 `SessionKey`)。The session key. |
key | String | 键(类型 `String`)。The key. |
type | Class<T> | 类型(类型 `Class`)。The type. |
Optional<T>io/agentscope/core/session/mysql/MysqlSession.java:571–600getList
public T extends State List<T> getList(SessionKey sessionKey, String key, Class<T> itemType)
获取列表。
Returns the list.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
sessionKey | SessionKey | 会话键(类型 `SessionKey`)。The session key. |
key | String | 键(类型 `String`)。The key. |
itemType | Class<T> | 条目类型(类型 `Class`)。The item type. |
List<T>io/agentscope/core/session/mysql/MysqlSession.java:602–632exists
public boolean exists(SessionKey sessionKey)
判断数据是否存在。
Performs the exists operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
sessionKey | SessionKey | 会话键(类型 `SessionKey`)。The session key. |
booleanio/agentscope/core/session/mysql/MysqlSession.java:634–652delete
public void delete(SessionKey sessionKey)
删除数据。
Performs the delete operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
sessionKey | SessionKey | 会话键(类型 `SessionKey`)。The session key. |
io/agentscope/core/session/mysql/MysqlSession.java:654–673listSessionKeys
public Set<SessionKey> listSessionKeys()
列出会话密钥。
Lists the session keys.
Set<SessionKey>io/agentscope/core/session/mysql/MysqlSession.java:675–693close
public void close()
关闭连接并释放相关资源。
Close the session and release any resources.
io/agentscope/core/session/mysql/MysqlSession.java:701–704getDatabaseName
public String getDatabaseName()
获取数据库名称。
Get the database name used for storing sessions.
Stringio/agentscope/core/session/mysql/MysqlSession.java:711–713getTableName
public String getTableName()
获取 getTableName。
Get the table name used for storing sessions.
Stringio/agentscope/core/session/mysql/MysqlSession.java:720–722getDataSource
public DataSource getDataSource()
获取数据来源。
Get the DataSource used for database connections.
DataSourceio/agentscope/core/session/mysql/MysqlSession.java:729–731clearAllSessions
public int clearAllSessions()
清空所有会话。
Clear all sessions from the database (for testing or cleanup).
intio/agentscope/core/session/mysql/MysqlSession.java:739–756truncateAllSessions
public int truncateAllSessions()
返回 truncateAllSessions 结果。
Truncate session table from the database (for testing or cleanup).
intio/agentscope/core/session/mysql/MysqlSession.java:774–783validateSessionId
protected void validateSessionId(String sessionId)
校验会话标识。
Validate a session ID format.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
sessionId | String | 会话标识(类型 `String`)。Session ID to validate |
IllegalArgumentException当if 会话标识是无效时抛出 `IllegalArgumentException`。if session ID is invalid
io/agentscope/core/session/mysql/MysqlSession.java:791–801