service.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/mcn/model"
  6. "go-common/app/interface/main/mcn/conf"
  7. "go-common/app/interface/main/mcn/dao/bfs"
  8. "go-common/app/interface/main/mcn/dao/cache"
  9. "go-common/app/interface/main/mcn/dao/global"
  10. "go-common/app/interface/main/mcn/dao/mcndao"
  11. "go-common/app/interface/main/mcn/dao/msg"
  12. "go-common/app/interface/main/mcn/tool/worker"
  13. "go-common/app/interface/main/mcn/dao/datadao"
  14. "github.com/bluele/gcache"
  15. )
  16. // Service struct
  17. type Service struct {
  18. c *conf.Config
  19. mcndao *mcndao.Dao
  20. bfsdao *bfs.Dao
  21. notifych chan func()
  22. msg *msg.Dao
  23. msgMap map[model.MSGType]*model.MSG
  24. worker *worker.Pool
  25. uniqueChecker *UniqueCheck
  26. datadao *datadao.Dao
  27. }
  28. // New init
  29. func New(c *conf.Config) (s *Service) {
  30. var localcache = gcache.New(c.RankCache.Size).Simple().Build()
  31. global.Init(c)
  32. s = &Service{
  33. c: c,
  34. mcndao: mcndao.New(c, localcache),
  35. bfsdao: bfs.New(c),
  36. notifych: make(chan func(), 10240),
  37. msg: msg.New(c),
  38. worker: worker.New(nil),
  39. uniqueChecker: NewUniqueCheck(),
  40. datadao: datadao.New(c),
  41. }
  42. s.datadao.Client.Debug = true
  43. s.refreshCache()
  44. s.setMsgTypeMap()
  45. go s.cacheproc()
  46. return s
  47. }
  48. // Ping Service
  49. func (s *Service) Ping(c context.Context) (err error) {
  50. return nil
  51. }
  52. // Close Service
  53. func (s *Service) Close() {
  54. s.worker.Close()
  55. s.worker.Wait()
  56. s.mcndao.Close()
  57. }
  58. func (s *Service) refreshCache() {
  59. cache.LoadCache()
  60. s.loadMcnUniqueCache()
  61. }
  62. func (s *Service) cacheproc() {
  63. for {
  64. time.Sleep(5 * time.Minute)
  65. s.refreshCache()
  66. }
  67. }