140 lines
3.1 KiB
Go
Raw Normal View History

2019-04-22 02:59:20 +00:00
// Code generated by $GOPATH/src/go-common/app/tool/cache/gen. DO NOT EDIT.
/*
Package manager is a generated cache proxy package.
It is generated from:
type _cache interface {
// cache: -nullcache=&upgrpc.UpSpecial{GroupIDs:[]int64{-1}} -check_null_code=$!=nil&&len($.GroupIDs)>0&&$.GroupIDs[0]==-1
UpSpecial(c context.Context, mid int64) (us *upgrpc.UpSpecial, err error)
// cache: -batch=100 -max_group=1 -batch_err=break -nullcache=&upgrpc.UpSpecial{GroupIDs:[]int64{-1}} -check_null_code=$!=nil&&len($.GroupIDs)>0&&$.GroupIDs[0]==-1
UpsSpecial(c context.Context, mids []int64) (map[int64]*upgrpc.UpSpecial, error)
}
*/
package manager
import (
"context"
"sync"
upgrpc "go-common/app/service/main/up/api/v1"
"go-common/library/stat/prom"
"go-common/library/sync/errgroup"
)
var _ _cache
// UpSpecial get data from cache if miss will call source method, then add to cache.
func (d *Dao) UpSpecial(c context.Context, id int64) (res *upgrpc.UpSpecial, err error) {
addCache := true
res, err = d.CacheUpSpecial(c, id)
if err != nil {
addCache = false
err = nil
}
defer func() {
if res != nil && len(res.GroupIDs) > 0 && res.GroupIDs[0] == -1 {
res = nil
}
}()
if res != nil {
prom.CacheHit.Incr("UpSpecial")
return
}
prom.CacheMiss.Incr("UpSpecial")
res, err = d.RawUpSpecial(c, id)
if err != nil {
return
}
miss := res
if miss == nil {
miss = &upgrpc.UpSpecial{GroupIDs: []int64{-1}}
}
if !addCache {
return
}
d.cache.Do(c, func(c context.Context) {
d.AddCacheUpSpecial(c, id, miss)
})
return
}
// UpsSpecial get data from cache if miss will call source method, then add to cache.
func (d *Dao) UpsSpecial(c context.Context, keys []int64) (res map[int64]*upgrpc.UpSpecial, err error) {
if len(keys) == 0 {
return
}
addCache := true
if res, err = d.CacheUpsSpecial(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("UpsSpecial", int64(len(keys)-len(miss)))
for k, v := range res {
if v != nil && len(v.GroupIDs) > 0 && v.GroupIDs[0] == -1 {
delete(res, k)
}
}
missLen := len(miss)
if missLen == 0 {
return
}
missData := make(map[int64]*upgrpc.UpSpecial, missLen)
prom.CacheMiss.Add("UpsSpecial", int64(missLen))
var mutex sync.Mutex
group, ctx := errgroup.WithContext(c)
if missLen > 1 {
group.GOMAXPROCS(1)
}
var run = func(ms []int64) {
group.Go(func() (err error) {
data, err := d.RawUpsSpecial(ctx, ms)
mutex.Lock()
for k, v := range data {
missData[k] = v
}
mutex.Unlock()
return
})
}
var (
i int
n = missLen / 100
)
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]*upgrpc.UpSpecial, len(keys))
}
for k, v := range missData {
res[k] = v
}
if err != nil {
return
}
for _, key := range miss {
if res[key] == nil {
missData[key] = &upgrpc.UpSpecial{GroupIDs: []int64{-1}}
}
}
if !addCache {
return
}
d.cache.Do(c, func(c context.Context) {
d.AddCacheUpsSpecial(c, missData)
})
return
}