cache.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/video/stream-mng/model"
  6. )
  7. //go:generate $GOPATH/src/go-common/app/tool/cache/gen
  8. type _cache interface {
  9. // 获取流完整信息
  10. // cache: -singleflight=true -ignores=||id,sname -nullcache=&model.StreamFullInfo{RoomID:-1} -check_null_code=$!=nil&&$.RoomID<=0
  11. streamFullInfo(c context.Context, id int64, sname string) (res *model.StreamFullInfo, err error)
  12. // 获取rid
  13. // cache: -singleflight=true -ignores=||sname -nullcache=&model.StreamFullInfo{RoomID:-1} -check_null_code=$!=nil&&$.RoomID<=0
  14. streamRIDByName(c context.Context, sname string) (res *model.StreamFullInfo, err error)
  15. // 批量获取接口
  16. // cache: -nullcache=&model.StreamFullInfo{RoomID:-1} -check_null_code=$!=nil&&$.RoomID<=0
  17. multiStreamInfo(c context.Context, rid []int64) (res map[int64]*model.StreamFullInfo, err error)
  18. }
  19. func (d *Dao) cacheSFstreamFullInfo(id int64, sname string) string {
  20. if sname != "" {
  21. return fmt.Sprintf("sf_sname_%s", sname)
  22. }
  23. return fmt.Sprintf("sf_rid_%d", id)
  24. }
  25. func (d *Dao) cacheSFstreamRIDByName(sname string) string {
  26. return fmt.Sprintf("sf_rid_map_name_%s", sname)
  27. }
  28. // StreamFullInfo 传入rid或者sname 获取房间流信息
  29. func (d *Dao) StreamFullInfo(c context.Context, rid int64, sname string) (res *model.StreamFullInfo, err error) {
  30. info, err := d.streamFullInfo(c, rid, sname)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if info == nil {
  35. return nil, fmt.Errorf("can not find by room_id=%d", rid)
  36. }
  37. if len(info.List) == 1 && info.List[0].StreamName == "miss" {
  38. return nil, fmt.Errorf("can not find any info by room_id=%d", rid)
  39. }
  40. return info, nil
  41. }
  42. // OriginUpStreamInfo 原始上行流名和src
  43. func (d *Dao) OriginUpStreamInfo(c context.Context, rid int64) (sname string, origin int64, err error) {
  44. info, err := d.streamFullInfo(c, rid, "")
  45. if err != nil {
  46. return "", 0, err
  47. }
  48. if info == nil {
  49. return "", 0, fmt.Errorf("can not find by room_id=%d", rid)
  50. }
  51. for _, v := range info.List {
  52. if v.Type == 1 {
  53. // 优先级高
  54. if v.Origin != 0 {
  55. return v.StreamName, v.Origin, nil
  56. }
  57. return v.StreamName, v.DefaultUpStream, nil
  58. }
  59. }
  60. return "", 0, fmt.Errorf("can not find by room_id=%d", rid)
  61. }
  62. func (d *Dao) DefaultUpStreamInfo(c context.Context, rid int64) (sname string, origin int64, err error) {
  63. info, err := d.streamFullInfo(c, rid, "")
  64. if err != nil {
  65. return "", 0, err
  66. }
  67. if info == nil {
  68. return "", 0, fmt.Errorf("can not find by room_id=%d", rid)
  69. }
  70. for _, v := range info.List {
  71. if v.Type == 1 {
  72. return v.StreamName, v.DefaultUpStream, nil
  73. }
  74. }
  75. return "", 0, fmt.Errorf("can not find by room_id=%d", rid)
  76. }
  77. // OriginUpStreamInfoBySName 查询流的上行,正在推流上行和默认上行; 包含备用流
  78. func (d *Dao) OriginUpStreamInfoBySName(c context.Context, sname string) (rid int64, origin int64, err error) {
  79. info, err := d.streamFullInfo(c, 0, sname)
  80. if err != nil {
  81. return 0, 0, err
  82. }
  83. if info == nil {
  84. return 0, 0, fmt.Errorf("can not find by sname=%s", sname)
  85. }
  86. for _, v := range info.List {
  87. if v.StreamName == sname {
  88. // 优先级高
  89. if v.Origin != 0 {
  90. return info.RoomID, v.Origin, nil
  91. }
  92. return info.RoomID, v.DefaultUpStream, nil
  93. }
  94. }
  95. return 0, 0, fmt.Errorf("can not find by sname=%s", sname)
  96. }
  97. // StreamRIDByName 获取rid
  98. func (d *Dao) StreamRIDByName(c context.Context, sname string) (int64, error) {
  99. info, err := d.streamRIDByName(c, sname)
  100. if err != nil {
  101. return -1, err
  102. }
  103. if info != nil && info.RoomID > 0 {
  104. return info.RoomID, nil
  105. }
  106. return -1, fmt.Errorf("can not find by sname=%s", sname)
  107. }
  108. // MultiStreamInfo 批量接口
  109. func (d *Dao) MultiStreamInfo(c context.Context, rids []int64) (res map[int64]*model.StreamFullInfo, err error) {
  110. infos, err := d.multiStreamInfo(c, rids)
  111. if err != nil {
  112. return res, err
  113. }
  114. resp := map[int64]*model.StreamFullInfo{}
  115. for k, v := range infos {
  116. if len(v.List) == 1 && v.List[0].StreamName == "miss" {
  117. continue
  118. }
  119. resp[k] = v
  120. }
  121. return resp, nil
  122. }