MCPcopy Create free account
hub / github.com/astercloud/aster / NewMySQLStore

Function NewMySQLStore

pkg/store/mysql.go:84–127  ·  view source on GitHub ↗

NewMySQLStore 创建 MySQL 存储

(config MySQLConfig)

Source from the content-addressed store, hash-verified

82
83// NewMySQLStore 创建 MySQL 存储
84func NewMySQLStore(config MySQLConfig) (*MySQLStore, error) {
85 db, err := gorm.Open(mysql.Open(config.DSN), &gorm.Config{
86 Logger: logger.Default.LogMode(logger.Silent),
87 })
88 if err != nil {
89 return nil, fmt.Errorf("connect to mysql: %w", err)
90 }
91
92 sqlDB, err := db.DB()
93 if err != nil {
94 return nil, fmt.Errorf("get sql.DB: %w", err)
95 }
96
97 // 配置连接池
98 if config.MaxOpenConns > 0 {
99 sqlDB.SetMaxOpenConns(config.MaxOpenConns)
100 } else {
101 sqlDB.SetMaxOpenConns(25)
102 }
103 if config.MaxIdleConns > 0 {
104 sqlDB.SetMaxIdleConns(config.MaxIdleConns)
105 } else {
106 sqlDB.SetMaxIdleConns(10)
107 }
108 if config.MaxLifetime > 0 {
109 sqlDB.SetConnMaxLifetime(config.MaxLifetime)
110 } else {
111 sqlDB.SetConnMaxLifetime(5 * time.Minute)
112 }
113
114 // 自动迁移表结构
115 if err := db.AutoMigrate(
116 &AgentMessage{},
117 &AgentToolRecord{},
118 &AgentSnapshot{},
119 &AgentInfo{},
120 &AgentTodo{},
121 &CollectionItem{},
122 ); err != nil {
123 return nil, fmt.Errorf("auto migrate: %w", err)
124 }
125
126 return &MySQLStore{db: db}, nil
127}
128
129// SaveMessages 保存消息列表
130func (s *MySQLStore) SaveMessages(ctx context.Context, agentID string, messages []types.Message) error {

Callers 2

mainFunction · 0.92
NewStoreFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected