From f63619eb8a5b46a48f89f5d4b56ff8dc1d342c89 Mon Sep 17 00:00:00 2001 From: yangjiankun Date: Mon, 20 May 2019 18:19:47 +0800 Subject: [PATCH 1/2] fix genbts --- tool/kratos-gen-bts/multi_template.go | 2 +- tool/kratos-gen-bts/testdata/dao.bts.go | 83 ++++++++++++++++++++++++- tool/kratos-gen-bts/testdata/dao.go | 2 + tool/kratos-gen-bts/testdata/multi.go | 18 ++++++ 4 files changed, 103 insertions(+), 2 deletions(-) diff --git a/tool/kratos-gen-bts/multi_template.go b/tool/kratos-gen-bts/multi_template.go index 4a2bca550..9ef3a3dc0 100644 --- a/tool/kratos-gen-bts/multi_template.go +++ b/tool/kratos-gen-bts/multi_template.go @@ -59,7 +59,7 @@ func (d *Dao) NAME(c context.Context, {{.IDName}} []KEY{{.ExtraArgsType}}) (res {{if .BatchErrBreak}} group := errgroup.WithCancel(c) {{else}} - group := &errgroup.WithContext(c) + group := errgroup.WithContext(c) {{end}} if missLen > MAXGROUP { group.GOMAXPROCS(MAXGROUP) diff --git a/tool/kratos-gen-bts/testdata/dao.bts.go b/tool/kratos-gen-bts/testdata/dao.bts.go index c53efa2f7..d89937dd1 100644 --- a/tool/kratos-gen-bts/testdata/dao.bts.go +++ b/tool/kratos-gen-bts/testdata/dao.bts.go @@ -1,4 +1,4 @@ -// Code generated by kratos tool btsgen. DO NOT EDIT. +// Code generated by kratos tool genbts. DO NOT EDIT. /* Package testdata is a generated cache proxy package. @@ -6,6 +6,8 @@ type _bts interface { // bts: -batch=2 -max_group=20 -batch_err=break -nullcache=&Demo{ID:-1} -check_null_code=$.ID==-1 Demos(c context.Context, keys []int64) (map[int64]*Demo, error) + // bts: -batch=2 -max_group=20 -batch_err=continue -nullcache=&Demo{ID:-1} -check_null_code=$.ID==-1 + Demos1(c context.Context, keys []int64) (map[int64]*Demo, error) // bts: -sync=true -nullcache=&Demo{ID:-1} -check_null_code=$.ID==-1 Demo(c context.Context, key int64) (*Demo, error) // bts: -paging=true @@ -106,6 +108,85 @@ func (d *Dao) Demos(c context.Context, keys []int64) (res map[int64]*Demo, err e return } +// Demos1 get data from cache if miss will call source method, then add to cache. +func (d *Dao) Demos1(c context.Context, keys []int64) (res map[int64]*Demo, err error) { + if len(keys) == 0 { + return + } + addCache := true + if res, err = d.CacheDemos1(c, keys); err != nil { + addCache = false + res = nil + err = nil + } + var miss []int64 + for _, key := range keys { + if (res == nil) || (res[key] == nil) { + miss = append(miss, key) + } + } + prom.CacheHit.Add("Demos1", int64(len(keys)-len(miss))) + for k, v := range res { + if v.ID == -1 { + delete(res, k) + } + } + missLen := len(miss) + if missLen == 0 { + return + } + missData := make(map[int64]*Demo, missLen) + prom.CacheMiss.Add("Demos1", int64(missLen)) + var mutex sync.Mutex + group := errgroup.WithContext(c) + if missLen > 20 { + group.GOMAXPROCS(20) + } + var run = func(ms []int64) { + group.Go(func(ctx context.Context) (err error) { + data, err := d.RawDemos1(ctx, ms) + mutex.Lock() + for k, v := range data { + missData[k] = v + } + mutex.Unlock() + return + }) + } + var ( + i int + n = missLen / 2 + ) + for i = 0; i < n; i++ { + run(miss[i*2 : (i+1)*2]) + } + if len(miss[i*2:]) > 0 { + run(miss[i*2:]) + } + err = group.Wait() + if res == nil { + res = make(map[int64]*Demo, len(keys)) + } + for k, v := range missData { + res[k] = v + } + if err != nil { + return + } + for _, key := range miss { + if res[key] == nil { + missData[key] = &Demo{ID: -1} + } + } + if !addCache { + return + } + d.cache.Do(c, func(c context.Context) { + d.AddCacheDemos1(c, missData) + }) + return +} + // Demo get data from cache if miss will call source method, then add to cache. func (d *Dao) Demo(c context.Context, key int64) (res *Demo, err error) { addCache := true diff --git a/tool/kratos-gen-bts/testdata/dao.go b/tool/kratos-gen-bts/testdata/dao.go index 847e57754..1b6686a52 100644 --- a/tool/kratos-gen-bts/testdata/dao.go +++ b/tool/kratos-gen-bts/testdata/dao.go @@ -26,6 +26,8 @@ func New() *Dao { type _bts interface { // bts: -batch=2 -max_group=20 -batch_err=break -nullcache=&Demo{ID:-1} -check_null_code=$.ID==-1 Demos(c context.Context, keys []int64) (map[int64]*Demo, error) + // bts: -batch=2 -max_group=20 -batch_err=continue -nullcache=&Demo{ID:-1} -check_null_code=$.ID==-1 + Demos1(c context.Context, keys []int64) (map[int64]*Demo, error) // bts: -sync=true -nullcache=&Demo{ID:-1} -check_null_code=$.ID==-1 Demo(c context.Context, key int64) (*Demo, error) // bts: -paging=true diff --git a/tool/kratos-gen-bts/testdata/multi.go b/tool/kratos-gen-bts/testdata/multi.go index fe2fdb580..b398adb67 100644 --- a/tool/kratos-gen-bts/testdata/multi.go +++ b/tool/kratos-gen-bts/testdata/multi.go @@ -28,3 +28,21 @@ func (d *Dao) AddCacheDemos(c context.Context, values map[int64]*Demo) error { // add to cache return _multiAddCacheFunc(c, values) } + +// CacheDemos1 . +func (d *Dao) CacheDemos1(c context.Context, keys []int64) (map[int64]*Demo, error) { + // get data from cache + return _multiCacheFunc(c, keys) +} + +// RawDemos . +func (d *Dao) RawDemos1(c context.Context, keys []int64) (map[int64]*Demo, error) { + // get data from db + return _multiRawFunc(c, keys) +} + +// AddCacheDemos . +func (d *Dao) AddCacheDemos1(c context.Context, values map[int64]*Demo) error { + // add to cache + return _multiAddCacheFunc(c, values) +} From ae811ae90a88eb65c37a6c23bde0013d61035101 Mon Sep 17 00:00:00 2001 From: yangjiankun Date: Mon, 20 May 2019 18:25:47 +0800 Subject: [PATCH 2/2] reindent --- tool/kratos-gen-bts/testdata/dao.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tool/kratos-gen-bts/testdata/dao.go b/tool/kratos-gen-bts/testdata/dao.go index 1b6686a52..6e1f5aece 100644 --- a/tool/kratos-gen-bts/testdata/dao.go +++ b/tool/kratos-gen-bts/testdata/dao.go @@ -26,8 +26,8 @@ func New() *Dao { type _bts interface { // bts: -batch=2 -max_group=20 -batch_err=break -nullcache=&Demo{ID:-1} -check_null_code=$.ID==-1 Demos(c context.Context, keys []int64) (map[int64]*Demo, error) - // bts: -batch=2 -max_group=20 -batch_err=continue -nullcache=&Demo{ID:-1} -check_null_code=$.ID==-1 - Demos1(c context.Context, keys []int64) (map[int64]*Demo, error) + // bts: -batch=2 -max_group=20 -batch_err=continue -nullcache=&Demo{ID:-1} -check_null_code=$.ID==-1 + Demos1(c context.Context, keys []int64) (map[int64]*Demo, error) // bts: -sync=true -nullcache=&Demo{ID:-1} -check_null_code=$.ID==-1 Demo(c context.Context, key int64) (*Demo, error) // bts: -paging=true