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