ZBLOG

func GetUserHealthDiary(userId int) ai_common.UserHealthDiary { resp := ai_common.UserHealthDiary{} date := time.Now().Format("2026-01-02") dbData, isExist := dbrespository.GetUserHealthDiaryByUserId(userId, date) if !isExist { // create tes...

要通过协程优化这个函数,我们可以使用 Go 的 sync.WaitGroup 来并行执行每一个获取用户健康日记信息的操作。这样可以提高效率,特别是在有多个 IO 操作或计算时。以下是修改后的代码示例:

  1. package main
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. func GetUserHealthDiary(userId int) ai_common.UserHealthDiary {
  7. resp := ai_common.UserHealthDiary{}
  8. date := time.Now().Format("2006-01-02") // 使用正确的日期格式
  9. dbData, isExist := dbrespository.GetUserHealthDiaryByUserId(userId, date)
  10. if !isExist {
  11. // create test data
  12. dbData.UserID = userId
  13. resp.RecordDate = dbData.RecordDate // 记录健康日记的日期,默认是当天
  14. dbrespository.CreateUserHealthDiary(&dbData)
  15. }
  16. resp.RecordDate = dbData.RecordDate // 记录健康日记的日期,默认是当天
  17. var wg sync.WaitGroup
  18. wg.Add(1)
  19. go func() {
  20. defer wg.Done()
  21. resp.HappinessIndex = getHappinessIndex(dbData.HappinessIndexID)
  22. dbData.HappinessIndexID = resp.HappinessIndex.ID
  23. }()
  24. wg.Add(1)
  25. go func() {
  26. defer wg.Done()
  27. resp.PsychologicalPressure = getPsychologicalPressure(dbData.PsychologicalPressureID)
  28. }()
  29. wg.Add(1)
  30. go func() {
  31. defer wg.Done()
  32. resp.WorkStudyIntensity = getWorkStudyIntensity(dbData.WorkStudyIntensityID)
  33. }()
  34. wg.Add(1)
  35. go func() {
  36. defer wg.Done()
  37. resp.OvertimeSituation = getOvertimeSituation(dbData.OvertimeSituationID)
  38. }()
  39. wg.Add(1)
  40. go func() {
  41. defer wg.Done()
  42. resp.ElectronicDeviceTime = getElectronicDeviceTime(dbData.ElectronicDeviceTimeID)
  43. }()
  44. wg.Add(1)
  45. go func() {
  46. defer wg.Done()
  47. resp.ExerciseIntensity = getExerciseIntensity(dbData.ExerciseIntensityID)
  48. }()
  49. for _, healthDiaryBadLiving := range dbData.HealthDiaryBadLiving {
  50. resp.BadLivingBehaviors = append(resp.BadLivingBehaviors, healthDiaryBadLiving.BadLiving)
  51. }
  52. if len(dbData.HealthDiaryBadLiving) == 0 {
  53. // create test data
  54. }
  55. wg.Add(1)
  56. go func() {
  57. defer wg.Done()
  58. resp.BadSocialEmotions = getBadSocialEmotions(dbData.BadSocialEmotionsID)
  59. dbData.BadSocialEmotionsID = resp.BadSocialEmotions.ID
  60. }()
  61. wg.Add(1)
  62. go func() {
  63. defer wg.Done()
  64. resp.StayUpLastNight = getStayUpLastNight(dbData.StayUpLastNightID)
  65. dbData.StayUpLastNightID = resp.StayUpLastNight.ID
  66. }()
  67. wg.Add(1)
  68. go func() {
  69. defer wg.Done()
  70. resp.SleepQuality = getSleepQuality(dbData.SleepQualityID)
  71. dbData.SleepQualityID = resp.SleepQuality.ID
  72. }()
  73. for _, healthDiaryBadSleep := range dbData.HealthDiaryBadSleep {
  74. resp.BadSleep = append(resp.BadSleep, healthDiaryBadSleep.BadSleep)
  75. }
  76. if len(dbData.HealthDiaryBadSleep) == 0 {
  77. // create test data
  78. }
  79. wg.Add(1)
  80. go func() {
  81. defer wg.Done()
  82. resp.NoonBreak = getNoonBreak(dbData.NoonBreakID)
  83. dbData.NoonBreakID = resp.NoonBreak.ID
  84. }()
  85. wg.Add(1)
  86. go func() {
  87. defer wg.Done()
  88. resp.WaterIntake= getWaterIntake(dbData.WaterIntakeID )
  89. dbData.WaterIntakeID=resp.WaterIntake.ID
  90. }()
  91. wg.Add(1)
  92. go func(){
  93. defer wg.Done ()
  94. resp.EatingSituation=getEatingSituation (dbdata.eatingsituationid )
  95. dbdata.eatingsituationid=resp.eatingsituation.id
  96. }()
  97. wg.Wait()
  98. for _, healthDiaryBadDining := range dbData.HealthDiaryBadDining {
  99. resp.BadDining=append(resp.BadDining,healthDiaryBadDining.Beat.Dining )
  100. }
  101. if len(dbdata.healthdiarybaddining)==0{
  102. //create test data
  103. }
  104. return resp
  105. }

注意事项:

  • 并发安全:如果 dbrespository 和相关的方法不是线程安全的(例如直接读写共享数据),在多个 goroutine 中同时调用可能会引发问题。在这种情况下,可以考虑加锁。
  • 结果合并:请确保你在合并结果时不会出现竞争条件。
  • 性能监控:测试新的实现是否确实提高了性能,尤其是在高负载情况下。

此代码利用 Goroutines 和 WaitGroups 来实现并发处理,但仍保持原有逻辑的完整性。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=22670

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?