From 1aa7cc260abac87d4a8e479c290a3c8554bf43b5 Mon Sep 17 00:00:00 2001 From: Giovanni Harting <539@idlegandalf.com> Date: Tue, 1 Feb 2022 19:23:41 +0100 Subject: [PATCH] update steam profiles only once a day --- main.go | 38 ++++++++++++++++++++++++++------------ utils/utils.go | 3 ++- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index a42c999..cdf030b 100644 --- a/main.go +++ b/main.go @@ -61,19 +61,33 @@ func housekeeping() { } // update players from steam - tPlayerNeedSteamUpdate, err := db.Player.Query().Where( - player.SteamUpdatedLTE(time.Now().UTC().Add(dur * -1)), - ).All(context.Background()) - if err != nil { - log.Errorf("[HK] Can't query players: %v", err) - continue - } - - if len(tPlayerNeedSteamUpdate) > 0 { - log.Infof("[HK] Refreshing %d profiles from steam", len(tPlayerNeedSteamUpdate)) - _, err = utils.PlayerFromSteam(tPlayerNeedSteamUpdate, db, conf.Steam.APIKey, rL) + var lastUpdated time.Time + err = rdc.Get(context.Background(), utils.CachePrefix+"last_updated_profiles", lastUpdated) + if err != nil || time.Since(lastUpdated) >= (time.Hour*24) { + tPlayerNeedSteamUpdate, err := db.Player.Query().Where( + player.SteamUpdatedLTE(time.Now().UTC().Add(dur * -1)), + ).All(context.Background()) if err != nil { - log.Warningf("[HK] Unable to update profiles from steam: %v", err) + log.Errorf("[HK] Can't query players: %v", err) + continue + } + + if len(tPlayerNeedSteamUpdate) > 0 { + log.Infof("[HK] Updating %d profiles from steam", len(tPlayerNeedSteamUpdate)) + _, err = utils.PlayerFromSteam(tPlayerNeedSteamUpdate, db, conf.Steam.APIKey, rL) + if err != nil { + log.Warningf("[HK] Unable to update profiles from steam: %v", err) + } + } + err = rdc.Set(&cache.Item{ + Ctx: context.Background(), + Key: utils.CachePrefix + "last_updated_profiles", + Value: time.Now().UTC(), + TTL: time.Hour * 24 * 30, + SkipLocalCache: true, + }) + if err != nil { + log.Errorf("[HK] Failure setting cache: %v", err) } } diff --git a/utils/utils.go b/utils/utils.go index 2d4ab12..992f01b 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -209,7 +209,8 @@ type ( const ( shareCodeURLEntry = "https://api.steampowered.com/ICSGOPlayers_730/GetNextMatchSharingCode/v1?key=%s&steamid=%d&steamidkey=%s&knowncode=%s" - SideMetaCacheKey = "csgowtfd_side_meta_%d" + CachePrefix = "csgowtfd_" + SideMetaCacheKey = CachePrefix + "side_meta_%d" ) //goland:noinspection SpellCheckingInspection