irish-monitor-service/src/main/java/eu/dnetlib/irishmonitorservice/services/CacheService.java

42 lines
1.5 KiB
Java

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();
}
}