added caching to chat translation

This commit is contained in:
2022-02-12 02:19:23 +01:00
parent 8c672f7186
commit 1e2541c76b
2 changed files with 49 additions and 34 deletions

20
main.go
View File

@@ -667,6 +667,7 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
lang, _ := tag[0].Base()
var translate bool var translate bool
if trans != "" { if trans != "" {
@@ -684,6 +685,9 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
return return
} }
resp := map[string][]*utils.ChatResponse{}
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()) tStats, err := db.Messages.Query().Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchId)))).WithMatchPlayer().All(context.Background())
if err != nil { if err != nil {
log.Infof("[GMC] match %d not found: %+v", matchId, err) log.Infof("[GMC] match %d not found: %+v", matchId, err)
@@ -691,8 +695,6 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
return return
} }
resp := map[string][]*utils.ChatResponse{}
for _, stat := range tStats { for _, stat := range tStats {
steamid := strconv.FormatUint(stat.Edges.MatchPlayer.PlayerStats, 10) steamid := strconv.FormatUint(stat.Edges.MatchPlayer.PlayerStats, 10)
if _, ok := resp[steamid]; !ok { if _, ok := resp[steamid]; !ok {
@@ -700,7 +702,6 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
} }
if translate { if translate {
lang, _ := tag[0].Base()
translated, srcLang, err := utils.TranslateWithDeepL(stat.Message, lang.String(), translated, srcLang, err := utils.TranslateWithDeepL(stat.Message, lang.String(),
conf.DeepL.BaseURL, conf.DeepL.APIKey, conf.DeepL.Timeout) conf.DeepL.BaseURL, conf.DeepL.APIKey, conf.DeepL.Timeout)
if err != nil { if err != nil {
@@ -730,6 +731,19 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
}) })
} }
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) err = utils.SendJSON(resp, w)
if err != nil { if err != nil {
log.Errorf("[GMC] JSON: %+v", err) log.Errorf("[GMC] JSON: %+v", err)

View File

@@ -233,6 +233,7 @@ const (
shareCodeURLEntry = "https://api.steampowered.com/ICSGOPlayers_730/GetNextMatchSharingCode/v1?key=%s&steamid=%d&steamidkey=%s&knowncode=%s" shareCodeURLEntry = "https://api.steampowered.com/ICSGOPlayers_730/GetNextMatchSharingCode/v1?key=%s&steamid=%d&steamidkey=%s&knowncode=%s"
CachePrefix = "csgowtfd_" CachePrefix = "csgowtfd_"
SideMetaCacheKey = CachePrefix + "side_meta_%d" SideMetaCacheKey = CachePrefix + "side_meta_%d"
MatchChatCacheKey = CachePrefix + "chat_%d_%s"
) )
//goland:noinspection SpellCheckingInspection //goland:noinspection SpellCheckingInspection