refactor: cleanup unused code and simplify internal packages
This commit performs a significant cleanup of the codebase by removing unused functions, methods, and entire files across various internal modules. This reduces technical debt and simplifies the project structure. Key changes include: - **cache**: Removed unused cache key generators and metrics snapshots. - **dto**: Removed redundant converter functions and segment creation helpers. - **middleware**: Deleted the unused `logger.go` middleware and simplified `ratelimit.go` and `casbin.go`. - **model**: Removed unused ID helpers, database closing functions, and batch decryption logic. - **pkg**: Cleaned up unused utility functions in `circuitbreaker`, `crypto`, `cursor`, `hook`, and `utils`. - **service**: Deleted `account_cleanup_service.go` and removed unused helper functions in `log_cleanup_service.go` and `sensitive_service.go`. - **repository**: Removed unused private loading methods in `comment_repo.go`.
This commit is contained in:
@@ -303,76 +303,6 @@ func (m *Message) IsInteractionNotification() bool {
|
||||
}
|
||||
}
|
||||
|
||||
// BatchDecryptMessages 批量并行解密消息
|
||||
// 用于批量查询后提高解密性能,比逐条调用 AfterFind 更高效
|
||||
// workerCount 指定并行工作数,如果 <= 0 则使用默认值
|
||||
func BatchDecryptMessages(messages []*Message, workerCount int) error {
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
encryptor := crypto.GetMessageEncryptor()
|
||||
if encryptor == nil {
|
||||
// 加密器未初始化,直接解析 JSON(兼容模式)
|
||||
for _, m := range messages {
|
||||
if m.decrypted || m.SegmentsEncrypted == "" {
|
||||
m.decrypted = true
|
||||
continue
|
||||
}
|
||||
if err := json.Unmarshal([]byte(m.SegmentsEncrypted), &m.Segments); err != nil {
|
||||
return err
|
||||
}
|
||||
m.decrypted = true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 小批量直接串行处理
|
||||
if len(messages) <= 10 {
|
||||
for _, m := range messages {
|
||||
if err := m.decryptSegments(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 并行解密
|
||||
// 先收集需要解密的密文
|
||||
ciphertexts := make([]string, len(messages))
|
||||
for i, m := range messages {
|
||||
ciphertexts[i] = m.SegmentsEncrypted
|
||||
}
|
||||
|
||||
// 批量并行解密
|
||||
plaintexts := encryptor.BatchDecrypt(ciphertexts, workerCount)
|
||||
|
||||
// 解析结果
|
||||
for i, m := range messages {
|
||||
if m.decrypted {
|
||||
continue
|
||||
}
|
||||
if m.SegmentsEncrypted == "" {
|
||||
m.decrypted = true
|
||||
continue
|
||||
}
|
||||
|
||||
plaintext := plaintexts[i]
|
||||
if plaintext == nil {
|
||||
// 解密失败,尝试直接解析(兼容旧数据)
|
||||
if err := json.Unmarshal([]byte(m.SegmentsEncrypted), &m.Segments); err != nil {
|
||||
continue // 忽略单条错误
|
||||
}
|
||||
} else {
|
||||
if err := json.Unmarshal(plaintext, &m.Segments); err != nil {
|
||||
continue // 忽略单条错误
|
||||
}
|
||||
}
|
||||
m.decrypted = true
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// BatchDecryptMessagesParallel 使用 worker pool 并行解密
|
||||
// 这是一个更高效的版本,适用于大量消息
|
||||
|
||||
Reference in New Issue
Block a user