From 29bbeef9dc7f06955e480c844ad8a475b7f1a733 Mon Sep 17 00:00:00 2001 From: xing Date: Fri, 3 Feb 2023 14:46:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/querycondition.go | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/model/querycondition.go b/model/querycondition.go index 33f0484..6b720a3 100644 --- a/model/querycondition.go +++ b/model/querycondition.go @@ -64,17 +64,9 @@ func ChunkFind[T Model](ctx context.Context, perLimit int, q *QueryCondition) (r if 1 == i { rr, total, err = SimplePagination[T](ctx, q.where, q.fields, q.group, i, perLimit, q.order, q.join, q.having, q.in...) } else { - rr, err = Finds[T](ctx, Conditions( - Where(q.where), - Fields(q.fields), - Group(q.group), - Having(q.having), - Join(q.join), - Order(q.order), - Offset(offset), - Limit(perLimit), - In(q.in...), - )) + q.offset = offset + q.limit = perLimit + rr, err = Finds[T](ctx, q) } offset += perLimit if (err != nil && err != sql.ErrNoRows) || len(rr) < 1 { @@ -102,17 +94,9 @@ func Chunk[T Model, R any](ctx context.Context, perLimit int, fn func(rows T) (R if 1 == i { rr, total, err = SimplePagination[T](ctx, q.where, q.fields, q.group, i, perLimit, q.order, q.join, q.having, q.in...) } else { - rr, err = Finds[T](ctx, Conditions( - Where(q.where), - Fields(q.fields), - Group(q.group), - Having(q.having), - Join(q.join), - Order(q.order), - Offset(offset), - Limit(perLimit), - In(q.in...), - )) + q.offset = offset + q.limit = perLimit + rr, err = Finds[T](ctx, q) } offset += perLimit if (err != nil && err != sql.ErrNoRows) || len(rr) < 1 { From d185f78d6c1cbeda3bb983968601599a1e348973 Mon Sep 17 00:00:00 2001 From: xing Date: Fri, 3 Feb 2023 21:55:00 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/map.go | 3 +-- cache/map_test.go | 32 ++++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/cache/map.go b/cache/map.go index e4e2dbb..dfa1f87 100644 --- a/cache/map.go +++ b/cache/map.go @@ -152,8 +152,7 @@ func (m *MapCache[K, V]) GetCacheBatch(c context.Context, key []K, timeout time. defer m.mux.Unlock() vers := slice.Reduce(needFlush, func(t K, r int) int { - r += m.data.Ver(c, t) - return r + return r + m.data.Ver(c, t) }, 0) if vers > ver { diff --git a/cache/map_test.go b/cache/map_test.go index feed65e..7fdb3e7 100644 --- a/cache/map_test.go +++ b/cache/map_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "github.com/fthvgb1/wp-go/helper/slice" + "github.com/fthvgb1/wp-go/taskPools" "reflect" "strings" "testing" @@ -22,9 +23,10 @@ func init() { } ct = context.Background() batchFn = func(a ...any) (map[string]string, error) { + fmt.Println(a) arr := a[1].([]string) - return slice.SimpleToMap(arr, func(t string) string { - return strings.Repeat(t, 2) + return slice.FilterAndToMap(arr, func(t string) (string, string, bool) { + return t, strings.Repeat(t, 2), true }), nil } ca = *NewMemoryMapCacheByFn[string, string](fn, time.Second*2) @@ -190,14 +192,36 @@ func TestMapCache_GetCacheBatch(t *testing.T) { c: ct, key: []string{"xx", "oo"}, timeout: time.Second, - params: []any{ct, []string{"xx", "oo"}}, + params: []any{ct, []string{"xx", "oo", "aa"}}, }, - want: []string{"xxxx", "oooo"}, + want: []string{"xxxx", "oooo", "aaaa"}, wantErr: false, }, } + time.Sleep(2 * time.Second) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + p := taskPools.NewPools(10) + for i := 0; i < 800000; i++ { + p.Execute(func() { + c := context.Background() + //time.Sleep(time.Millisecond * number.Rand[time.Duration](200, 400)) + a, err := ca.GetCacheBatch(c, []string{"xx", "oo", "aa"}, time.Hour, c, []string{"xx", "oo", "aa"}) + if err != nil { + panic(err) + return + } + + if a[0] == "xxxx" && a[1] == "oooo" && a[2] == "aaaa" { + + } else { + fmt.Println(a) + panic("xxx") + } + //fmt.Println(x) + }) + } + p.Wait() got, err := tt.m.GetCacheBatch(tt.args.c, tt.args.key, tt.args.timeout, tt.args.params...) if (err != nil) != tt.wantErr { t.Errorf("GetCacheBatch() error = %v, wantErr %v", err, tt.wantErr)