This commit is contained in:
John Guo 2022-02-14 16:08:26 +08:00
parent 5f2047d61b
commit 23c00eb83f
4 changed files with 64 additions and 16 deletions

View File

@ -171,10 +171,13 @@ func (r *Ring) Link(s *Ring) *Ring {
//
func (r *Ring) Unlink(n int) *Ring {
r.mu.Lock()
r.ring = r.ring.Unlink(n)
resultRing := r.ring.Unlink(n)
r.dirty.Set(true)
r.mu.Unlock()
return r
resultGRing := New(resultRing.Len())
resultGRing.ring = resultRing
resultGRing.dirty.Set(true)
return resultGRing
}
// RLockIteratorNext iterates and locks reading forward

View File

@ -7,6 +7,7 @@
package gring_test
import (
"container/ring"
"testing"
"github.com/gogf/gf/v2/container/gring"
@ -137,3 +138,48 @@ func TestRing_Slice(t *testing.T) {
t.Assert(array4, g.Slice{1, 5, 4, 3, 2})
})
}
// https://github.com/gogf/gf/issues/1394
func Test_Issue1394(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// gring.
gRing := gring.New(10)
for i := 0; i < 10; i++ {
gRing.Put(i)
}
t.Logf("the length:%d", gRing.Len())
gRingResult := gRing.Unlink(6)
for i := 0; i < 10; i++ {
t.Log(gRing.Val())
gRing = gRing.Next()
}
t.Logf("the ring length:%d", gRing.Len())
t.Logf("the result length:%d", gRingResult.Len())
// stdring
stdRing := ring.New(10)
for i := 0; i < 10; i++ {
stdRing.Value = i
stdRing = stdRing.Next()
}
t.Logf("the length:%d", stdRing.Len())
stdRingResult := stdRing.Unlink(6)
for i := 0; i < 10; i++ {
t.Log(stdRing.Value)
stdRing = stdRing.Next()
}
t.Logf("the ring length:%d", stdRing.Len())
t.Logf("the result length:%d", stdRingResult.Len())
// Assertion.
t.Assert(gRing.Len(), stdRing.Len())
t.Assert(gRingResult.Len(), stdRingResult.Len())
for i := 0; i < 10; i++ {
t.Assert(stdRing.Value, gRing.Val())
stdRing = stdRing.Next()
gRing = gRing.Next()
}
})
}

View File

@ -504,6 +504,7 @@ func (m *Model) Having(having interface{}, args ...interface{}) *Model {
// doGetAllBySql does the select statement on the database.
func (m *Model) doGetAllBySql(sql string, args ...interface{}) (result Result, err error) {
var (
ok bool
ctx = m.GetCtx()
cacheKey = ""
cacheObj = m.db.GetCache()
@ -512,20 +513,18 @@ func (m *Model) doGetAllBySql(sql string, args ...interface{}) (result Result, e
if m.cacheEnabled && m.tx == nil {
cacheKey = m.cacheOption.Name
if len(cacheKey) == 0 {
cacheKey = "gcache:" + gmd5.MustEncryptString(sql+", @PARAMS:"+gconv.String(args))
cacheKey = "GCache:" + gmd5.MustEncryptString(sql+", @PARAMS:"+gconv.String(args))
}
if v, _ := cacheObj.Get(ctx, cacheKey); !v.IsNil() {
if result, ok := v.Val().(Result); ok {
if result, ok = v.Val().(Result); ok {
// In-memory cache.
return result, nil
}
// Other cache, it needs conversion.
if err = json.UnmarshalUseNumber(v.Bytes(), &result); err != nil {
return nil, err
} else {
// Other cache, it needs conversion.
var result Result
if err = json.UnmarshalUseNumber(v.Bytes(), &result); err != nil {
return nil, err
} else {
return result, nil
}
return result, nil
}
}
}
@ -535,7 +534,7 @@ func (m *Model) doGetAllBySql(sql string, args ...interface{}) (result Result, e
// Cache the result.
if cacheKey != "" && err == nil {
if m.cacheOption.Duration < 0 {
if _, err := cacheObj.Remove(ctx, cacheKey); err != nil {
if _, err = cacheObj.Remove(ctx, cacheKey); err != nil {
intlog.Errorf(m.GetCtx(), `%+v`, err)
}
} else {
@ -543,7 +542,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 {
intlog.Errorf(m.GetCtx(), `%+v`, err)
}
}

View File

@ -80,7 +80,7 @@ func (view *View) Parse(ctx context.Context, file string, params ...Params) (res
}
// Monitor template files changes using fsnotify asynchronously.
if resource == nil {
if _, err := gfsnotify.AddOnce("gview.Parse:"+folder, folder, func(event *gfsnotify.Event) {
if _, err = gfsnotify.AddOnce("gview.Parse:"+folder, folder, func(event *gfsnotify.Event) {
// CLEAR THEM ALL.
view.fileCacheMap.Clear()
templates.Clear()
@ -137,11 +137,11 @@ func (view *View) Parse(ctx context.Context, file string, params ...Params) (res
if err != nil {
return "", err
}
if err := newTpl.Execute(buffer, variables); err != nil {
if err = newTpl.Execute(buffer, variables); err != nil {
return "", err
}
} else {
if err := tpl.(*texttpl.Template).Execute(buffer, variables); err != nil {
if err = tpl.(*texttpl.Template).Execute(buffer, variables); err != nil {
return "", err
}
}