You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.1 KiB
102 lines
2.1 KiB
// Code generated by $GOPATH/src/go-common/app/tool/cache/gen. DO NOT EDIT. |
|
|
|
/* |
|
Package dao is a generated cache proxy package. |
|
It is generated from: |
|
type _cache interface { |
|
// cache: -batch=10 -max_group=10 -batch_err=break -nullcache=&api.UserBase{Mid:-1} -check_null_code=$==nil||$.Mid==-1 |
|
UserBase(c context.Context, mid []int64) (map[int64]*api.UserBase, error) |
|
} |
|
*/ |
|
|
|
package dao |
|
|
|
import ( |
|
"context" |
|
"sync" |
|
|
|
"go-common/app/service/bbq/user/api" |
|
"go-common/library/stat/prom" |
|
"go-common/library/sync/errgroup" |
|
) |
|
|
|
var _ _cache |
|
|
|
// UserBase get data from cache if miss will call source method, then add to cache. |
|
func (d *Dao) UserBase(c context.Context, keys []int64) (res map[int64]*api.UserBase, err error) { |
|
if len(keys) == 0 { |
|
return |
|
} |
|
addCache := true |
|
if res, err = d.CacheUserBase(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("UserBase", int64(len(keys)-len(miss))) |
|
for k, v := range res { |
|
if v == nil || v.Mid == -1 { |
|
delete(res, k) |
|
} |
|
} |
|
missLen := len(miss) |
|
if missLen == 0 { |
|
return |
|
} |
|
missData := make(map[int64]*api.UserBase, missLen) |
|
prom.CacheMiss.Add("UserBase", int64(missLen)) |
|
var mutex sync.Mutex |
|
group, ctx := errgroup.WithContext(c) |
|
if missLen > 10 { |
|
group.GOMAXPROCS(10) |
|
} |
|
var run = func(ms []int64) { |
|
group.Go(func() (err error) { |
|
data, err := d.RawUserBase(ctx, ms) |
|
mutex.Lock() |
|
for k, v := range data { |
|
missData[k] = v |
|
} |
|
mutex.Unlock() |
|
return |
|
}) |
|
} |
|
var ( |
|
i int |
|
n = missLen / 10 |
|
) |
|
for i = 0; i < n; i++ { |
|
run(miss[i*n : (i+1)*n]) |
|
} |
|
if len(miss[i*n:]) > 0 { |
|
run(miss[i*n:]) |
|
} |
|
err = group.Wait() |
|
if res == nil { |
|
res = make(map[int64]*api.UserBase, len(keys)) |
|
} |
|
for k, v := range missData { |
|
res[k] = v |
|
} |
|
if err != nil { |
|
return |
|
} |
|
for _, key := range miss { |
|
if res[key] == nil { |
|
missData[key] = &api.UserBase{Mid: -1} |
|
} |
|
} |
|
if !addCache { |
|
return |
|
} |
|
d.cache.Do(c, func(c context.Context) { |
|
d.AddCacheUserBase(c, missData) |
|
}) |
|
return |
|
}
|
|
|