bilibili-backup/app/service/video/stream-mng/dao/dao.cache.go
2019-04-22 02:59:20 +00:00

178 lines
4.2 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: -singleflight=true -ignores=||id,sname -nullcache=&model.StreamFullInfo{RoomID:-1} -check_null_code=$!=nil&&$.RoomID<=0
streamFullInfo(c context.Context, id int64, sname string) (res *model.StreamFullInfo, err error)
// 获取rid
// cache: -singleflight=true -ignores=||sname -nullcache=&model.StreamFullInfo{RoomID:-1} -check_null_code=$!=nil&&$.RoomID<=0
streamRIDByName(c context.Context, sname string) (res *model.StreamFullInfo, err error)
// 批量获取接口
// cache: -nullcache=&model.StreamFullInfo{RoomID:-1} -check_null_code=$!=nil&&$.RoomID<=0
multiStreamInfo(c context.Context, rid []int64) (res map[int64]*model.StreamFullInfo, err error)
}
*/
package dao
import (
"context"
"go-common/app/service/video/stream-mng/model"
"go-common/library/net/metadata"
"go-common/library/stat/prom"
"go-common/library/log"
"golang.org/x/sync/singleflight"
)
var _ _cache
var cacheSingleFlights = [2]*singleflight.Group{{}, {}}
// streamFullInfo 获取流完整信息
func (d *Dao) streamFullInfo(c context.Context, id int64, sname string) (res *model.StreamFullInfo, err error) {
addCache := true
res, err = d.CacheStreamFullInfo(c, id, sname)
if err != nil {
addCache = false
err = nil
}
defer func() {
if res != nil && res.RoomID <= 0 {
res = nil
}
}()
if res != nil {
log.Warn("get from redis")
prom.CacheHit.Incr("streamFullInfo")
return
}
var rr interface{}
sf := d.cacheSFstreamFullInfo(id, sname)
rr, err, _ = cacheSingleFlights[0].Do(sf, func() (r interface{}, e error) {
prom.CacheMiss.Incr("streamFullInfo")
r, e = d.RawStreamFullInfo(c, id, sname)
return
})
res = rr.(*model.StreamFullInfo)
if err != nil {
return
}
miss := res
if miss == nil {
miss = &model.StreamFullInfo{RoomID: -1}
}
if !addCache {
return
}
d.cache.Save(func() {
d.AddCacheStreamFullInfo(metadata.WithContext(c), id, miss)
})
return
}
// streamRIDByName 获取rid
func (d *Dao) streamRIDByName(c context.Context, id string) (res *model.StreamFullInfo, err error) {
addCache := true
res, err = d.CacheStreamRIDByName(c, id)
if err != nil {
addCache = false
err = nil
}
defer func() {
if res != nil && res.RoomID <= 0 {
res = nil
}
}()
if res != nil {
prom.CacheHit.Incr("streamRIDByName")
return
}
var rr interface{}
sf := d.cacheSFstreamRIDByName(id)
rr, err, _ = cacheSingleFlights[1].Do(sf, func() (r interface{}, e error) {
prom.CacheMiss.Incr("streamRIDByName")
r, e = d.RawStreamRIDByName(c, id)
return
})
res = rr.(*model.StreamFullInfo)
if err != nil {
return
}
miss := res
if miss == nil {
miss = &model.StreamFullInfo{RoomID: -1}
}
if !addCache {
return
}
d.cache.Save(func() {
d.AddCacheStreamRIDByName(metadata.WithContext(c), id, miss)
})
return
}
// multiStreamInfo 批量获取接口
func (d *Dao) multiStreamInfo(c context.Context, keys []int64) (res map[int64]*model.StreamFullInfo, err error) {
if len(keys) == 0 {
return
}
addCache := true
res, err = d.CacheMultiStreamInfo(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("multiStreamInfo", int64(len(keys)-len(miss)))
defer func() {
for k, v := range res {
if v != nil && v.RoomID <= 0 {
delete(res, k)
}
}
}()
if len(miss) == 0 {
log.Warn("get from redis")
return
}
var missData map[int64]*model.StreamFullInfo
prom.CacheMiss.Add("multiStreamInfo", int64(len(miss)))
missData, err = d.RawMultiStreamInfo(c, miss)
if res == nil {
res = make(map[int64]*model.StreamFullInfo)
}
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.StreamFullInfo, len(keys))
}
missData[key] = &model.StreamFullInfo{RoomID: -1}
}
}
if !addCache {
return
}
d.cache.Save(func() {
d.AddCacheMultiStreamInfo(metadata.WithContext(c), missData)
})
return
}