fix: cache value assertion panic if the cache adapter is not in-memory for soft time feature of package gdb; improve converting performance for gconv.Scan (#3351)

This commit is contained in:
John Guo
2024-03-07 11:36:42 +08:00
committed by GitHub
parent cab6b89446
commit 607f079b23
16 changed files with 807 additions and 719 deletions

View File

@ -5,6 +5,8 @@
// You can obtain one at https://github.com/gogf/gf.
// Package gdb provides ORM features for popular relationship databases.
//
// TODO use context.Context as required parameter for all DB operations.
package gdb
import (

View File

@ -11,7 +11,6 @@ import (
"time"
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/internal/json"
)
// CacheOption is options for model cache control in query.
@ -67,7 +66,6 @@ func (m *Model) getSelectResultFromCache(ctx context.Context, sql string, args .
return
}
var (
ok bool
cacheItem *selectCacheItem
cacheKey = m.makeSelectCacheKey(sql, args...)
cacheObj = m.db.GetCache()
@ -82,12 +80,7 @@ func (m *Model) getSelectResultFromCache(ctx context.Context, sql string, args .
}
}()
if v, _ := cacheObj.Get(ctx, cacheKey); !v.IsNil() {
if cacheItem, ok = v.Val().(*selectCacheItem); ok {
// In-memory cache.
return cacheItem.Result, nil
}
// Other cache, it needs conversion.
if err = json.UnmarshalUseNumber(v.Bytes(), &cacheItem); err != nil {
if err = v.Scan(&cacheItem); err != nil {
return nil, err
}
return cacheItem.Result, nil

View File

@ -219,7 +219,10 @@ func (m *softTimeMaintainer) getSoftFieldNameAndType(
intlog.Error(ctx, err)
}
if result != nil {
var cacheItem = result.Val().(getSoftFieldNameAndTypeCacheItem)
var cacheItem getSoftFieldNameAndTypeCacheItem
if err = result.Scan(&cacheItem); err != nil {
return "", ""
}
fieldName = cacheItem.FieldName
fieldType = cacheItem.FieldType
}