refactor(db): 增强测试事务和缓存管理

- 在 HoTimeDB 中新增 testMu 互斥锁,确保 testTx 的串行访问,解决并发冲突问题
- 在 BeginTestTx 中初始化 testMu,确保测试事务的安全性
- 在 HoTimeCache 中新增 DisableDbCache 方法,测试模式下禁用 DB 和 Redis 缓存,避免锁等待超时
- 更新 Select 方法,确保在 testTx 激活时跳过缓存逻辑,提升测试稳定性
- 优化 Swagger 生成逻辑,支持模块化输出和导航页生成
- 移除冗余的调试日志代码,提升代码整洁性
This commit is contained in:
2026-03-14 13:06:32 +08:00
parent bd20af0c89
commit 87098a9180
9 changed files with 254 additions and 205 deletions
-43
View File
@@ -1,10 +1,7 @@
package hotime
import (
"encoding/json"
"os"
"sync"
"time"
. "code.hoteas.com/golang/hotime/cache"
. "code.hoteas.com/golang/hotime/common"
@@ -21,15 +18,6 @@ type SessionIns struct {
// set 保存 session 到缓存,必须在锁内调用或传入深拷贝的 map
func (that *SessionIns) setWithCopy() {
// #region agent log
logFile, _ := os.OpenFile(`d:\work\hotimev1.5\.cursor\debug.log`, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if logFile != nil {
logEntry, _ := json.Marshal(map[string]interface{}{"sessionId": "debug-session", "runId": "run1", "hypothesisId": "A", "location": "session.go:setWithCopy", "message": "Session写入数据库触发", "data": map[string]interface{}{"session_id": that.SessionId, "map_size": len(that.Map)}, "timestamp": time.Now().UnixMilli()})
logFile.Write(append(logEntry, '\n'))
logFile.Close()
}
// #endregion
// 深拷贝 Map 防止并发修改
that.mutex.RLock()
copyMap := make(Map, len(that.Map))
@@ -49,15 +37,6 @@ func (that *SessionIns) Session(key string, data ...interface{}) *Obj {
that.mutex.Unlock()
if len(data) != 0 {
// #region agent log
logFile, _ := os.OpenFile(`d:\work\hotimev1.5\.cursor\debug.log`, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if logFile != nil {
logEntry, _ := json.Marshal(map[string]interface{}{"sessionId": "debug-session", "runId": "run1", "hypothesisId": "B", "location": "session.go:Session", "message": "Session.Set调用", "data": map[string]interface{}{"key": key, "is_delete": data[0] == nil}, "timestamp": time.Now().UnixMilli()})
logFile.Write(append(logEntry, '\n'))
logFile.Close()
}
// #endregion
that.mutex.Lock()
if data[0] == nil {
delete(that.Map, key)
@@ -85,19 +64,6 @@ func (that *SessionIns) SessionsSet(data Map) {
return
}
// #region agent log
logFile, _ := os.OpenFile(`d:\work\hotimev1.5\.cursor\debug.log`, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if logFile != nil {
keys := make([]string, 0, len(data))
for k := range data {
keys = append(keys, k)
}
logEntry, _ := json.Marshal(map[string]interface{}{"sessionId": "debug-session", "runId": "run1", "hypothesisId": "C", "location": "session.go:SessionsSet", "message": "SessionsSet批量设置", "data": map[string]interface{}{"keys": keys, "count": len(data)}, "timestamp": time.Now().UnixMilli()})
logFile.Write(append(logEntry, '\n'))
logFile.Close()
}
// #endregion
that.mutex.Lock()
if that.Map == nil {
that.getWithoutLock()
@@ -124,15 +90,6 @@ func (that *SessionIns) SessionsDelete(keys ...string) {
return
}
// #region agent log
logFile, _ := os.OpenFile(`d:\work\hotimev1.5\.cursor\debug.log`, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if logFile != nil {
logEntry, _ := json.Marshal(map[string]interface{}{"sessionId": "debug-session", "runId": "run1", "hypothesisId": "C", "location": "session.go:SessionsDelete", "message": "SessionsDelete批量删除", "data": map[string]interface{}{"keys": keys, "count": len(keys)}, "timestamp": time.Now().UnixMilli()})
logFile.Write(append(logEntry, '\n'))
logFile.Close()
}
// #endregion
that.mutex.Lock()
if that.Map == nil {
that.getWithoutLock()