dao.cache.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Code generated by $GOPATH/src/go-common/app/tool/cache/gen. DO NOT EDIT.
  2. /*
  3. Package dao is a generated cache proxy package.
  4. It is generated from:
  5. type _cache interface {
  6. // 获取流完整信息
  7. // cache: -singleflight=true -ignores=||id,sname -nullcache=&model.StreamFullInfo{RoomID:-1} -check_null_code=$!=nil&&$.RoomID<=0
  8. streamFullInfo(c context.Context, id int64, sname string) (res *model.StreamFullInfo, err error)
  9. // 获取rid
  10. // cache: -singleflight=true -ignores=||sname -nullcache=&model.StreamFullInfo{RoomID:-1} -check_null_code=$!=nil&&$.RoomID<=0
  11. streamRIDByName(c context.Context, sname string) (res *model.StreamFullInfo, err error)
  12. // 批量获取接口
  13. // cache: -nullcache=&model.StreamFullInfo{RoomID:-1} -check_null_code=$!=nil&&$.RoomID<=0
  14. multiStreamInfo(c context.Context, rid []int64) (res map[int64]*model.StreamFullInfo, err error)
  15. }
  16. */
  17. package dao
  18. import (
  19. "context"
  20. "go-common/app/service/video/stream-mng/model"
  21. "go-common/library/net/metadata"
  22. "go-common/library/stat/prom"
  23. "go-common/library/log"
  24. "golang.org/x/sync/singleflight"
  25. )
  26. var _ _cache
  27. var cacheSingleFlights = [2]*singleflight.Group{{}, {}}
  28. // streamFullInfo 获取流完整信息
  29. func (d *Dao) streamFullInfo(c context.Context, id int64, sname string) (res *model.StreamFullInfo, err error) {
  30. addCache := true
  31. res, err = d.CacheStreamFullInfo(c, id, sname)
  32. if err != nil {
  33. addCache = false
  34. err = nil
  35. }
  36. defer func() {
  37. if res != nil && res.RoomID <= 0 {
  38. res = nil
  39. }
  40. }()
  41. if res != nil {
  42. log.Warn("get from redis")
  43. prom.CacheHit.Incr("streamFullInfo")
  44. return
  45. }
  46. var rr interface{}
  47. sf := d.cacheSFstreamFullInfo(id, sname)
  48. rr, err, _ = cacheSingleFlights[0].Do(sf, func() (r interface{}, e error) {
  49. prom.CacheMiss.Incr("streamFullInfo")
  50. r, e = d.RawStreamFullInfo(c, id, sname)
  51. return
  52. })
  53. res = rr.(*model.StreamFullInfo)
  54. if err != nil {
  55. return
  56. }
  57. miss := res
  58. if miss == nil {
  59. miss = &model.StreamFullInfo{RoomID: -1}
  60. }
  61. if !addCache {
  62. return
  63. }
  64. d.cache.Save(func() {
  65. d.AddCacheStreamFullInfo(metadata.WithContext(c), id, miss)
  66. })
  67. return
  68. }
  69. // streamRIDByName 获取rid
  70. func (d *Dao) streamRIDByName(c context.Context, id string) (res *model.StreamFullInfo, err error) {
  71. addCache := true
  72. res, err = d.CacheStreamRIDByName(c, id)
  73. if err != nil {
  74. addCache = false
  75. err = nil
  76. }
  77. defer func() {
  78. if res != nil && res.RoomID <= 0 {
  79. res = nil
  80. }
  81. }()
  82. if res != nil {
  83. prom.CacheHit.Incr("streamRIDByName")
  84. return
  85. }
  86. var rr interface{}
  87. sf := d.cacheSFstreamRIDByName(id)
  88. rr, err, _ = cacheSingleFlights[1].Do(sf, func() (r interface{}, e error) {
  89. prom.CacheMiss.Incr("streamRIDByName")
  90. r, e = d.RawStreamRIDByName(c, id)
  91. return
  92. })
  93. res = rr.(*model.StreamFullInfo)
  94. if err != nil {
  95. return
  96. }
  97. miss := res
  98. if miss == nil {
  99. miss = &model.StreamFullInfo{RoomID: -1}
  100. }
  101. if !addCache {
  102. return
  103. }
  104. d.cache.Save(func() {
  105. d.AddCacheStreamRIDByName(metadata.WithContext(c), id, miss)
  106. })
  107. return
  108. }
  109. // multiStreamInfo 批量获取接口
  110. func (d *Dao) multiStreamInfo(c context.Context, keys []int64) (res map[int64]*model.StreamFullInfo, err error) {
  111. if len(keys) == 0 {
  112. return
  113. }
  114. addCache := true
  115. res, err = d.CacheMultiStreamInfo(c, keys)
  116. if err != nil {
  117. addCache = false
  118. res = nil
  119. err = nil
  120. }
  121. var miss []int64
  122. for _, key := range keys {
  123. if (res == nil) || (res[key] == nil) {
  124. miss = append(miss, key)
  125. }
  126. }
  127. prom.CacheHit.Add("multiStreamInfo", int64(len(keys)-len(miss)))
  128. defer func() {
  129. for k, v := range res {
  130. if v != nil && v.RoomID <= 0 {
  131. delete(res, k)
  132. }
  133. }
  134. }()
  135. if len(miss) == 0 {
  136. log.Warn("get from redis")
  137. return
  138. }
  139. var missData map[int64]*model.StreamFullInfo
  140. prom.CacheMiss.Add("multiStreamInfo", int64(len(miss)))
  141. missData, err = d.RawMultiStreamInfo(c, miss)
  142. if res == nil {
  143. res = make(map[int64]*model.StreamFullInfo)
  144. }
  145. for k, v := range missData {
  146. res[k] = v
  147. }
  148. if err != nil {
  149. return
  150. }
  151. for _, key := range keys {
  152. if res[key] == nil {
  153. if missData == nil {
  154. missData = make(map[int64]*model.StreamFullInfo, len(keys))
  155. }
  156. missData[key] = &model.StreamFullInfo{RoomID: -1}
  157. }
  158. }
  159. if !addCache {
  160. return
  161. }
  162. d.cache.Save(func() {
  163. d.AddCacheMultiStreamInfo(metadata.WithContext(c), missData)
  164. })
  165. return
  166. }