pic_list.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package v2
  2. import (
  3. "context"
  4. "time"
  5. "go-common/library/log"
  6. v2pb "go-common/app/interface/live/app-interface/api/http/v2"
  7. )
  8. // 获取分区入口
  9. func (s *IndexService) getAreaEntrance(ctx context.Context) (res []*v2pb.MAreaEntrance) {
  10. moduleInfoMap := s.GetAllModuleInfoMapFromCache(ctx)
  11. listMap := s.getAreaEntranceListMapFromCache(ctx)
  12. res = make([]*v2pb.MAreaEntrance, 0)
  13. for _, m := range moduleInfoMap[_entranceType] {
  14. if l, ok := listMap[m.Id]; ok {
  15. res = append(res, &v2pb.MAreaEntrance{
  16. ModuleInfo: m,
  17. List: l,
  18. })
  19. }
  20. }
  21. return
  22. }
  23. // load from cache
  24. func (s *IndexService) getAreaEntranceListMapFromCache(ctx context.Context) (res map[int64][]*v2pb.PicItem) {
  25. // load
  26. i := s.areaEntranceListMap.Load()
  27. // assert
  28. res, ok := i.(map[int64][]*v2pb.PicItem)
  29. if ok {
  30. return
  31. }
  32. // 回源&log
  33. res = s.getAreaEntranceListMap(ctx)
  34. log.Warn("[getAreaEntranceListMapFromCache]memory cache miss!! i:%+v; res:%+v", i, res)
  35. return
  36. }
  37. // getAreaEntranceListMap raw
  38. func (s *IndexService) getAreaEntranceListMap(ctx context.Context) (listMap map[int64][]*v2pb.PicItem) {
  39. moduleIds := s.getIdsFromModuleMap(ctx, []int64{_entranceType})
  40. if len(moduleIds) <= 0 {
  41. return
  42. }
  43. areaResult, err := s.roomDao.GetAreaEntrance(ctx, moduleIds)
  44. if err != nil {
  45. log.Error("[loadAreaEntranceCache]roomDao.GetAreaEntrance get data error: %+v, data: %+v", err, areaResult)
  46. return
  47. }
  48. if len(areaResult) > 0 {
  49. listMap = make(map[int64][]*v2pb.PicItem)
  50. for moduleId, i := range areaResult {
  51. if i != nil && i.List != nil {
  52. for _, ii := range i.List {
  53. listMap[moduleId] = append(listMap[moduleId], &v2pb.PicItem{
  54. Id: ii.Id,
  55. Pic: ii.Pic,
  56. Link: ii.Link,
  57. Title: ii.Title,
  58. })
  59. }
  60. }
  61. }
  62. }
  63. return
  64. }
  65. // ticker
  66. func (s *IndexService) areaEntranceProc() {
  67. for {
  68. time.Sleep(time.Minute * 1)
  69. s.loadAreaEntranceCache()
  70. }
  71. }
  72. func (s *IndexService) loadAreaEntranceCache() {
  73. areaEntranceListMap := s.getAreaEntranceListMap(context.TODO())
  74. if len(areaEntranceListMap) > 0 {
  75. s.areaEntranceListMap.Store(areaEntranceListMap)
  76. log.Info("[loadAreaEntranceCache]load data success!")
  77. }
  78. return
  79. }