[master]: cache response from stats tool after clearing the cache.

This commit is contained in:
Konstantinos Triantafyllou 2024-04-12 13:10:55 +03:00
parent d25c54ce5a
commit 3176a0448d
4 changed files with 55 additions and 34 deletions

View File

@ -1,46 +1,37 @@
package eu.dnetlib.irishmonitorservice.controllers;
import eu.dnetlib.irishmonitorservice.configuration.properties.StatsToolProperties;
import eu.dnetlib.irishmonitorservice.entities.StakeholderPublications;
import eu.dnetlib.irishmonitorservice.utils.StatsToolUtils;
import eu.dnetlib.irishmonitorservice.services.CacheService;
import eu.dnetlib.irishmonitorservice.services.StatsToolService;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.service.StakeholderService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("publications")
public class StakeholderPublicationsController {
private final StakeholderService service;
private final CacheService cacheService;
@Autowired
private StakeholderService service;
@Autowired
private StatsToolUtils utils;
public List<List<String>> getResponse(String type) throws UnsupportedEncodingException {
if(type.equals("funder")) {
return this.utils.getFunders();
} else if(type.equals("organization")) {
return this.utils.getOrganizations();
} else if(type.equals("datasource")) {
return this.utils.getDataSources();
} else {
return null;
}
public StakeholderPublicationsController(StakeholderService service, CacheService cacheService) {
this.service = service;
this.cacheService = cacheService;
}
@RequestMapping(value = "/{type}", method = RequestMethod.GET)
public List<StakeholderPublications> getStakeholders(@PathVariable String type) throws UnsupportedEncodingException, InterruptedException {
List<Stakeholder> stakeholders = this.service.getStakeholdersByTypeAndRole(type, null, false);
List<StakeholderPublications> stakeholderPublications = new ArrayList<>();
List<List<String>> data = this.getResponse(type);
List<List<String>> data = this.cacheService.getResponse(type);
List<Stakeholder> remain = new ArrayList<>(stakeholders);
if (data != null) {
data.forEach(entity -> {

View File

@ -1,4 +1,4 @@
package eu.dnetlib.irishmonitorservice.utils;
package eu.dnetlib.irishmonitorservice.entities;
import java.util.List;
import java.util.Map;

View File

@ -0,0 +1,41 @@
package eu.dnetlib.irishmonitorservice.services;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class CacheService {
private final Logger logger = LogManager.getLogger(CacheService.class);
@Autowired
private StatsToolService statsToolService;
public List<List<String>> getResponse(String type) throws UnsupportedEncodingException {
if (type.equals("funder")) {
return this.statsToolService.getFunders();
} else if (type.equals("organization")) {
return this.statsToolService.getOrganizations();
} else if (type.equals("datasource")) {
return this.statsToolService.getDataSources();
} else {
return null;
}
}
@CacheEvict(allEntries = true, value = {"funders", "organizations", "datasources"}, beforeInvocation = true)
@Scheduled(cron = "0 0 0 * * *") // Reset cache every day at 00:00
public void clearCacheAtMidnight() throws UnsupportedEncodingException {
logger.info("Cache cleared at " + LocalDateTime.now());
this.statsToolService.getFunders();
this.statsToolService.getOrganizations();
this.statsToolService.getDataSources();
}
}

View File

@ -1,14 +1,11 @@
package eu.dnetlib.irishmonitorservice.utils;
package eu.dnetlib.irishmonitorservice.services;
import eu.dnetlib.irishmonitorservice.configuration.properties.StatsToolProperties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import eu.dnetlib.irishmonitorservice.entities.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
@ -17,12 +14,10 @@ import org.springframework.web.util.UriComponentsBuilder;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class StatsToolUtils {
private final Logger logger = LogManager.getLogger(StatsToolUtils.class);
public class StatsToolService {
@Autowired
private StatsToolProperties properties;
@ -64,10 +59,4 @@ public class StatsToolUtils {
}
return null;
}
@CacheEvict(allEntries = true, value = {"funders", "organizations", "datasources"})
@Scheduled(cron = "0 0 0 * * *") // Reset cache every day at 00:00
public void clearCacheAtMidnight() {
logger.info("Cache cleared at " + LocalDateTime.now());
}
}