Merge pull request #108 from bilibili/kratos/fix_btsgen

fix genbts
pull/368/head
Tony 6 years ago committed by GitHub
commit 457908d64b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      tool/kratos-gen-bts/multi_template.go
  2. 83
      tool/kratos-gen-bts/testdata/dao.bts.go
  3. 2
      tool/kratos-gen-bts/testdata/dao.go
  4. 18
      tool/kratos-gen-bts/testdata/multi.go

@ -59,7 +59,7 @@ func (d *Dao) NAME(c context.Context, {{.IDName}} []KEY{{.ExtraArgsType}}) (res
{{if .BatchErrBreak}} {{if .BatchErrBreak}}
group := errgroup.WithCancel(c) group := errgroup.WithCancel(c)
{{else}} {{else}}
group := &errgroup.WithContext(c) group := errgroup.WithContext(c)
{{end}} {{end}}
if missLen > MAXGROUP { if missLen > MAXGROUP {
group.GOMAXPROCS(MAXGROUP) group.GOMAXPROCS(MAXGROUP)

@ -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. Package testdata is a generated cache proxy package.
@ -6,6 +6,8 @@
type _bts interface { type _bts interface {
// bts: -batch=2 -max_group=20 -batch_err=break -nullcache=&Demo{ID:-1} -check_null_code=$.ID==-1 // 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) 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 // bts: -sync=true -nullcache=&Demo{ID:-1} -check_null_code=$.ID==-1
Demo(c context.Context, key int64) (*Demo, error) Demo(c context.Context, key int64) (*Demo, error)
// bts: -paging=true // bts: -paging=true
@ -106,6 +108,85 @@ func (d *Dao) Demos(c context.Context, keys []int64) (res map[int64]*Demo, err e
return 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. // 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) { func (d *Dao) Demo(c context.Context, key int64) (res *Demo, err error) {
addCache := true addCache := true

@ -26,6 +26,8 @@ func New() *Dao {
type _bts interface { type _bts interface {
// bts: -batch=2 -max_group=20 -batch_err=break -nullcache=&Demo{ID:-1} -check_null_code=$.ID==-1 // 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) 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 // bts: -sync=true -nullcache=&Demo{ID:-1} -check_null_code=$.ID==-1
Demo(c context.Context, key int64) (*Demo, error) Demo(c context.Context, key int64) (*Demo, error)
// bts: -paging=true // bts: -paging=true

@ -28,3 +28,21 @@ func (d *Dao) AddCacheDemos(c context.Context, values map[int64]*Demo) error {
// add to cache // add to cache
return _multiAddCacheFunc(c, values) 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)
}

Loading…
Cancel
Save