diff --git a/src/main/java/app/entities/other/SteamID.java b/src/main/java/app/entities/other/SteamID.java index 8f58999..565aeda 100644 --- a/src/main/java/app/entities/other/SteamID.java +++ b/src/main/java/app/entities/other/SteamID.java @@ -1,9 +1,12 @@ package app.entities.other; import app.exceptions.steam.InvalidSteamID; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; +import java.time.Instant; + @JsonSerialize public class SteamID { /** @@ -23,6 +26,8 @@ public class SteamID { public String community_url; public long account_id; + @JsonIgnore + private long time; public SteamID(){ } @@ -33,6 +38,7 @@ public class SteamID { this.steam64 = Long.valueOf(steam64); this.community_url = String.format("https://steamcommunity.com/profiles/%s", steam64); this.account_id = account_id; + this.time = Instant.now().getEpochSecond(); } @Override @@ -48,4 +54,9 @@ public class SteamID { throw new InvalidSteamID(); } } + + @JsonIgnore + public long getTime() { + return time; + } } \ No newline at end of file diff --git a/src/main/java/app/entities/steam/SteamData.java b/src/main/java/app/entities/steam/SteamData.java index 522069f..ee0d120 100644 --- a/src/main/java/app/entities/steam/SteamData.java +++ b/src/main/java/app/entities/steam/SteamData.java @@ -3,13 +3,16 @@ package app.entities.steam; import com.fasterxml.jackson.databind.JsonNode; import lombok.Data; +import java.time.Instant; + @Data public class SteamData { String avatar; String nickname; - double time; + long time; public SteamData(JsonNode player){ + time = Instant.now().getEpochSecond(); nickname = player.get("personaname").asText(); if (player.has("avatarfull")) avatar = player.get("avatarfull").asText(); else avatar = player.get("avatar").asText(); diff --git a/src/main/java/app/services/steam/SteamWebApi.java b/src/main/java/app/services/steam/SteamWebApi.java index 822f023..ce3a173 100644 --- a/src/main/java/app/services/steam/SteamWebApi.java +++ b/src/main/java/app/services/steam/SteamWebApi.java @@ -18,6 +18,7 @@ import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; +import java.time.Instant; import java.util.HashMap; @Service @@ -32,31 +33,44 @@ public class SteamWebApi { restTemplate = new RestTemplate(); } - @Scheduled(cron = "0 */30 * * * *") - @Caching(evict = { - @CacheEvict(value = "steam_data", allEntries = true), - @CacheEvict(value = "steam_search_data", allEntries = true) - }) - public void clearCache() {} + //todo mb store in db + private final HashMap steamDataCache = new HashMap<>(); + private final HashMap steamIdCache = new HashMap<>(); + private static final int cache_lifetime = 60 * 60 * 24; + //mojno bilo sdelat 4erez spring cache no ne hochu public SteamData getSteamData(long steam64) { - try { - return this.getSteamDataCaching(steam64); - } catch (Exception e) { - return null; + if (steamDataCache.containsKey(steam64) && (Instant.now().getEpochSecond() - steamDataCache.get(steam64).getTime() < cache_lifetime)) { + return steamDataCache.get(steam64); + } else { + try { + SteamData result = this.getSteamDataFromSteam(steam64); + steamDataCache.put(steam64, result); + return result; + } catch (Exception e) { + return steamDataCache.getOrDefault(steam64, null); + } } } - @Cacheable(value = "steam_data", key = "#steam64") - public SteamData getSteamDataCaching(long steam64) throws Exception { + private SteamData getSteamDataFromSteam(long steam64) throws Exception { String url = String.format("https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s", webapi_key, steam64); ResponseEntity response = restTemplate.getForEntity(url, String.class); JsonNode player = (new ObjectMapper()).readTree(response.getBody()).get("response").get("players").get(0); return new SteamData(player); } - @Cacheable(value = "steam_search_data", key = "#community_url") public SteamID getSteamID(String community_url) { - return SteamUrlConverter.getSteamID(community_url, restTemplate); + if (steamIdCache.containsKey(community_url)) + return steamIdCache.get(community_url); + else { + try { + SteamID steamID = SteamUrlConverter.getSteamID(community_url, restTemplate); + steamIdCache.put(community_url, steamID); + return steamID; + } catch (Exception ignored) { + return steamIdCache.getOrDefault(community_url, null); + } + } } }