service.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package channel
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/app-card/model/card/live"
  6. "go-common/app/interface/main/app-card/model/card/operate"
  7. "go-common/app/interface/main/app-channel/conf"
  8. accdao "go-common/app/interface/main/app-channel/dao/account"
  9. actdao "go-common/app/interface/main/app-channel/dao/activity"
  10. arcdao "go-common/app/interface/main/app-channel/dao/archive"
  11. artdao "go-common/app/interface/main/app-channel/dao/article"
  12. audiodao "go-common/app/interface/main/app-channel/dao/audio"
  13. adtdao "go-common/app/interface/main/app-channel/dao/audit"
  14. bgmdao "go-common/app/interface/main/app-channel/dao/bangumi"
  15. carddao "go-common/app/interface/main/app-channel/dao/card"
  16. convergedao "go-common/app/interface/main/app-channel/dao/converge"
  17. gamedao "go-common/app/interface/main/app-channel/dao/game"
  18. livdao "go-common/app/interface/main/app-channel/dao/live"
  19. locdao "go-common/app/interface/main/app-channel/dao/location"
  20. rgdao "go-common/app/interface/main/app-channel/dao/region"
  21. reldao "go-common/app/interface/main/app-channel/dao/relation"
  22. shopdao "go-common/app/interface/main/app-channel/dao/shopping"
  23. specialdao "go-common/app/interface/main/app-channel/dao/special"
  24. tabdao "go-common/app/interface/main/app-channel/dao/tab"
  25. tagdao "go-common/app/interface/main/app-channel/dao/tag"
  26. "go-common/app/interface/main/app-channel/model/card"
  27. "go-common/app/interface/main/app-channel/model/channel"
  28. "go-common/app/interface/main/app-channel/model/tab"
  29. )
  30. // Service channel
  31. type Service struct {
  32. c *conf.Config
  33. // dao
  34. acc *accdao.Dao
  35. arc *arcdao.Dao
  36. act *actdao.Dao
  37. art *artdao.Dao
  38. adt *adtdao.Dao
  39. bgm *bgmdao.Dao
  40. audio *audiodao.Dao
  41. rel *reldao.Dao
  42. sp *shopdao.Dao
  43. tg *tagdao.Dao
  44. cd *carddao.Dao
  45. ce *convergedao.Dao
  46. g *gamedao.Dao
  47. sl *specialdao.Dao
  48. rg *rgdao.Dao
  49. lv *livdao.Dao
  50. loc *locdao.Dao
  51. tab *tabdao.Dao
  52. // tick
  53. tick time.Duration
  54. // cache
  55. cardCache map[int64][]*card.Card
  56. cardPlatCache map[string][]*card.CardPlat
  57. upCardCache map[int64]*operate.Follow
  58. convergeCardCache map[int64]*operate.Converge
  59. gameDownloadCache map[int64]*operate.Download
  60. specialCardCache map[int64]*operate.Special
  61. liveCardCache map[int64][]*live.Card
  62. cardSetCache map[int64]*operate.CardSet
  63. menuCache map[int64][]*tab.Menu
  64. // new region list cache
  65. cachelist map[string][]*channel.Region
  66. limitCache map[int64][]*channel.RegionLimit
  67. configCache map[int64][]*channel.RegionConfig
  68. // audit cache
  69. auditCache map[string]map[int]struct{} // audit mobi_app builds
  70. // infoc
  71. logCh chan interface{}
  72. }
  73. // New channel
  74. func New(c *conf.Config) (s *Service) {
  75. s = &Service{
  76. c: c,
  77. arc: arcdao.New(c),
  78. acc: accdao.New(c),
  79. adt: adtdao.New(c),
  80. art: artdao.New(c),
  81. act: actdao.New(c),
  82. bgm: bgmdao.New(c),
  83. sp: shopdao.New(c),
  84. tg: tagdao.New(c),
  85. cd: carddao.New(c),
  86. ce: convergedao.New(c),
  87. g: gamedao.New(c),
  88. sl: specialdao.New(c),
  89. rg: rgdao.New(c),
  90. audio: audiodao.New(c),
  91. lv: livdao.New(c),
  92. rel: reldao.New(c),
  93. loc: locdao.New(c),
  94. tab: tabdao.New(c),
  95. // tick
  96. tick: time.Duration(c.Tick),
  97. // cache
  98. cardCache: map[int64][]*card.Card{},
  99. cardPlatCache: map[string][]*card.CardPlat{},
  100. upCardCache: map[int64]*operate.Follow{},
  101. convergeCardCache: map[int64]*operate.Converge{},
  102. gameDownloadCache: map[int64]*operate.Download{},
  103. specialCardCache: map[int64]*operate.Special{},
  104. cachelist: map[string][]*channel.Region{},
  105. limitCache: map[int64][]*channel.RegionLimit{},
  106. configCache: map[int64][]*channel.RegionConfig{},
  107. liveCardCache: map[int64][]*live.Card{},
  108. cardSetCache: map[int64]*operate.CardSet{},
  109. menuCache: map[int64][]*tab.Menu{},
  110. // audit cache
  111. auditCache: map[string]map[int]struct{}{},
  112. // infoc
  113. logCh: make(chan interface{}, 1024),
  114. }
  115. s.loadCache()
  116. go s.loadCacheproc()
  117. go s.infocproc()
  118. return
  119. }
  120. func (s *Service) loadCacheproc() {
  121. for {
  122. time.Sleep(s.tick)
  123. s.loadCache()
  124. }
  125. }
  126. func (s *Service) loadCache() {
  127. now := time.Now()
  128. s.loadAuditCache()
  129. s.loadRegionlist()
  130. // card
  131. s.loadCardCache(now)
  132. s.loadConvergeCache()
  133. s.loadSpecialCache()
  134. s.loadLiveCardCache()
  135. s.loadGameDownloadCache()
  136. s.loadCardSetCache()
  137. s.loadMenusCache(now)
  138. }
  139. // Ping is check server ping.
  140. func (s *Service) Ping(c context.Context) (err error) {
  141. if err = s.cd.PingDB(c); err != nil {
  142. return
  143. }
  144. return
  145. }