|
|
@ -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<Long, SteamData> steamDataCache = new HashMap<>(); |
|
|
|
private final HashMap<String, SteamID> 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<String> 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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|