Browse Source

caching

master
gsd 3 months ago
parent
commit
8db8aa3e3b
  1. 5
      pom.xml
  2. 16
      src/main/java/app/configurations/CacheConfig.java
  3. 32
      src/main/java/app/services/steam/SteamWebApi.java

5
pom.xml

@ -27,6 +27,11 @@
<artifactId>spring-boot-starter</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>

16
src/main/java/app/configurations/CacheConfig.java

@ -0,0 +1,16 @@
package app.configurations;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("steam_data", "steam_search_data");
}
}

32
src/main/java/app/services/steam/SteamWebApi.java

@ -4,15 +4,22 @@ import app.entities.other.SteamID;
import app.entities.steam.SteamData;
import app.utils.SteamIDConverter;
import app.utils.SteamUrlConverter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
@Service
public class SteamWebApi {
private RestTemplate restTemplate;
@ -25,17 +32,30 @@ public class SteamWebApi {
restTemplate = new RestTemplate();
}
public SteamData getSteamData(long steam64){
String url = String.format("https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s", webapi_key, steam64);
@Scheduled(cron = "0 */30 * * * *")
@Caching(evict = {
@CacheEvict(value = "steam_data", allEntries = true),
@CacheEvict(value = "steam_search_data", allEntries = true)
})
public void clearCache() {}
public SteamData getSteamData(long steam64) {
try {
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);
} catch (Exception err){
return this.getSteamDataCaching(steam64);
} catch (Exception e) {
return null;
}
}
@Cacheable(value = "steam_data", key = "#steam64")
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);
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);
}

Loading…
Cancel
Save