Fix cache service and clear cache methods

This commit is contained in:
Konstantinos Triantafyllou 2024-06-19 14:26:35 +03:00
parent 3ce205e6cc
commit 8e2dd89950
2 changed files with 21 additions and 6 deletions

View File

@ -4,6 +4,8 @@ import eu.dnetlib.irishmonitorservice.entities.Data;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@ -11,13 +13,21 @@ import org.springframework.stereotype.Service;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
@Service
public class CacheService {
private final Logger logger = LogManager.getLogger(CacheService.class);
private final StatsToolService statsToolService;
private final CacheManager cacheManager;
@Autowired
private StatsToolService statsToolService;
public CacheService(StatsToolService statsToolService, CacheManager cacheManager) throws UnsupportedEncodingException {
this.statsToolService = statsToolService;
this.cacheManager = cacheManager;
this.clearCache();
}
public List<List<List<String>>> getResponse(String type) throws UnsupportedEncodingException {
switch (type) {
@ -32,9 +42,12 @@ public class CacheService {
}
}
@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 {
public void clearCache() throws UnsupportedEncodingException {
this.cacheManager.getCacheNames().stream()
.map(this.cacheManager::getCache)
.filter(Objects::nonNull)
.forEach(Cache::clear);
logger.info("Cache cleared at " + LocalDateTime.now());
this.statsToolService.getFunders();
this.statsToolService.getOrganizations();

View File

@ -3,6 +3,8 @@ package eu.dnetlib.irishmonitorservice.services;
import eu.dnetlib.irishmonitorservice.configuration.properties.Indicators;
import eu.dnetlib.irishmonitorservice.configuration.properties.StatsToolProperties;
import eu.dnetlib.irishmonitorservice.entities.Data;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
@ -20,6 +22,7 @@ import java.util.List;
@Service
public class StatsToolService {
private final Logger logger = LogManager.getLogger(StatsToolService.class);
@Autowired
private StatsToolProperties properties;
@ -43,14 +46,13 @@ public class StatsToolService {
}
public List<List<List<String>>> getData(Indicators indicators) throws UnsupportedEncodingException {
List<List<List<String>>> data = new ArrayList<>();
if(indicators != null) {
List<List<List<String>>> data = new ArrayList<>();
data.add(this.getData(properties.getHost(), indicators.getPublications()));
data.add(this.getData(properties.getHost(), indicators.getPublicationsPR()));
data.add(this.getData(properties.getHost(), indicators.getPublicationsPROA()));
return data;
}
return null;
return data;
}
private List<List<String>> getData(String service, String json) throws UnsupportedEncodingException {