refactor(cache): 移除调试日志并优化缓存逻辑
- 删除 CacheDb 结构体中的 debugLog 调用,清理冗余的调试日志代码 - 优化历史表的创建逻辑,简化条件检查 - 更新缓存的获取和设置方法,确保逻辑清晰且高效 - 维护代码整洁性,提升可读性和可维护性
This commit is contained in:
Vendored
+1
-85
@@ -3,31 +3,12 @@ package cache
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
. "code.hoteas.com/golang/hotime/common"
|
||||
)
|
||||
|
||||
// #region agent log
|
||||
func debugLog(hypothesisId, location, message string, data map[string]interface{}) {
|
||||
logEntry := fmt.Sprintf(`{"hypothesisId":"%s","location":"%s","message":"%s","data":%s,"timestamp":%d,"sessionId":"debug-session"}`,
|
||||
hypothesisId, location, message, toJSON(data), time.Now().UnixMilli())
|
||||
f, _ := os.OpenFile(`d:\work\hotimev1.5\.cursor\debug.log`, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if f != nil {
|
||||
f.WriteString(logEntry + "\n")
|
||||
f.Close()
|
||||
}
|
||||
}
|
||||
func toJSON(data map[string]interface{}) string {
|
||||
b, _ := json.Marshal(data)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// 表名常量
|
||||
const (
|
||||
CacheTableName = "hotime_cache"
|
||||
@@ -86,12 +67,6 @@ func (that *CacheDb) initDbTable() {
|
||||
tableName := that.getTableName()
|
||||
historyTableName := that.getHistoryTableName()
|
||||
|
||||
// #region agent log
|
||||
debugLog("F", "cache_db.go:initDbTable", "initDbTable started", map[string]interface{}{
|
||||
"dbType": dbType, "tableName": tableName, "historyTableName": historyTableName, "HistorySet": that.HistorySet,
|
||||
})
|
||||
// #endregion
|
||||
|
||||
// 检查并创建主表
|
||||
if !that.tableExists(tableName) {
|
||||
that.createMainTable(dbType, tableName)
|
||||
@@ -104,20 +79,8 @@ func (that *CacheDb) initDbTable() {
|
||||
}
|
||||
|
||||
// 检查并创建历史表(开启历史记录时)
|
||||
historyTableExists := that.tableExists(historyTableName)
|
||||
// #region agent log
|
||||
debugLog("F", "cache_db.go:initDbTable", "history table check", map[string]interface{}{
|
||||
"HistorySet": that.HistorySet, "historyTableName": historyTableName, "exists": historyTableExists,
|
||||
"shouldCreate": that.HistorySet && !historyTableExists,
|
||||
})
|
||||
// #endregion
|
||||
if that.HistorySet && !historyTableExists {
|
||||
if that.HistorySet && !that.tableExists(historyTableName) {
|
||||
that.createHistoryTable(dbType, historyTableName)
|
||||
// #region agent log
|
||||
debugLog("F", "cache_db.go:initDbTable", "createHistoryTable called", map[string]interface{}{
|
||||
"dbType": dbType, "historyTableName": historyTableName,
|
||||
})
|
||||
// #endregion
|
||||
}
|
||||
|
||||
that.isInit = true
|
||||
@@ -323,14 +286,6 @@ func (that *CacheDb) writeHistory(key string) {
|
||||
|
||||
// 插入历史表
|
||||
that.Db.Insert(historyTableName, historyData)
|
||||
|
||||
// #region agent log
|
||||
logFile3, _ := os.OpenFile(`d:\work\hotimev1.5\.cursor\debug.log`, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if logFile3 != nil {
|
||||
fmt.Fprintf(logFile3, `{"hypothesisId":"C","location":"cache_db.go:writeHistory","message":"history written","data":{"key":"%s","cacheId":%d},"timestamp":%d,"sessionId":"debug-session"}`+"\n", key, cached.GetInt64("id"), time.Now().UnixMilli())
|
||||
logFile3.Close()
|
||||
}
|
||||
// #endregion
|
||||
}
|
||||
|
||||
// get 获取缓存
|
||||
@@ -338,15 +293,6 @@ func (that *CacheDb) get(key string) interface{} {
|
||||
tableName := that.getTableName()
|
||||
cached := that.Db.Get(tableName, "*", Map{"key": key})
|
||||
|
||||
// #region agent log
|
||||
logFile4, _ := os.OpenFile(`d:\work\hotimev1.5\.cursor\debug.log`, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if logFile4 != nil {
|
||||
found := cached != nil
|
||||
fmt.Fprintf(logFile4, `{"hypothesisId":"D","location":"cache_db.go:get","message":"get query","data":{"key":"%s","found":%t},"timestamp":%d,"sessionId":"debug-session"}`+"\n", key, found, time.Now().UnixMilli())
|
||||
logFile4.Close()
|
||||
}
|
||||
// #endregion
|
||||
|
||||
if cached == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -372,14 +318,6 @@ func (that *CacheDb) get(key string) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// #region agent log
|
||||
logFile5, _ := os.OpenFile(`d:\work\hotimev1.5\.cursor\debug.log`, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if logFile5 != nil {
|
||||
fmt.Fprintf(logFile5, `{"hypothesisId":"D","location":"cache_db.go:get","message":"get success","data":{"key":"%s","hasData":true},"timestamp":%d,"sessionId":"debug-session"}`+"\n", key, time.Now().UnixMilli())
|
||||
logFile5.Close()
|
||||
}
|
||||
// #endregion
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -433,14 +371,6 @@ func (that *CacheDb) set(key string, value interface{}, endTime time.Time) {
|
||||
}
|
||||
}
|
||||
|
||||
// #region agent log
|
||||
logFile2, _ := os.OpenFile(`d:\work\hotimev1.5\.cursor\debug.log`, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if logFile2 != nil {
|
||||
fmt.Fprintf(logFile2, `{"hypothesisId":"B","location":"cache_db.go:set","message":"set completed","data":{"key":"%s","endTime":"%s","dbType":"%s"},"timestamp":%d,"sessionId":"debug-session"}`+"\n", key, endTimeStr, dbType, time.Now().UnixMilli())
|
||||
logFile2.Close()
|
||||
}
|
||||
// #endregion
|
||||
|
||||
// 写入历史记录
|
||||
that.writeHistory(key)
|
||||
|
||||
@@ -473,20 +403,6 @@ func (that *CacheDb) delete(key string) {
|
||||
func (that *CacheDb) Cache(key string, data ...interface{}) *Obj {
|
||||
that.initDbTable()
|
||||
|
||||
// #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 {
|
||||
op := "get"
|
||||
if len(data) == 1 && data[0] == nil {
|
||||
op = "delete"
|
||||
} else if len(data) >= 1 {
|
||||
op = "set"
|
||||
}
|
||||
fmt.Fprintf(logFile, `{"hypothesisId":"A","location":"cache_db.go:Cache","message":"Cache called","data":{"key":"%s","operation":"%s","dataLen":%d},"timestamp":%d,"sessionId":"debug-session"}`+"\n", key, op, len(data), time.Now().UnixMilli())
|
||||
logFile.Close()
|
||||
}
|
||||
// #endregion
|
||||
|
||||
// 获取缓存
|
||||
if len(data) == 0 {
|
||||
return &Obj{Data: that.get(key)}
|
||||
|
||||
Reference in New Issue
Block a user