added cache for meta-stats

This commit is contained in:
2021-10-08 18:29:15 +02:00
parent 1816c5a2b5
commit cc48365eea
5 changed files with 320 additions and 35 deletions

73
main.go
View File

@@ -11,6 +11,8 @@ import (
"csgowtfd/utils"
"flag"
"fmt"
"github.com/go-redis/cache/v8"
"github.com/go-redis/redis/v8"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
_ "github.com/mattn/go-sqlite3"
@@ -33,6 +35,8 @@ var (
demoLoader = &csgo.DemoMatchLoader{}
router *mux.Router
db *utils.DBWithLock
rdb *redis.Client
rdc *cache.Cache
sendGC chan *csgo.Demo
demoParser = &csgo.DemoParser{}
firstHK = true
@@ -43,18 +47,14 @@ var (
)
type PlayerResponse struct {
SteamID64 uint64 `json:"steamid64,string"`
Name string `json:"name"`
Avatar string `json:"avatar"`
VAC bool `json:"vac"`
Tracked bool `json:"tracked"`
VanityURL string `json:"vanity_url,omitempty"`
MatchStats struct {
Win int `json:"win,omitempty"`
Tie int `json:"tie,omitempty"`
Loss int `json:"loss,omitempty"`
} `json:"match_stats,omitempty"`
Matches []*MatchResponse `json:"matches,omitempty"`
SteamID64 uint64 `json:"steamid64,string"`
Name string `json:"name"`
Avatar string `json:"avatar"`
VAC bool `json:"vac"`
Tracked bool `json:"tracked"`
VanityURL string `json:"vanity_url,omitempty"`
MatchStats utils.MatchStats `json:"match_stats,omitempty"`
Matches []*MatchResponse `json:"matches,omitempty"`
}
type MatchResponse struct {
@@ -172,16 +172,38 @@ func getPlayer(w http.ResponseWriter, r *http.Request) {
}
}
wins, ties, losses, err := utils.GetMatchStats(db.Client, db.Lock, tPlayer)
metaStats := new(utils.MatchStats)
err = rdc.Get(context.Background(), fmt.Sprintf("csgowtfd_meta_%d", tPlayer.ID), &metaStats)
if err != nil {
log.Errorf("[GP] Error retrieving match-stats for player %s: %v", id, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
wins, ties, losses, err := utils.GetMatchStats(db.Client, db.Lock, tPlayer)
if err != nil {
log.Errorf("[GP] Error retrieving match-stats for player %s: %v", id, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
response.MatchStats.Win = wins
response.MatchStats.Tie = ties
response.MatchStats.Loss = losses
response.MatchStats.Win = wins
response.MatchStats.Tie = ties
response.MatchStats.Loss = losses
err = rdc.Set(&cache.Item{
Ctx: context.Background(),
Key: fmt.Sprintf("csgowtfd_meta_%d", tPlayer.ID),
Value: response.MatchStats,
TTL: time.Hour * 12,
})
if err != nil {
log.Errorf("[GP] Failure saving to cache: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
log.Debugf("[GP] Metastats for %d saved to cache", tPlayer.ID)
} else {
log.Debugf("[GP] Metastats for %d from redis", tPlayer.ID)
response.MatchStats.Win = metaStats.Win
response.MatchStats.Tie = metaStats.Tie
response.MatchStats.Loss = metaStats.Loss
}
for _, iMatch := range tMatches {
mResponse := &MatchResponse{
@@ -410,6 +432,17 @@ func main() {
log.Panicf("Automigrate failed: %v", err)
}
rdb = redis.NewClient(&redis.Options{
Addr: conf.Redis.Address,
Password: conf.Redis.Password,
DB: 0,
})
rdc = cache.New(&cache.Options{
Redis: rdb,
LocalCache: cache.NewTinyLFU(1000, time.Minute),
})
rL = ratelimit.New(conf.Steam.RatePerSecond)
// setup GC
err = demoLoader.Setup(conf.Steam.Username, conf.Steam.Password, *authCodeFlag, conf.Steam.Sentry, conf.Steam.LoginKey, conf.Steam.ServerList)