service.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package feed
  2. import (
  3. "time"
  4. "go-common/app/interface/main/app-card/model/card/ai"
  5. "go-common/app/interface/main/app-card/model/card/operate"
  6. "go-common/app/interface/main/app-card/model/card/rank"
  7. "go-common/app/interface/main/app-intl/conf"
  8. accdao "go-common/app/interface/main/app-intl/dao/account"
  9. arcdao "go-common/app/interface/main/app-intl/dao/archive"
  10. blkdao "go-common/app/interface/main/app-intl/dao/black"
  11. carddao "go-common/app/interface/main/app-intl/dao/card"
  12. locdao "go-common/app/interface/main/app-intl/dao/location"
  13. rankdao "go-common/app/interface/main/app-intl/dao/rank"
  14. rcmdao "go-common/app/interface/main/app-intl/dao/recommend"
  15. reldao "go-common/app/interface/main/app-intl/dao/relation"
  16. tagdao "go-common/app/interface/main/app-intl/dao/tag"
  17. "go-common/library/log"
  18. "go-common/library/stat/prom"
  19. )
  20. // Service is show service.
  21. type Service struct {
  22. c *conf.Config
  23. pHit *prom.Prom
  24. pMiss *prom.Prom
  25. // dao
  26. rcmd *rcmdao.Dao
  27. tg *tagdao.Dao
  28. blk *blkdao.Dao
  29. rank *rankdao.Dao
  30. card *carddao.Dao
  31. // rpc
  32. arc *arcdao.Dao
  33. acc *accdao.Dao
  34. rel *reldao.Dao
  35. loc *locdao.Dao
  36. // tick
  37. tick time.Duration
  38. // black cache
  39. blackCache map[int64]struct{} // black aids
  40. // ai cache
  41. rcmdCache []*ai.Item
  42. // rank cache
  43. rankCache []*rank.Rank
  44. // follow cache
  45. followCache map[int64]*operate.Follow
  46. // group cache
  47. groupCache map[int64]int
  48. // cache
  49. cacheCh chan func()
  50. // infoc
  51. logCh chan interface{}
  52. }
  53. // New new a show service.
  54. func New(c *conf.Config) (s *Service) {
  55. s = &Service{
  56. c: c,
  57. pHit: prom.CacheHit,
  58. pMiss: prom.CacheMiss,
  59. // dao
  60. rcmd: rcmdao.New(c),
  61. blk: blkdao.New(c),
  62. rank: rankdao.New(c),
  63. tg: tagdao.New(c),
  64. card: carddao.New(c),
  65. // rpc
  66. arc: arcdao.New(c),
  67. rel: reldao.New(c),
  68. acc: accdao.New(c),
  69. loc: locdao.New(c),
  70. // tick
  71. tick: time.Duration(c.Tick),
  72. // group cache
  73. groupCache: map[int64]int{},
  74. // cache
  75. cacheCh: make(chan func(), 1024),
  76. // infoc
  77. logCh: make(chan interface{}, 1024),
  78. }
  79. s.loadBlackCache()
  80. s.loadRcmdCache()
  81. s.loadRankCache()
  82. s.loadUpCardCache()
  83. s.loadGroupCache()
  84. go s.cacheproc()
  85. go s.blackproc()
  86. go s.rcmdproc()
  87. go s.rankproc()
  88. go s.upCardproc()
  89. go s.groupproc()
  90. go s.infocproc()
  91. return
  92. }
  93. // addCache is.
  94. func (s *Service) addCache(f func()) {
  95. select {
  96. case s.cacheCh <- f:
  97. default:
  98. log.Warn("cacheproc chan full")
  99. }
  100. }
  101. // cacheproc is.
  102. func (s *Service) cacheproc() {
  103. for {
  104. f, ok := <-s.cacheCh
  105. if !ok {
  106. log.Warn("cache proc exit")
  107. return
  108. }
  109. f()
  110. }
  111. }