pull/108/head
yangjiankun 6 years ago
parent 71e9436652
commit f63619eb8a
  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}}
group := errgroup.WithCancel(c)
{{else}}
group := &errgroup.WithContext(c)
group := errgroup.WithContext(c)
{{end}}
if missLen > 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.
@ -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

@ -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

@ -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)
}

Loading…
Cancel
Save