refactor(cache): 重构 CacheMemory 实现

- 使用 sync.Map替代自定义 Map 结构- 优化缓存过期处理逻辑
- 改进通配符删除功能
- 随机触发缓存清理机制
This commit is contained in:
2025-04-10 22:09:53 +08:00
parent 754668d017
commit 6db8f75ec7
5 changed files with 210 additions and 117 deletions
+47 -3
View File
@@ -276,7 +276,10 @@ func (that *HoTimeDB) Action(action func(db HoTimeDB) (isSuccess bool)) (isSucce
that.Prefix, that.LastQuery, that.LastData,
that.ConnectFunc, that.LastErr, that.limit, that.Tx,
that.SlaveDB, that.Mode}
//tx, err := db.BeginTx(context.Background(), &sql.TxOptions{Isolation: sql.LevelReadCommitted})
tx, err := db.Begin()
if err != nil {
that.LastErr.SetError(err)
return isSuccess
@@ -559,6 +562,24 @@ func (that *HoTimeDB) Query(query string, args ...interface{}) []Map {
that.LastErr.SetError(err)
return nil
}
for key, _ := range args {
arg := args[key]
argType := reflect.ValueOf(arg).Type().String()
if strings.Contains(argType, "[]") || strings.Contains(argType, "Slice") {
argLis := ObjToSlice(arg)
//将slice转为,分割字符串
argStr := ""
for i := 0; i < len(argLis); i++ {
if i == len(argLis)-1 {
argStr += ObjToStr(argLis[i])
} else {
argStr += ObjToStr(argLis[i]) + ","
}
}
args[key] = argStr
}
}
if that.Tx != nil {
resl, err = that.Tx.Query(query, args...)
@@ -601,6 +622,29 @@ func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, *Erro
return nil, that.LastErr
}
for key, _ := range args {
arg := args[key]
argType := ""
if arg != nil {
argType = reflect.ValueOf(arg).Type().String()
}
if strings.Contains(argType, "[]") || strings.Contains(argType, "Slice") {
argLis := ObjToSlice(arg)
//将slice转为,分割字符串
argStr := ""
for i := 0; i < len(argLis); i++ {
if i == len(argLis)-1 {
argStr += ObjToStr(argLis[i])
} else {
argStr += ObjToStr(argLis[i]) + ","
}
}
args[key] = argStr
}
}
if that.Tx != nil {
resl, e = that.Tx.Exec(query, args...)
} else {
@@ -874,7 +918,7 @@ func (that *HoTimeDB) Sum(table string, column string, qu ...interface{}) float6
}
//where语句解析
// where语句解析
func (that *HoTimeDB) where(data Map) (string, []interface{}) {
where := ""
@@ -1368,7 +1412,7 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
return where, res
}
// that.Db.Update("user",hotime.Map{"ustate":"1"},hotime.Map{"AND":hotime.Map{"OR":hotime.Map{"uid":4,"uname":"dasda"}},"ustate":1})
// that.Db.Update("user",hotime.Map{"ustate":"1"},hotime.Map{"AND":hotime.Map{"OR":hotime.Map{"uid":4,"uname":"dasda"}},"ustate":1})
func (that *HoTimeDB) notIn(k string, v interface{}, where string, res []interface{}) (string, []interface{}) {
//where:=""
//fmt.Println(reflect.ValueOf(v).Type().String())
@@ -1504,7 +1548,7 @@ func (that *HoTimeDB) Update(table string, data Map, where Map) int64 {
func (that *HoTimeDB) Delete(table string, data map[string]interface{}) int64 {
query := "DELETE FROM " + that.Prefix + table + " "
query := "DELETE FROM `" + that.Prefix + table + "` "
temp, resWhere := that.where(data)
query += temp + ";"