Optimize the history record function of [One-click configuration]

This commit is contained in:
心隨緣動 2025-09-25 22:58:59 +08:00 committed by GitHub
parent d698cd4cb0
commit de7a45656c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -968,16 +968,24 @@ func (s *ServerService) GetNewmlkem768() (any, error) {
return keyPair, nil return keyPair, nil
} }
// SaveLinkHistory saves a new link and trims the history to the latest 10 // SaveLinkHistory 保存一个新的链接记录,并确保其被永久写入数据库文件。
func (s *ServerService) SaveLinkHistory(historyType, link string) error { func (s *ServerService) SaveLinkHistory(historyType, link string) error {
record := &database.LinkHistory{ record := &database.LinkHistory{
Type: historyType, Type: historyType,
Link: link, Link: link,
CreatedAt: time.Now(), CreatedAt: time.Now(),
} }
// 【核心修正】: 直接调用 AddLinkHistory 并返回其结果。
// 我们将把 Checkpoint 的逻辑移入 AddLinkHistory 内部,确保操作的原子性。 // 【核心修正】: 第一步,调用重构后的 AddLinkHistory 函数。
return database.AddLinkHistory(record) // 这个函数现在是一个原子事务。如果它没有返回错误,就意味着数据已经成功提交到了 .wal 日志文件。
err := database.AddLinkHistory(record)
if err != nil {
return err // 如果事务失败,直接返回错误,不执行后续操作
}
// 【核心修正】: 第二步,在事务成功提交后,我们在这里调用 Checkpoint。
// 此时 .wal 文件中已经包含了我们的新数据,调用 Checkpoint 可以确保这些数据被立即写入主数据库文件。
return database.Checkpoint()
} }
// LoadLinkHistory loads the latest 10 links from the database // LoadLinkHistory loads the latest 10 links from the database