123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- // Code generated by $GOPATH/src/go-common/app/tool/cache/gen. DO NOT EDIT.
- /*
- Package testdata is a generated cache proxy package.
- It is generated from:
- type _cache interface {
- // cache: -batch=10 -max_group=10 -batch_err=break -nullcache=&Article{ID:-1} -check_null_code=$.ID==-1
- Articles(c context.Context, keys []int64) (map[int64]*Article, error)
- // cache: -sync=true -nullcache=&Article{ID:-1} -check_null_code=$.ID==-1
- Article(c context.Context, key int64) (*Article, error)
- // cache: -paging=true
- Article1(c context.Context, key int64, pn, ps int) (*Article, error)
- // cache: -nullcache=&Article{ID:-1} -check_null_code=$.ID==-1
- None(c context.Context) (*Article, error)
- }
- */
- package testdata
- import (
- "context"
- "sync"
- "go-common/library/stat/prom"
- "go-common/library/sync/errgroup"
- )
- var _ _cache
- // Articles get data from cache if miss will call source method, then add to cache.
- func (d *Dao) Articles(c context.Context, keys []int64) (res map[int64]*Article, err error) {
- if len(keys) == 0 {
- return
- }
- addCache := true
- if res, err = d.CacheArticles(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("Articles", int64(len(keys)-len(miss)))
- for k, v := range res {
- if v.ID == -1 {
- delete(res, k)
- }
- }
- missLen := len(miss)
- if missLen == 0 {
- return
- }
- missData := make(map[int64]*Article, missLen)
- prom.CacheMiss.Add("Articles", 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.RawArticles(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]*Article, len(keys))
- }
- for k, v := range missData {
- res[k] = v
- }
- if err != nil {
- return
- }
- for _, key := range miss {
- if res[key] == nil {
- missData[key] = &Article{ID: -1}
- }
- }
- if !addCache {
- return
- }
- d.cache.Do(c, func(c context.Context) {
- d.AddCacheArticles(c, missData)
- })
- return
- }
- // Article get data from cache if miss will call source method, then add to cache.
- func (d *Dao) Article(c context.Context, id int64) (res *Article, err error) {
- addCache := true
- res, err = d.CacheArticle(c, id)
- if err != nil {
- addCache = false
- err = nil
- }
- defer func() {
- if res.ID == -1 {
- res = nil
- }
- }()
- if res != nil {
- prom.CacheHit.Incr("Article")
- return
- }
- prom.CacheMiss.Incr("Article")
- res, err = d.RawArticle(c, id)
- if err != nil {
- return
- }
- miss := res
- if miss == nil {
- miss = &Article{ID: -1}
- }
- if !addCache {
- return
- }
- d.AddCacheArticle(c, id, miss)
- return
- }
- // Article1 get data from cache if miss will call source method, then add to cache.
- func (d *Dao) Article1(c context.Context, id int64, pn, ps int) (res *Article, err error) {
- addCache := true
- res, err = d.CacheArticle1(c, id, pn, ps)
- if err != nil {
- addCache = false
- err = nil
- }
- if res != nil {
- prom.CacheHit.Incr("Article1")
- return
- }
- var miss *Article
- prom.CacheMiss.Incr("Article1")
- res, miss, err = d.RawArticle1(c, id, pn, ps)
- if err != nil {
- return
- }
- if !addCache {
- return
- }
- d.cache.Do(c, func(c context.Context) {
- d.AddCacheArticle1(c, id, miss, pn, ps)
- })
- return
- }
- // None get data from cache if miss will call source method, then add to cache.
- func (d *Dao) None(c context.Context) (res *Article, err error) {
- addCache := true
- res, err = d.CacheNone(c)
- if err != nil {
- addCache = false
- err = nil
- }
- defer func() {
- if res.ID == -1 {
- res = nil
- }
- }()
- if res != nil {
- prom.CacheHit.Incr("None")
- return
- }
- prom.CacheMiss.Incr("None")
- res, err = d.RawNone(c)
- if err != nil {
- return
- }
- var miss = res
- if miss == nil {
- miss = &Article{ID: -1}
- }
- if !addCache {
- return
- }
- d.cache.Do(c, func(c context.Context) {
- d.AddCacheNone(c, miss)
- })
- return
- }
|