123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- // 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: -nullcache=&model.List{ID:-1} -check_null_code=$!=nil&&$.ID==-1 -singleflight=true
- List(c context.Context, id int64) (*model.List, error)
- // cache: -batch=100 -max_group=10 -nullcache=&model.List{ID:-1} -check_null_code=$!=nil&&$.ID==-1
- Lists(c context.Context, keys []int64) (map[int64]*model.List, error)
- // cache: -singleflight=true -nullcache=[]*model.ListArtMeta{{ID:-1}} -check_null_code=len($)==1&&$[0].ID==-1
- ListArts(c context.Context, id int64) ([]*model.ListArtMeta, error)
- // cache: -nullcache=[]*model.ListArtMeta{{ID:-1}} -check_null_code=len($)==1&&$[0].ID==-1
- ListsArts(c context.Context, ids []int64) (map[int64][]*model.ListArtMeta, error)
- // cache: -nullcache=-1 -batch=100 -max_group=10
- ArtsListID(c context.Context, keys []int64) (map[int64]int64, error)
- // cache: -nullcache=[]int64{-1} -check_null_code=len($)==1&&$[0]==-1 -singleflight=true
- UpLists(c context.Context, mid int64) ([]int64, error)
- // cache: -nullcache=&model.AuthorLimit{Limit:-1} -check_null_code=$!=nil&&$.Limit==-1
- Author(c context.Context, mid int64) (*model.AuthorLimit, error)
- }
- */
- package dao
- import (
- "context"
- "sync"
- "go-common/app/interface/openplatform/article/model"
- "go-common/library/net/metadata"
- "go-common/library/stat/prom"
- "go-common/library/sync/errgroup"
- "golang.org/x/sync/singleflight"
- )
- var _ _cache
- var cacheSingleFlights = [3]*singleflight.Group{{}, {}, {}}
- // List get data from cache if miss will call source method, then add to cache.
- func (d *Dao) List(c context.Context, id int64) (res *model.List, err error) {
- addCache := true
- res, err = d.CacheList(c, id)
- if err != nil {
- addCache = false
- err = nil
- }
- defer func() {
- if res != nil && res.ID == -1 {
- res = nil
- }
- }()
- if res != nil {
- prom.CacheHit.Incr("List")
- return
- }
- var rr interface{}
- sf := d.cacheSFList(id)
- rr, err, _ = cacheSingleFlights[0].Do(sf, func() (r interface{}, e error) {
- prom.CacheMiss.Incr("List")
- r, e = d.RawList(c, id)
- return
- })
- res = rr.(*model.List)
- if err != nil {
- return
- }
- miss := res
- if miss == nil {
- miss = &model.List{ID: -1}
- }
- if !addCache {
- return
- }
- d.cache.Save(func() {
- d.AddCacheList(metadata.WithContext(c), id, miss)
- })
- return
- }
- // Lists get data from cache if miss will call source method, then add to cache.
- func (d *Dao) Lists(c context.Context, keys []int64) (res map[int64]*model.List, err error) {
- if len(keys) == 0 {
- return
- }
- addCache := true
- res, err = d.CacheLists(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("Lists", int64(len(keys)-len(miss)))
- defer func() {
- for k, v := range res {
- if v != nil && v.ID == -1 {
- delete(res, k)
- }
- }
- }()
- if len(miss) == 0 {
- return
- }
- var missData map[int64]*model.List
- missLen := len(miss)
- prom.CacheMiss.Add("Lists", int64(missLen))
- mutex := sync.Mutex{}
- for i := 0; i < missLen; i += 100 * 10 {
- var subKeys []int64
- group, ctx := errgroup.WithContext(c)
- if (i + 100*10) > missLen {
- subKeys = miss[i:]
- } else {
- subKeys = miss[i : i+100*10]
- }
- missSubLen := len(subKeys)
- for j := 0; j < missSubLen; j += 100 {
- var ks []int64
- if (j + 100) > missSubLen {
- ks = subKeys[j:]
- } else {
- ks = subKeys[j : j+100]
- }
- group.Go(func() (err error) {
- data, err := d.RawLists(ctx, ks)
- mutex.Lock()
- for k, v := range data {
- if missData == nil {
- missData = make(map[int64]*model.List, len(keys))
- }
- missData[k] = v
- }
- mutex.Unlock()
- return
- })
- }
- err1 := group.Wait()
- if err1 != nil {
- err = err1
- break
- }
- }
- if res == nil {
- res = make(map[int64]*model.List)
- }
- 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.List, len(keys))
- }
- missData[key] = &model.List{ID: -1}
- }
- }
- if !addCache {
- return
- }
- d.cache.Save(func() {
- d.AddCacheLists(metadata.WithContext(c), missData)
- })
- return
- }
- // ListArts get data from cache if miss will call source method, then add to cache.
- func (d *Dao) ListArts(c context.Context, id int64) (res []*model.ListArtMeta, err error) {
- addCache := true
- res, err = d.CacheListArts(c, id)
- if err != nil {
- addCache = false
- err = nil
- }
- defer func() {
- if len(res) == 1 && res[0].ID == -1 {
- res = nil
- }
- }()
- if len(res) != 0 {
- prom.CacheHit.Incr("ListArts")
- return
- }
- var rr interface{}
- sf := d.cacheSFListArts(id)
- rr, err, _ = cacheSingleFlights[1].Do(sf, func() (r interface{}, e error) {
- prom.CacheMiss.Incr("ListArts")
- r, e = d.RawListArts(c, id)
- return
- })
- res = rr.([]*model.ListArtMeta)
- if err != nil {
- return
- }
- miss := res
- if len(miss) == 0 {
- miss = []*model.ListArtMeta{{ID: -1}}
- }
- if !addCache {
- return
- }
- d.cache.Save(func() {
- d.AddCacheListArts(metadata.WithContext(c), id, miss)
- })
- return
- }
- // ListsArts get data from cache if miss will call source method, then add to cache.
- func (d *Dao) ListsArts(c context.Context, keys []int64) (res map[int64][]*model.ListArtMeta, err error) {
- if len(keys) == 0 {
- return
- }
- addCache := true
- res, err = d.CacheListsArts(c, keys)
- if err != nil {
- addCache = false
- res = nil
- err = nil
- }
- var miss []int64
- for _, key := range keys {
- if (res == nil) || (len(res[key]) == 0) {
- miss = append(miss, key)
- }
- }
- prom.CacheHit.Add("ListsArts", int64(len(keys)-len(miss)))
- defer func() {
- for k, v := range res {
- if len(v) == 1 && v[0].ID == -1 {
- delete(res, k)
- }
- }
- }()
- if len(miss) == 0 {
- return
- }
- var missData map[int64][]*model.ListArtMeta
- prom.CacheMiss.Add("ListsArts", int64(len(miss)))
- missData, err = d.RawListsArts(c, miss)
- if res == nil {
- res = make(map[int64][]*model.ListArtMeta)
- }
- for k, v := range missData {
- res[k] = v
- }
- if err != nil {
- return
- }
- for _, key := range keys {
- if len(res[key]) == 0 {
- if missData == nil {
- missData = make(map[int64][]*model.ListArtMeta, len(keys))
- }
- missData[key] = []*model.ListArtMeta{{ID: -1}}
- }
- }
- if !addCache {
- return
- }
- d.cache.Save(func() {
- d.AddCacheListsArts(metadata.WithContext(c), missData)
- })
- return
- }
- // ArtsListID get data from cache if miss will call source method, then add to cache.
- func (d *Dao) ArtsListID(c context.Context, keys []int64) (res map[int64]int64, err error) {
- if len(keys) == 0 {
- return
- }
- addCache := true
- res, err = d.CacheArtsListID(c, keys)
- if err != nil {
- addCache = false
- res = nil
- err = nil
- }
- var miss []int64
- for _, key := range keys {
- if _, ok := res[key]; !ok {
- miss = append(miss, key)
- }
- }
- prom.CacheHit.Add("ArtsListID", int64(len(keys)-len(miss)))
- defer func() {
- for k, v := range res {
- if v == -1 {
- delete(res, k)
- }
- }
- }()
- if len(miss) == 0 {
- return
- }
- var missData map[int64]int64
- missLen := len(miss)
- prom.CacheMiss.Add("ArtsListID", int64(missLen))
- mutex := sync.Mutex{}
- for i := 0; i < missLen; i += 100 * 10 {
- var subKeys []int64
- group, ctx := errgroup.WithContext(c)
- if (i + 100*10) > missLen {
- subKeys = miss[i:]
- } else {
- subKeys = miss[i : i+100*10]
- }
- missSubLen := len(subKeys)
- for j := 0; j < missSubLen; j += 100 {
- var ks []int64
- if (j + 100) > missSubLen {
- ks = subKeys[j:]
- } else {
- ks = subKeys[j : j+100]
- }
- group.Go(func() (err error) {
- data, err := d.RawArtsListID(ctx, ks)
- mutex.Lock()
- for k, v := range data {
- if missData == nil {
- missData = make(map[int64]int64, len(keys))
- }
- missData[k] = v
- }
- mutex.Unlock()
- return
- })
- }
- err1 := group.Wait()
- if err1 != nil {
- err = err1
- break
- }
- }
- if res == nil {
- res = make(map[int64]int64)
- }
- for k, v := range missData {
- res[k] = v
- }
- if err != nil {
- return
- }
- for _, key := range keys {
- if res[key] == 0 {
- if missData == nil {
- missData = make(map[int64]int64, len(keys))
- }
- missData[key] = -1
- }
- }
- if !addCache {
- return
- }
- d.cache.Save(func() {
- d.AddCacheArtsListID(metadata.WithContext(c), missData)
- })
- return
- }
- // UpLists get data from cache if miss will call source method, then add to cache.
- func (d *Dao) UpLists(c context.Context, id int64) (res []int64, err error) {
- addCache := true
- res, err = d.CacheUpLists(c, id)
- if err != nil {
- addCache = false
- err = nil
- }
- defer func() {
- if len(res) == 1 && res[0] == -1 {
- res = nil
- }
- }()
- if len(res) != 0 {
- prom.CacheHit.Incr("UpLists")
- return
- }
- var rr interface{}
- sf := d.cacheSFUpLists(id)
- rr, err, _ = cacheSingleFlights[2].Do(sf, func() (r interface{}, e error) {
- prom.CacheMiss.Incr("UpLists")
- r, e = d.RawUpLists(c, id)
- return
- })
- res = rr.([]int64)
- if err != nil {
- return
- }
- miss := res
- if len(miss) == 0 {
- miss = []int64{-1}
- }
- if !addCache {
- return
- }
- d.cache.Save(func() {
- d.AddCacheUpLists(metadata.WithContext(c), id, miss)
- })
- return
- }
- // Author get data from cache if miss will call source method, then add to cache.
- func (d *Dao) Author(c context.Context, id int64) (res *model.AuthorLimit, err error) {
- addCache := true
- res, err = d.CacheAuthor(c, id)
- if err != nil {
- addCache = false
- err = nil
- }
- defer func() {
- if res != nil && res.Limit == -1 {
- res = nil
- }
- }()
- if res != nil {
- prom.CacheHit.Incr("Author")
- return
- }
- prom.CacheMiss.Incr("Author")
- res, err = d.RawAuthor(c, id)
- if err != nil {
- return
- }
- miss := res
- if miss == nil {
- miss = &model.AuthorLimit{Limit: -1}
- }
- if !addCache {
- return
- }
- d.cache.Save(func() {
- d.AddCacheAuthor(metadata.WithContext(c), id, miss)
- })
- return
- }
|