service.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package space
  2. import (
  3. "context"
  4. "runtime"
  5. "time"
  6. "go-common/app/interface/main/app-interface/conf"
  7. accdao "go-common/app/interface/main/app-interface/dao/account"
  8. arcdao "go-common/app/interface/main/app-interface/dao/archive"
  9. artdao "go-common/app/interface/main/app-interface/dao/article"
  10. audiodao "go-common/app/interface/main/app-interface/dao/audio"
  11. bgmdao "go-common/app/interface/main/app-interface/dao/bangumi"
  12. bplusdao "go-common/app/interface/main/app-interface/dao/bplus"
  13. coindao "go-common/app/interface/main/app-interface/dao/coin"
  14. commdao "go-common/app/interface/main/app-interface/dao/community"
  15. elecdao "go-common/app/interface/main/app-interface/dao/elec"
  16. favdao "go-common/app/interface/main/app-interface/dao/favorite"
  17. livedao "go-common/app/interface/main/app-interface/dao/live"
  18. memberdao "go-common/app/interface/main/app-interface/dao/member"
  19. paydao "go-common/app/interface/main/app-interface/dao/pay"
  20. reldao "go-common/app/interface/main/app-interface/dao/relation"
  21. srchdao "go-common/app/interface/main/app-interface/dao/search"
  22. shopdao "go-common/app/interface/main/app-interface/dao/shop"
  23. spcdao "go-common/app/interface/main/app-interface/dao/space"
  24. tagdao "go-common/app/interface/main/app-interface/dao/tag"
  25. thumbupdao "go-common/app/interface/main/app-interface/dao/thumbup"
  26. "go-common/library/log"
  27. )
  28. // Service is space service
  29. type Service struct {
  30. c *conf.Config
  31. arcDao *arcdao.Dao
  32. spcDao *spcdao.Dao
  33. accDao *accdao.Dao
  34. coinDao *coindao.Dao
  35. commDao *commdao.Dao
  36. srchDao *srchdao.Dao
  37. favDao *favdao.Dao
  38. bgmDao *bgmdao.Dao
  39. tagDao *tagdao.Dao
  40. liveDao *livedao.Dao
  41. elecDao *elecdao.Dao
  42. artDao *artdao.Dao
  43. audioDao *audiodao.Dao
  44. relDao *reldao.Dao
  45. bplusDao *bplusdao.Dao
  46. shopDao *shopdao.Dao
  47. thumbupDao *thumbupdao.Dao
  48. payDao *paydao.Dao
  49. memberDao *memberdao.Dao
  50. // chan
  51. mCh chan func()
  52. tick time.Duration
  53. BlackList map[int64]struct{}
  54. }
  55. // New new space
  56. func New(c *conf.Config) (s *Service) {
  57. s = &Service{
  58. c: c,
  59. arcDao: arcdao.New(c),
  60. spcDao: spcdao.New(c),
  61. accDao: accdao.New(c),
  62. coinDao: coindao.New(c),
  63. commDao: commdao.New(c),
  64. srchDao: srchdao.New(c),
  65. favDao: favdao.New(c),
  66. bgmDao: bgmdao.New(c),
  67. tagDao: tagdao.New(c),
  68. liveDao: livedao.New(c),
  69. elecDao: elecdao.New(c),
  70. artDao: artdao.New(c),
  71. audioDao: audiodao.New(c),
  72. relDao: reldao.New(c),
  73. bplusDao: bplusdao.New(c),
  74. shopDao: shopdao.New(c),
  75. thumbupDao: thumbupdao.New(c),
  76. payDao: paydao.New(c),
  77. memberDao: memberdao.New(c),
  78. // mc proc
  79. mCh: make(chan func(), 1024),
  80. tick: time.Duration(c.Tick),
  81. BlackList: make(map[int64]struct{}),
  82. }
  83. // video db
  84. for i := 0; i < runtime.NumCPU(); i++ {
  85. go s.cacheproc()
  86. }
  87. if c != nil && c.Space != nil {
  88. for _, mid := range c.Space.ForbidMid {
  89. s.BlackList[mid] = struct{}{}
  90. }
  91. }
  92. s.loadBlacklist()
  93. go s.blacklistproc()
  94. return
  95. }
  96. // addCache add archive to mc or redis
  97. func (s *Service) addCache(f func()) {
  98. select {
  99. case s.mCh <- f:
  100. default:
  101. log.Warn("cacheproc chan full")
  102. }
  103. }
  104. // cacheproc write memcache and stat redis use goroutine
  105. func (s *Service) cacheproc() {
  106. for {
  107. f := <-s.mCh
  108. f()
  109. }
  110. }
  111. // Ping check server ok
  112. func (s *Service) Ping(c context.Context) (err error) {
  113. return
  114. }
  115. // loadBlacklist
  116. func (s *Service) loadBlacklist() {
  117. list, err := s.spcDao.Blacklist(context.Background())
  118. if err != nil {
  119. log.Error("%+v", err)
  120. return
  121. }
  122. s.BlackList = list
  123. }
  124. func (s *Service) blacklistproc() {
  125. for {
  126. time.Sleep(s.tick)
  127. s.loadBlacklist()
  128. }
  129. }