Browse Source

steamdata cache

master
gsd 6 days ago
parent
commit
da7aa3f27b
  1. 11
      src/main/java/app/entities/other/SteamID.java
  2. 5
      src/main/java/app/entities/steam/SteamData.java
  3. 42
      src/main/java/app/services/steam/SteamWebApi.java

11
src/main/java/app/entities/other/SteamID.java

@ -1,9 +1,12 @@
package app.entities.other; package app.entities.other;
import app.exceptions.steam.InvalidSteamID; import app.exceptions.steam.InvalidSteamID;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import java.time.Instant;
@JsonSerialize @JsonSerialize
public class SteamID { public class SteamID {
/** /**
@ -23,6 +26,8 @@ public class SteamID {
public String community_url; public String community_url;
public long account_id; public long account_id;
@JsonIgnore
private long time;
public SteamID(){ public SteamID(){
} }
@ -33,6 +38,7 @@ public class SteamID {
this.steam64 = Long.valueOf(steam64); this.steam64 = Long.valueOf(steam64);
this.community_url = String.format("https://steamcommunity.com/profiles/%s", steam64); this.community_url = String.format("https://steamcommunity.com/profiles/%s", steam64);
this.account_id = account_id; this.account_id = account_id;
this.time = Instant.now().getEpochSecond();
} }
@Override @Override
@ -48,4 +54,9 @@ public class SteamID {
throw new InvalidSteamID(); throw new InvalidSteamID();
} }
} }
@JsonIgnore
public long getTime() {
return time;
}
} }

5
src/main/java/app/entities/steam/SteamData.java

@ -3,13 +3,16 @@ package app.entities.steam;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data; import lombok.Data;
import java.time.Instant;
@Data @Data
public class SteamData { public class SteamData {
String avatar; String avatar;
String nickname; String nickname;
double time; long time;
public SteamData(JsonNode player){ public SteamData(JsonNode player){
time = Instant.now().getEpochSecond();
nickname = player.get("personaname").asText(); nickname = player.get("personaname").asText();
if (player.has("avatarfull")) avatar = player.get("avatarfull").asText(); if (player.has("avatarfull")) avatar = player.get("avatarfull").asText();
else avatar = player.get("avatar").asText(); else avatar = player.get("avatar").asText();

42
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.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.time.Instant;
import java.util.HashMap; import java.util.HashMap;
@Service @Service
@ -32,31 +33,44 @@ public class SteamWebApi {
restTemplate = new RestTemplate(); restTemplate = new RestTemplate();
} }
@Scheduled(cron = "0 */30 * * * *") //todo mb store in db
@Caching(evict = { private final HashMap<Long, SteamData> steamDataCache = new HashMap<>();
@CacheEvict(value = "steam_data", allEntries = true), private final HashMap<String, SteamID> steamIdCache = new HashMap<>();
@CacheEvict(value = "steam_search_data", allEntries = true) private static final int cache_lifetime = 60 * 60 * 24;
})
public void clearCache() {}
//mojno bilo sdelat 4erez spring cache no ne hochu
public SteamData getSteamData(long steam64) { public SteamData getSteamData(long steam64) {
try { if (steamDataCache.containsKey(steam64) && (Instant.now().getEpochSecond() - steamDataCache.get(steam64).getTime() < cache_lifetime)) {
return this.getSteamDataCaching(steam64); return steamDataCache.get(steam64);
} catch (Exception e) { } else {
return null; 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") private SteamData getSteamDataFromSteam(long steam64) throws Exception {
public SteamData getSteamDataCaching(long steam64) throws Exception {
String url = String.format("https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s", webapi_key, steam64); 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); ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
JsonNode player = (new ObjectMapper()).readTree(response.getBody()).get("response").get("players").get(0); JsonNode player = (new ObjectMapper()).readTree(response.getBody()).get("response").get("players").get(0);
return new SteamData(player); return new SteamData(player);
} }
@Cacheable(value = "steam_search_data", key = "#community_url")
public SteamID getSteamID(String 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);
}
}
} }
} }

Loading…
Cancel
Save