-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gdb returns result when cache set failed #1660
Conversation
@qinyuguang 不能丢弃方法中的任何 |
@gqcn 比如这样一段代码,怎么判断是db错误,还是cache错误呢? 任意一种错误,结果返回的都是 count=0 且 err!=nil func cache(ctx context.Context) {
count, err := g.DB().Model("test").Ctx(ctx).
Cache(gdb.CacheOption{Duration: 10 * time.Minute, Name: "count"}).
Count()
if err != nil {
// cache错误?db错误?
}
} 此时,给调用者返回错误呢,还是数据呢?(这个例子Count方法返回0大概还是个bug,执行了sql去查应该有数据的) 如果查询db有结果还好,可以按照结果不为空再进行一次判断,来确认是否cache错误。如果查询db是空结果,那就没法判断是什么错误了 |
额,实际上在cache挂掉时,scan出来的结果其实都是nil func cache2(ctx context.Context) {
type test struct {
ID int
}
var res *test
if err := g.DB().
Model("test").
Ctx(ctx).
Cache(gdb.CacheOption{Duration: 10 * time.Minute, Name: "cache2"}).
Scan(&res); err != nil {
g.Log().Debug(ctx, "err:", err)
}
g.Log().Debugf(ctx, "res: %#v", res)
} 输出
|
database/gdb/gdb_model_select.go
Outdated
@@ -546,7 +546,7 @@ func (m *Model) doGetAllBySql(sql string, args ...interface{}) (result Result, e | |||
if result.IsEmpty() && m.cacheOption.Force { | |||
result = Result{} | |||
} | |||
if err = cacheObj.Set(ctx, cacheKey, result, m.cacheOption.Duration); err != nil { | |||
if err := cacheObj.Set(ctx, cacheKey, result, m.cacheOption.Duration); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
换个名字,例如cacheErr
,同样的,上面的cacheObj.Remove
的err
也改一下名字。
Codecov Report
@@ Coverage Diff @@
## master #1660 +/- ##
==========================================
+ Coverage 71.47% 71.55% +0.07%
==========================================
Files 445 445
Lines 42544 42674 +130
==========================================
+ Hits 30410 30534 +124
- Misses 10237 10248 +11
+ Partials 1897 1892 -5
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
@gqcn 已修改 |
gdb缓存挂掉时候,查询操作应返回db数据,而不是返回设置缓存时的error