dao.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package archive
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/app-interface/conf"
  6. "go-common/app/service/main/archive/api"
  7. arcrpc "go-common/app/service/main/archive/api/gorpc"
  8. "go-common/app/service/main/archive/model/archive"
  9. "go-common/library/cache/memcache"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/sync/errgroup"
  14. "github.com/pkg/errors"
  15. )
  16. // Dao is archive dao.
  17. type Dao struct {
  18. // http client
  19. client *bm.Client
  20. // rpc
  21. arcRPC *arcrpc.Service2
  22. // memcache
  23. arcMc *memcache.Pool
  24. expireArc int32
  25. }
  26. // New new a archive dao.
  27. func New(c *conf.Config) (d *Dao) {
  28. d = &Dao{
  29. // http client
  30. client: bm.NewClient(c.HTTPWrite),
  31. arcRPC: arcrpc.New2(c.ArchiveRPC),
  32. // memcache
  33. arcMc: memcache.NewPool(c.Memcache.Archive.Config),
  34. expireArc: int32(time.Duration(c.Memcache.Archive.ArchiveExpire) / time.Second),
  35. }
  36. return
  37. }
  38. // UpArcs3 get upper archives
  39. func (d *Dao) UpArcs3(c context.Context, mid int64, pn, ps int) (as []*api.Arc, err error) {
  40. arg := &archive.ArgUpArcs2{Mid: mid, Pn: pn, Ps: ps}
  41. if as, err = d.arcRPC.UpArcs3(c, arg); err != nil {
  42. if ecode.Cause(err) == ecode.NothingFound {
  43. err = nil
  44. }
  45. }
  46. return
  47. }
  48. // UpCount2 get upper count.
  49. func (d *Dao) UpCount2(c context.Context, mid int64) (cnt int, err error) {
  50. arg := &archive.ArgUpCount2{Mid: mid}
  51. return d.arcRPC.UpCount2(c, arg)
  52. }
  53. // Archives multi get archives.
  54. func (d *Dao) Archives(c context.Context, aids []int64) (as map[int64]*api.Arc, err error) {
  55. if len(aids) == 0 {
  56. return
  57. }
  58. g, ctx := errgroup.WithContext(c)
  59. g.Go(func() (err error) {
  60. var missed []int64
  61. if as, missed, err = d.arcsCache(ctx, aids); err != nil {
  62. log.Error("%+v", err)
  63. missed = aids
  64. err = nil
  65. }
  66. if len(missed) == 0 {
  67. return
  68. }
  69. var tmp map[int64]*api.Arc
  70. arg := &archive.ArgAids2{Aids: missed}
  71. if tmp, err = d.arcRPC.Archives3(ctx, arg); err != nil {
  72. log.Error("d.arcRPC.Archives3(%v) error(%v)", arg, err)
  73. return
  74. }
  75. for aid, a := range tmp {
  76. as[aid] = a
  77. }
  78. return
  79. })
  80. var stm map[int64]*api.Stat
  81. g.Go(func() (err error) {
  82. var missed []int64
  83. if stm, missed, err = d.statsCache(ctx, aids); err != nil {
  84. log.Error("%+v", err)
  85. missed = aids
  86. err = nil
  87. }
  88. if len(missed) == 0 {
  89. return
  90. }
  91. var tmp map[int64]*api.Stat
  92. arg := &archive.ArgAids2{Aids: missed}
  93. if tmp, err = d.arcRPC.Stats3(ctx, arg); err != nil {
  94. log.Error("d.arcRPC.Stats3(%v) error(%v)", arg, err)
  95. err = nil
  96. return
  97. }
  98. for aid, st := range tmp {
  99. stm[aid] = st
  100. }
  101. return
  102. })
  103. if err = g.Wait(); err != nil {
  104. return
  105. }
  106. for aid, a := range as {
  107. if st, ok := stm[aid]; ok {
  108. a.Stat = *st
  109. }
  110. }
  111. return
  112. }
  113. // Archives2 multi get archives.
  114. func (d *Dao) Archives2(c context.Context, aids []int64) (am map[int64]*api.Arc, err error) {
  115. if len(aids) == 0 {
  116. return
  117. }
  118. var avMissed, stMissed []int64
  119. if am, avMissed, stMissed, err = d.avWithStCaches(c, aids); err != nil {
  120. avMissed = aids
  121. log.Error("%+v", err)
  122. err = nil
  123. }
  124. g, ctx := errgroup.WithContext(c)
  125. if len(avMissed) != 0 {
  126. g.Go(func() (err error) {
  127. arg := &archive.ArgAids2{Aids: avMissed}
  128. avm, err := d.arcRPC.Archives3(ctx, arg)
  129. if err != nil {
  130. err = errors.Wrapf(err, "%v", arg)
  131. return
  132. }
  133. for aid, a := range avm {
  134. am[aid] = a
  135. }
  136. return
  137. })
  138. }
  139. var stm map[int64]*api.Stat
  140. if len(stMissed) != 0 {
  141. g.Go(func() error {
  142. arg := &archive.ArgAids2{Aids: stMissed}
  143. if stm, err = d.arcRPC.Stats3(ctx, arg); err != nil {
  144. log.Error("d.arcRPC.Stats3(%v) error(%v)", arg, err)
  145. }
  146. return nil
  147. })
  148. }
  149. if err = g.Wait(); err != nil {
  150. return
  151. }
  152. for aid, a := range am {
  153. if st, ok := stm[aid]; ok {
  154. a.Stat = *st
  155. }
  156. }
  157. return
  158. }