added caching to chat translation
This commit is contained in:
20
main.go
20
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,6 +685,9 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
|
||||
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())
|
||||
if err != nil {
|
||||
log.Infof("[GMC] match %d not found: %+v", matchId, err)
|
||||
@@ -691,8 +695,6 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
resp := map[string][]*utils.ChatResponse{}
|
||||
|
||||
for _, stat := range tStats {
|
||||
steamid := strconv.FormatUint(stat.Edges.MatchPlayer.PlayerStats, 10)
|
||||
if _, ok := resp[steamid]; !ok {
|
||||
@@ -700,7 +702,6 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -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)
|
||||
if err != nil {
|
||||
log.Errorf("[GMC] JSON: %+v", err)
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user