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