cache.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package show
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/interface/main/app-resource/model/show"
  7. resource "go-common/app/service/main/resource/model"
  8. "go-common/library/log"
  9. )
  10. func (s *Service) loadTabCache() (err error) {
  11. var (
  12. tmp = map[int64]struct{}{}
  13. ss = map[string][]*show.Tab{}
  14. sideBars *resource.SideBars
  15. )
  16. if sideBars, err = s.rdao.ResSideBar(context.TODO()); err != nil || sideBars == nil {
  17. log.Error("s.sideDao.SideBar error(%v) or nil", err)
  18. return
  19. }
  20. for _, v := range sideBars.SideBar {
  21. if _, ok := tmp[v.ID]; ok {
  22. continue
  23. }
  24. tmp[v.ID] = struct{}{}
  25. st := &show.Tab{}
  26. if !st.TabChange(v, _showAbtest, _deafaultTab) {
  27. continue
  28. }
  29. key := fmt.Sprintf(_initTabKey, st.Plat, st.Language)
  30. ss[key] = append(ss[key], st)
  31. }
  32. if len(ss) == 0 && len(s.tabCache) == 0 {
  33. err = fmt.Errorf("tabCache is null")
  34. return
  35. } else if len(ss) == 0 {
  36. return
  37. }
  38. s.tabCache = ss
  39. s.limitsCahce = sideBars.Limit
  40. log.Info("loadTabCache cache success")
  41. return
  42. }
  43. func (s *Service) loadMenusCache(now time.Time) {
  44. menus, err := s.tdao.Menus(context.TODO(), now)
  45. if err != nil {
  46. log.Error("s.tab.Menus error(%v)", err)
  47. return
  48. }
  49. s.menuCache = menus
  50. log.Info("loadMenusCache cache success")
  51. }
  52. func (s *Service) loadAbTestCache() {
  53. var (
  54. groups string
  55. )
  56. for _, g := range _showAbtest {
  57. groups = groups + g + ","
  58. }
  59. if gLen := len(groups); gLen > 0 {
  60. groups = groups[:gLen-1]
  61. }
  62. res, err := s.rdao.AbTest(context.TODO(), groups)
  63. if err != nil {
  64. log.Error("resource s.rdao.AbTest error(%v)", err)
  65. return
  66. }
  67. s.abtestCache = res
  68. log.Info("loadAbTestCache cache success")
  69. }
  70. func (s *Service) loadCache() (err error) {
  71. now := time.Now()
  72. err = s.loadTabCache()
  73. s.loadMenusCache(now)
  74. s.loadAbTestCache()
  75. s.loadAuditCache()
  76. return
  77. }
  78. func (s *Service) loadCacheproc() {
  79. for {
  80. time.Sleep(s.tick)
  81. s.loadCache()
  82. }
  83. }
  84. func (s *Service) loadShowTabAids() {
  85. tmp := map[int64]struct{}{}
  86. for _, mid := range s.c.ShowTabMids {
  87. tmp[mid] = struct{}{}
  88. }
  89. s.showTabMids = tmp
  90. }