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...

你可以使用 Goroutines 来并发处理一些独立的计算,从而优化这个函数。在 Go 中,协程非常轻量级,并且易于使用,可以有效地提高程序的性能。下面是对 GetUserHealthDiary 函数进行并发优化的示例:

  1. func GetUserHealthDiary(userId int) ai_common.UserHealthDiary {
  2. resp := ai_common.UserHealthDiary{}
  3. date := time.Now().Format("2006-01-02") // 日期格式应该是标准的
  4. dbData, isExist := dbrespository.GetUserHealthDiaryByUserId(userId, date)
  5. if !isExist {
  6. // create test data
  7. dbData.UserID = userId
  8. resp.RecordDate = dbData.RecordDate // 记录健康日记的日期,默认是当天
  9. dbrespository.CreateUserHealthDiary(&dbData)
  10. }
  11. resp.RecordDate = dbData.RecordDate
  12. var wg sync.WaitGroup
  13. wg.Add(8) // 根据需要并发的函数数量进行调整
  14. go func() {
  15. defer wg.Done()
  16. resp.HappinessIndex = getHappinessIndex(dbData.HappinessIndexID)
  17. dbData.HappinessIndexID = resp.HappinessIndex.ID
  18. }()
  19. go func() {
  20. defer wg.Done()
  21. resp.PsychologicalPressure = getPsychologicalPressure(dbData.PsychologicalPressureID)
  22. }()
  23. go func() {
  24. defer wg.Done()
  25. resp.WorkStudyIntensity = getWorkStudyIntensity(dbData.WorkStudyIntensityID)
  26. }()
  27. go func() {
  28. defer wg.Done()
  29. resp.OvertimeSituation = getOvertimeSituation(dbData.OvertimeSituationID)
  30. }()
  31. go func() {
  32. defer wg.Done()
  33. resp.ElectronicDeviceTime = getElectronicDeviceTime(dbData.ElectronicDeviceTimeID)
  34. }()
  35. go func() {
  36. defer wg.Done()
  37. resp.ExerciseIntensity = getExerciseIntensity(dbData.ExerciseIntensityID)
  38. }()
  39. go func() {
  40. defer wg.Done()
  41. resp.BadSocialEmotions = getBadSocialEmotions(dbData.BadSocialEmotionsID)
  42. dbData.BadSocialEmotionsID = resp.BadSocialEmotions.ID
  43. }()
  44. go func() {
  45. defer wg.Done()
  46. resp.SleepQuality = getSleepQuality(dbData.SleepQualityID)
  47. dbData.SleepQualityID = resp.SleepQuality.ID
  48. }()
  49. wg.Wait()
  50. for _, healthDiaryBadLiving := range dbData.HealthDiaryBadLiving {
  51. resp.BadLivingBehaviors = append(resp.BadLivingBehaviors, healthDiaryBadLiving.BadLiving)
  52. }
  53. for _, healthDiaryBadSleep := range dbData.HealthDiaryBadSleep {
  54. resp.BadSleep = append(resp.BadSleep, healthDiaryBadSleep.BadSleep)
  55. }
  56. for _, healthDiaryBadDining := range dbData.HealthDiaryBadDining {
  57. resp.BadDining = append(resp.BadDining, healthDiaryBadDining.BadDining)
  58. }
  59. return resp
  60. }

优化要点:

  1. Goroutines: 使用 Goroutines 并发处理每一个获取数据的函数。
  2. WaitGroup: 使用 sync.WaitGroup 来等待所有 Goroutines 完成。
  3. 避免重复代码: 对于重复的逻辑(例如更新 ID 的部分),如果有可能的话也考虑封装到函数中。

请根据你的具体需求和上下文适当调整上述示例,确保每个 Goroutine 的逻辑完整并正确处理共享资源的问题。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?