From 1e2541c76bb52f85ed621361b5a1b8a1615c3384 Mon Sep 17 00:00:00 2001 From: Giovanni Harting <539@idlegandalf.com> Date: Sat, 12 Feb 2022 02:19:23 +0100 Subject: [PATCH] added caching to chat translation --- main.go | 82 +++++++++++++++++++++++++++++--------------------- utils/utils.go | 1 + 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/main.go b/main.go index 136ef88..a5e24ae 100644 --- a/main.go +++ b/main.go @@ -667,6 +667,7 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusBadRequest) return } + lang, _ := tag[0].Base() var translate bool if trans != "" { @@ -684,50 +685,63 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) { return } - tStats, err := db.Messages.Query().Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchId)))).WithMatchPlayer().All(context.Background()) - if err != nil { - log.Infof("[GMC] match %d not found: %+v", matchId, err) - w.WriteHeader(http.StatusNotFound) - return - } - resp := map[string][]*utils.ChatResponse{} - - for _, stat := range tStats { - steamid := strconv.FormatUint(stat.Edges.MatchPlayer.PlayerStats, 10) - if _, ok := resp[steamid]; !ok { - resp[steamid] = make([]*utils.ChatResponse, 0) + err = rdc.Get(context.Background(), fmt.Sprintf(utils.MatchChatCacheKey, matchId, lang), &resp) + if err != nil { + tStats, err := db.Messages.Query().Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchId)))).WithMatchPlayer().All(context.Background()) + if err != nil { + log.Infof("[GMC] match %d not found: %+v", matchId, err) + w.WriteHeader(http.StatusNotFound) + return } - if translate { - lang, _ := tag[0].Base() - translated, srcLang, err := utils.TranslateWithDeepL(stat.Message, lang.String(), - conf.DeepL.BaseURL, conf.DeepL.APIKey, conf.DeepL.Timeout) - if err != nil { - log.Warningf("[GMC] Unable to translate %s with DeepL: %v", stat.Message, err) - goto sendNormalResp + for _, stat := range tStats { + steamid := strconv.FormatUint(stat.Edges.MatchPlayer.PlayerStats, 10) + if _, ok := resp[steamid]; !ok { + resp[steamid] = make([]*utils.ChatResponse, 0) } - if srcLang == lang.String() { - goto sendNormalResp - } + if translate { + translated, srcLang, err := utils.TranslateWithDeepL(stat.Message, lang.String(), + conf.DeepL.BaseURL, conf.DeepL.APIKey, conf.DeepL.Timeout) + if err != nil { + log.Warningf("[GMC] Unable to translate %s with DeepL: %v", stat.Message, err) + goto sendNormalResp + } + if srcLang == lang.String() { + goto sendNormalResp + } + + resp[steamid] = append(resp[steamid], &utils.ChatResponse{ + Message: translated, + AllChat: stat.AllChat, + Tick: stat.Tick, + TranslatedFrom: srcLang, + TranslatedTo: lang.String(), + }) + + continue + } + sendNormalResp: resp[steamid] = append(resp[steamid], &utils.ChatResponse{ - Message: translated, - AllChat: stat.AllChat, - Tick: stat.Tick, - TranslatedFrom: srcLang, - TranslatedTo: lang.String(), + Message: stat.Message, + AllChat: stat.AllChat, + Tick: stat.Tick, }) - - continue } - sendNormalResp: - resp[steamid] = append(resp[steamid], &utils.ChatResponse{ - Message: stat.Message, - AllChat: stat.AllChat, - Tick: stat.Tick, + + err = rdc.Set(&cache.Item{ + Ctx: context.Background(), + Key: fmt.Sprintf(utils.MatchChatCacheKey, matchId, lang), + Value: resp, + TTL: time.Hour * 24 * 30, }) + if err != nil { + log.Errorf("[GMC] Failure saving to cache: %v", err) + w.WriteHeader(http.StatusInternalServerError) + return + } } err = utils.SendJSON(resp, w) diff --git a/utils/utils.go b/utils/utils.go index 4b2bf4d..029f5a0 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -233,6 +233,7 @@ const ( shareCodeURLEntry = "https://api.steampowered.com/ICSGOPlayers_730/GetNextMatchSharingCode/v1?key=%s&steamid=%d&steamidkey=%s&knowncode=%s" CachePrefix = "csgowtfd_" SideMetaCacheKey = CachePrefix + "side_meta_%d" + MatchChatCacheKey = CachePrefix + "chat_%d_%s" ) //goland:noinspection SpellCheckingInspection