123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- // 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 {
- Info(c context.Context, key int64) (*v1.Info, error)
- //cache: -batch=50 -max_group=10 -batch_err=continue
- Infos(c context.Context, keys []int64) (map[int64]*v1.Info, error)
- Card(c context.Context, key int64) (*v1.Card, error)
- //cache: -batch=50 -max_group=10 -batch_err=continue
- Cards(c context.Context, keys []int64) (map[int64]*v1.Card, error)
- Vip(c context.Context, key int64) (*v1.VipInfo, error)
- //cache: -batch=50 -max_group=10 -batch_err=continue
- Vips(c context.Context, keys []int64) (map[int64]*v1.VipInfo, error)
- Profile(c context.Context, key int64) (*v1.Profile, error)
- }
- */
- package dao
- import (
- "context"
- "sync"
- v1 "go-common/app/service/main/account/api"
- "go-common/library/stat/prom"
- "go-common/library/sync/errgroup"
- )
- var _ _cache
- // 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 *v1.Info, 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
- }
- miss := res
- if !addCache {
- return
- }
- d.cache.Do(c, func(ctx context.Context) {
- d.AddCacheInfo(ctx, id, miss)
- })
- return
- }
- // 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]*v1.Info, 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]*v1.Info
- 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]*v1.Info, len(keys))
- }
- missData[k] = v
- }
- mutex.Unlock()
- return
- })
- }
- err1 := group.Wait()
- if err1 != nil {
- err = err1
- }
- }
- if res == nil {
- res = make(map[int64]*v1.Info)
- }
- for k, v := range missData {
- res[k] = v
- }
- if err != nil {
- return
- }
- if !addCache {
- return
- }
- d.cache.Do(c, func(ctx context.Context) {
- d.AddCacheInfos(ctx, missData)
- })
- return
- }
- // Card cache: -batch=50 -max_group=10 -batch_err=continue
- func (d *Dao) Card(c context.Context, id int64) (res *v1.Card, err error) {
- addCache := true
- res, err = d.CacheCard(c, id)
- if err != nil {
- addCache = false
- err = nil
- }
- if res != nil {
- prom.CacheHit.Incr("Card")
- return
- }
- prom.CacheMiss.Incr("Card")
- res, err = d.RawCard(c, id)
- if err != nil {
- return
- }
- miss := res
- if !addCache {
- return
- }
- d.cache.Do(c, func(ctx context.Context) {
- d.AddCacheCard(ctx, id, miss)
- })
- return
- }
- // Cards get data from cache if miss will call source method, then add to cache.
- func (d *Dao) Cards(c context.Context, keys []int64) (res map[int64]*v1.Card, err error) {
- if len(keys) == 0 {
- return
- }
- addCache := true
- res, err = d.CacheCards(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("Cards", int64(len(keys)-len(miss)))
- if len(miss) == 0 {
- return
- }
- var missData map[int64]*v1.Card
- missLen := len(miss)
- prom.CacheMiss.Add("Cards", 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.RawCards(ctx, ks)
- mutex.Lock()
- for k, v := range data {
- if missData == nil {
- missData = make(map[int64]*v1.Card, len(keys))
- }
- missData[k] = v
- }
- mutex.Unlock()
- return
- })
- }
- err1 := group.Wait()
- if err1 != nil {
- err = err1
- }
- }
- if res == nil {
- res = make(map[int64]*v1.Card)
- }
- for k, v := range missData {
- res[k] = v
- }
- if err != nil {
- return
- }
- if !addCache {
- return
- }
- d.cache.Do(c, func(ctx context.Context) {
- d.AddCacheCards(ctx, missData)
- })
- return
- }
- // Vip cache: -batch=50 -max_group=10 -batch_err=continue
- func (d *Dao) Vip(c context.Context, id int64) (res *v1.VipInfo, err error) {
- addCache := true
- res, err = d.CacheVip(c, id)
- if err != nil {
- addCache = false
- err = nil
- }
- if res != nil {
- prom.CacheHit.Incr("Vip")
- return
- }
- prom.CacheMiss.Incr("Vip")
- res, err = d.RawVip(c, id)
- if err != nil {
- return
- }
- miss := res
- if !addCache {
- return
- }
- d.cache.Do(c, func(ctx context.Context) {
- d.AddCacheVip(ctx, id, miss)
- })
- return
- }
- // Vips get data from cache if miss will call source method, then add to cache.
- func (d *Dao) Vips(c context.Context, keys []int64) (res map[int64]*v1.VipInfo, err error) {
- if len(keys) == 0 {
- return
- }
- addCache := true
- res, err = d.CacheVips(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("Vips", int64(len(keys)-len(miss)))
- if len(miss) == 0 {
- return
- }
- var missData map[int64]*v1.VipInfo
- missLen := len(miss)
- prom.CacheMiss.Add("Vips", 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.RawVips(ctx, ks)
- mutex.Lock()
- for k, v := range data {
- if missData == nil {
- missData = make(map[int64]*v1.VipInfo, len(keys))
- }
- missData[k] = v
- }
- mutex.Unlock()
- return
- })
- }
- err1 := group.Wait()
- if err1 != nil {
- err = err1
- }
- }
- if res == nil {
- res = make(map[int64]*v1.VipInfo)
- }
- for k, v := range missData {
- res[k] = v
- }
- if err != nil {
- return
- }
- if !addCache {
- return
- }
- d.cache.Do(c, func(ctx context.Context) {
- d.AddCacheVips(ctx, missData)
- })
- return
- }
- // Profile cache: -batch=50 -max_group=10 -batch_err=continue
- func (d *Dao) Profile(c context.Context, id int64) (res *v1.Profile, err error) {
- addCache := true
- res, err = d.CacheProfile(c, id)
- if err != nil {
- addCache = false
- err = nil
- }
- if res != nil {
- prom.CacheHit.Incr("Profile")
- return
- }
- prom.CacheMiss.Incr("Profile")
- res, err = d.RawProfile(c, id)
- if err != nil {
- return
- }
- miss := res
- if !addCache {
- return
- }
- d.cache.Do(c, func(ctx context.Context) {
- d.AddCacheProfile(ctx, id, miss)
- })
- return
- }
|