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