bilibili-backup/app/service/bbq/topic/internal/dao/dao.cache.go
2019-04-22 02:59:20 +00:00

180 lines
3.9 KiB
Go

// 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: -sync=true -batch=10 -max_group=10 -batch_err=break -nullcache=&api.VideoExtension{Svid:-1} -check_null_code=$==nil||$.Svid==-1
VideoExtension(c context.Context, ids []int64) (map[int64]*api.VideoExtension, error)
// cache: -sync=true -batch=10 -max_group=10 -batch_err=break -nullcache=&api.TopicInfo{TopicId:-1} -check_null_code=$==nil||$.TopicId==-1
TopicInfo(c context.Context, ids []int64) (map[int64]*api.TopicInfo, error)
}
*/
package dao
import (
"context"
"sync"
"go-common/app/service/bbq/topic/api"
"go-common/library/stat/prom"
"go-common/library/sync/errgroup"
)
var _ _cache
// VideoExtension get data from cache if miss will call source method, then add to cache.
func (d *Dao) VideoExtension(c context.Context, keys []int64) (res map[int64]*api.VideoExtension, err error) {
if len(keys) == 0 {
return
}
addCache := true
if res, err = d.CacheVideoExtension(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("VideoExtension", int64(len(keys)-len(miss)))
for k, v := range res {
if v == nil || v.Svid == -1 {
delete(res, k)
}
}
missLen := len(miss)
if missLen == 0 {
return
}
missData := make(map[int64]*api.VideoExtension, missLen)
prom.CacheMiss.Add("VideoExtension", 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.RawVideoExtension(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]*api.VideoExtension, len(keys))
}
for k, v := range missData {
res[k] = v
}
if err != nil {
return
}
for _, key := range miss {
if res[key] == nil {
missData[key] = &api.VideoExtension{Svid: -1}
}
}
if !addCache {
return
}
d.AddCacheVideoExtension(c, missData)
return
}
// TopicInfo get data from cache if miss will call source method, then add to cache.
func (d *Dao) TopicInfo(c context.Context, keys []int64) (res map[int64]*api.TopicInfo, err error) {
if len(keys) == 0 {
return
}
addCache := true
if res, err = d.CacheTopicInfo(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("TopicInfo", int64(len(keys)-len(miss)))
for k, v := range res {
if v == nil || v.TopicId == -1 {
delete(res, k)
}
}
missLen := len(miss)
if missLen == 0 {
return
}
missData := make(map[int64]*api.TopicInfo, missLen)
prom.CacheMiss.Add("TopicInfo", 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.RawTopicInfo(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]*api.TopicInfo, len(keys))
}
for k, v := range missData {
res[k] = v
}
if err != nil {
return
}
for _, key := range miss {
if res[key] == nil {
missData[key] = &api.TopicInfo{TopicId: -1}
}
}
if !addCache {
return
}
d.AddCacheTopicInfo(c, missData)
return
}