123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- // 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
- }
|