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.
143 lines
3.1 KiB
143 lines
3.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=50 -max_group=10 -batch_err=continue -nullcache=&model.VipInfoDB{VipType:0} -check_null_code=$!=nil |
|
Infos(c context.Context, keys []int64) (map[int64]*model.VipInfoDB, error) |
|
// cache: -nullcache=&model.VipInfoDB{VipType:0} -check_null_code=$!=nil |
|
Info(c context.Context, key int64) (*model.VipInfoDB, error) |
|
} |
|
*/ |
|
|
|
package dao |
|
|
|
import ( |
|
"context" |
|
"sync" |
|
|
|
"go-common/app/service/main/vipinfo/model" |
|
"go-common/library/net/metadata" |
|
"go-common/library/stat/prom" |
|
"go-common/library/sync/errgroup" |
|
) |
|
|
|
var _ _cache |
|
|
|
// Infos get data from cache if miss will call source method, then add to cache. |
|
func (d *Dao) Infos(c context.Context, keys []int64) (res map[int64]*model.VipUserInfo, err error) { |
|
if len(keys) == 0 { |
|
return |
|
} |
|
addCache := true |
|
res, err = d.CacheInfos(c, keys) |
|
if 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("Infos", int64(len(keys)-len(miss))) |
|
if len(miss) == 0 { |
|
return |
|
} |
|
var missData map[int64]*model.VipUserInfo |
|
missLen := len(miss) |
|
prom.CacheMiss.Add("Infos", int64(missLen)) |
|
mutex := sync.Mutex{} |
|
for i := 0; i < missLen; i += 50 * 10 { |
|
var subKeys []int64 |
|
group := &errgroup.Group{} |
|
ctx := c |
|
if (i + 50*10) > missLen { |
|
subKeys = miss[i:] |
|
} else { |
|
subKeys = miss[i : i+50*10] |
|
} |
|
missSubLen := len(subKeys) |
|
for j := 0; j < missSubLen; j += 50 { |
|
var ks []int64 |
|
if (j + 50) > missSubLen { |
|
ks = subKeys[j:] |
|
} else { |
|
ks = subKeys[j : j+50] |
|
} |
|
group.Go(func() (err error) { |
|
data, err := d.RawInfos(ctx, ks) |
|
mutex.Lock() |
|
for k, v := range data { |
|
if missData == nil { |
|
missData = make(map[int64]*model.VipUserInfo, len(keys)) |
|
} |
|
missData[k] = v |
|
} |
|
mutex.Unlock() |
|
return |
|
}) |
|
} |
|
err1 := group.Wait() |
|
if err1 != nil { |
|
err = err1 |
|
} |
|
} |
|
if res == nil { |
|
res = make(map[int64]*model.VipUserInfo) |
|
} |
|
for k, v := range missData { |
|
res[k] = v |
|
} |
|
if err != nil { |
|
return |
|
} |
|
for _, key := range keys { |
|
if res[key] == nil { |
|
if missData == nil { |
|
missData = make(map[int64]*model.VipUserInfo, len(keys)) |
|
} |
|
missData[key] = &model.VipUserInfo{VipType: 0, VipStatus: 0} |
|
res[key] = missData[key] |
|
} |
|
} |
|
if !addCache { |
|
return |
|
} |
|
d.cache.Save(func() { |
|
d.AddCacheInfos(metadata.WithContext(c), missData) |
|
}) |
|
return |
|
} |
|
|
|
// Info get data from cache if miss will call source method, then add to cache. |
|
func (d *Dao) Info(c context.Context, id int64) (res *model.VipUserInfo, err error) { |
|
addCache := true |
|
res, err = d.CacheInfo(c, id) |
|
if err != nil { |
|
addCache = false |
|
err = nil |
|
} |
|
if res != nil { |
|
prom.CacheHit.Incr("Info") |
|
return |
|
} |
|
prom.CacheMiss.Incr("Info") |
|
res, err = d.RawInfo(c, id) |
|
if err != nil { |
|
return |
|
} |
|
if res == nil { |
|
res = &model.VipUserInfo{VipType: 0, VipStatus: 0} |
|
} |
|
if !addCache { |
|
return |
|
} |
|
d.cache.Save(func() { |
|
d.AddCacheInfo(metadata.WithContext(c), id, res) |
|
}) |
|
return |
|
}
|
|
|