Skip to content

Commit

Permalink
Make sure old cached are cleaned up on daily maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
bassages committed Mar 30, 2024
1 parent 016f983 commit 1f11b55
Show file tree
Hide file tree
Showing 34 changed files with 285 additions and 163 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ compileJava {

compileTestJava {
options.encoding = 'UTF-8'
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" << "-Werror"
}

if (System.getenv("GITHUB_TAG") != null) {
Expand Down
10 changes: 5 additions & 5 deletions etc/properties/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ logging.level.nl.homeserver.energy=WARN
logging.level.nl.homeserver.energy.MeterstandHousekeeping=INFO
logging.level.nl.homeserver.energy.OpgenomenVermogenHousekeeping=INFO

logging.level.nl.homeserver.climate.KlimaatCacheWarmer=INFO
logging.level.nl.homeserver.energy.meterreading.MeterstandCacheWarmer=INFO
logging.level.nl.homeserver.energy.opgenomenvermogen.OpgenomenVermogenCacheWarmer=INFO
logging.level.nl.homeserver.energy.standbypower.StandbyPowerCacheWarmer=INFO
logging.level.nl.homeserver.energy.verbruikkosten.VerbruikKostenCacheWarmer=INFO
logging.level.nl.homeserver.climate.KlimaatCacheMaintainer=INFO
logging.level.nl.homeserver.energy.meterreading.MeterstandCacheMaintainer=INFO
logging.level.nl.homeserver.energy.opgenomenvermogen.OpgenomenVermogenCacheMaintainer=INFO
logging.level.nl.homeserver.energy.standbypower.StandbyPowerCacheMaintainer=INFO
logging.level.nl.homeserver.energy.verbruikkosten.VerbruikKostenCacheMaintainer=INFO

logging.level.org.springframework.web.socket.config=WARN

Expand Down
23 changes: 15 additions & 8 deletions src/main/java/nl/homeserver/CachingConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@
@EnableCaching
public class CachingConfiguration {

public static final String CACHE_NAME_AVERAGE_CLIMATE_IN_MONTH = "averageClimateInMonth";
public static final String CACHE_NAME_CLIMATE_IN_PERIOD = "climateInPeriod";
public static final String CACHE_NAME_MEEST_RECENTE_METERSTAND_OP_DAG = "meestRecenteMeterstandOpDag";
public static final String CACHE_NAME_STANDBY_POWER = "standbyPower";
public static final String CACHE_NAME_GAS_VERBRUIK_IN_PERIODE = "gasVerbruikInPeriode";
public static final String CACHE_NAME_STROOM_VERBRUIK_IN_PERIODE = "stroomVerbruikInPeriode";
public static final String CACHE_NAME_OPGENOMEN_VERMOGEN_HISTORY = "opgenomenVermogenHistory";

private static final Map<String, Integer> CACHES = Map.of(
"energyContractsInPeriod", 20,
"gasVerbruikInPeriode", 100,
"stroomVerbruikInPeriode", 100,
"energieVerbruikInPeriode",100,
"opgenomenVermogenHistory", 100,
"meestRecenteMeterstandOpDag",100,
"klimaatInPeriod", 100,
"averageKlimaatInMonth", 48,
"standbyPower", 48
CACHE_NAME_GAS_VERBRUIK_IN_PERIODE, 100,
CACHE_NAME_STROOM_VERBRUIK_IN_PERIODE, 100,
CACHE_NAME_OPGENOMEN_VERMOGEN_HISTORY, 100,
CACHE_NAME_MEEST_RECENTE_METERSTAND_OP_DAG,100,
CACHE_NAME_CLIMATE_IN_PERIOD, 100,
CACHE_NAME_AVERAGE_CLIMATE_IN_MONTH, 48,
CACHE_NAME_STANDBY_POWER, 48
);

// IntelliJ: "Method '...' is never used"
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/nl/homeserver/cache/DailyCacheMaintainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nl.homeserver.cache;

public interface DailyCacheMaintainer {

void maintainCacheDaily();
}
32 changes: 32 additions & 0 deletions src/main/java/nl/homeserver/cache/DailyCacheMaintenance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package nl.homeserver.cache;

import lombok.extern.slf4j.Slf4j;
import nl.homeserver.housekeeping.HousekeepingSchedule;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
class DailyCacheMaintenance {

private final List<DailyCacheMaintainer> dailyCacheMaintainers;

@Value("${home-server.cache.maintenance.daily}")
private boolean isDailyCacheMaintenanceEnabled;

DailyCacheMaintenance(final List<DailyCacheMaintainer> dailyCacheMaintainers) {
this.dailyCacheMaintainers = dailyCacheMaintainers;
}

@Scheduled(cron = HousekeepingSchedule.WARMUP_CACHE)
public void considerDailyWarmup() {
if (isDailyCacheMaintenanceEnabled) {
log.info("Maintaining cache start");
dailyCacheMaintainers.forEach(DailyCacheMaintainer::maintainCacheDaily);
log.info("Maintaining cache completed");
}
}
}
6 changes: 0 additions & 6 deletions src/main/java/nl/homeserver/cache/DailyCacheWarmer.java

This file was deleted.

32 changes: 0 additions & 32 deletions src/main/java/nl/homeserver/cache/WarmupCacheDaily.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import nl.homeserver.cache.DailyCacheWarmer;
import nl.homeserver.cache.DailyCacheMaintainer;
import nl.homeserver.cache.StartupCacheWarmer;
import org.springframework.stereotype.Component;

import javax.cache.CacheManager;
import java.time.Clock;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Stream;

import static java.time.LocalDate.now;
import static nl.homeserver.CachingConfiguration.CACHE_NAME_AVERAGE_CLIMATE_IN_MONTH;
import static nl.homeserver.CachingConfiguration.CACHE_NAME_CLIMATE_IN_PERIOD;
import static nl.homeserver.DatePeriod.aPeriodWithToDate;

@Slf4j
@Component
@RequiredArgsConstructor
class KlimaatCacheWarmer implements StartupCacheWarmer, DailyCacheWarmer {
class KlimaatCacheMaintainer implements StartupCacheWarmer, DailyCacheMaintainer {

private final KlimaatController klimaatController;
private final CacheManager cacheManager;
private final Clock clock;

@Override
Expand All @@ -44,7 +48,10 @@ public void warmupCacheOnStartup() {
}

@Override
public void warmupCacheDaily() {
public void maintainCacheDaily() {
cacheManager.getCache(CACHE_NAME_AVERAGE_CLIMATE_IN_MONTH).clear();
cacheManager.getCache(CACHE_NAME_CLIMATE_IN_PERIOD).clear();

final LocalDate today = now(clock);
final LocalDate yesterday = today.minusDays(1);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/nl/homeserver/climate/KlimaatController.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public KlimaatSensor update(@PathVariable("sensorCode") final String sensorCode,
@DeleteMapping("sensors/{sensorCode}")
public void delete(@PathVariable("sensorCode") final String sensorCode) {
final KlimaatSensor existingKlimaatSensor = getKlimaatSensorExpectingOne(sensorCode);
klimaatService.deleteByKlimaatSensor(existingKlimaatSensor);
klimaatService.deleteByClimateSensor(existingKlimaatSensor);
klimaatSensorService.delete(existingKlimaatSensor);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/nl/homeserver/climate/KlimaatRepos.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,5 @@ BigDecimal getAverageLuchtvochtigheid(@Param("sensorCode") String sensorCode,
@Param("van") LocalDateTime van,
@Param("tot") LocalDateTime tot);

void deleteByKlimaatSensorCode(String klimaatSensorCode);
void deleteByKlimaatSensorCode(String climateSensorCode);
}
2 changes: 1 addition & 1 deletion src/main/java/nl/homeserver/climate/KlimaatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private List<Klimaat> getHighestHumidity(final String sensorCode, final DatePeri
.toList();
}

void deleteByKlimaatSensor(final KlimaatSensor klimaatSensor) {
void deleteByClimateSensor(final KlimaatSensor klimaatSensor) {
this.klimaatRepository.deleteByKlimaatSensorCode(klimaatSensor.getCode());
}
}
8 changes: 4 additions & 4 deletions src/main/java/nl/homeserver/climate/KlimaatServiceHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
import java.time.YearMonth;
import java.util.List;

import static nl.homeserver.CachingConfiguration.CACHE_NAME_AVERAGE_CLIMATE_IN_MONTH;
import static nl.homeserver.CachingConfiguration.CACHE_NAME_CLIMATE_IN_PERIOD;
import static nl.homeserver.DatePeriod.aPeriodWithToDate;

@Service
@RequiredArgsConstructor
public class KlimaatServiceHelper {
private static final String CACHE_NAME_AVERAGE_KLIMAAT_IN_MONTH = "averageKlimaatInMonth";

private final KlimaatRepos klimaatRepository;

@Cacheable(cacheNames = CACHE_NAME_AVERAGE_KLIMAAT_IN_MONTH)
@Cacheable(cacheNames = CACHE_NAME_AVERAGE_CLIMATE_IN_MONTH)
public GemiddeldeKlimaatPerMaand getPotentiallyCachedAverageInMonthOfYear(final String sensorCode,
final SensorType sensorType,
final YearMonth yearMonth) {
Expand All @@ -46,7 +46,7 @@ private BigDecimal getAverage(final String sensorCode, final SensorType sensorTy
};
}

@Cacheable(cacheNames = "klimaatInPeriod")
@Cacheable(cacheNames = CACHE_NAME_CLIMATE_IN_PERIOD)
public List<Klimaat> getPotentiallyCachedAllInPeriod(final String klimaatSensorCode, final DatePeriod period) {
return getNotCachedAllInPeriod(klimaatSensorCode, period);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import nl.homeserver.cache.DailyCacheWarmer;
import nl.homeserver.cache.DailyCacheMaintainer;
import nl.homeserver.cache.StartupCacheWarmer;
import org.springframework.stereotype.Component;

import javax.cache.CacheManager;
import java.time.Clock;
import java.time.LocalDate;
import java.time.Month;
Expand All @@ -15,14 +16,16 @@
import static java.time.temporal.TemporalAdjusters.lastDayOfMonth;
import static java.util.Comparator.reverseOrder;
import static java.util.stream.LongStream.rangeClosed;
import static nl.homeserver.CachingConfiguration.CACHE_NAME_MEEST_RECENTE_METERSTAND_OP_DAG;

@Slf4j
@Component
@RequiredArgsConstructor
class MeterstandCacheWarmer implements StartupCacheWarmer, DailyCacheWarmer {
class MeterstandCacheMaintainer implements StartupCacheWarmer, DailyCacheMaintainer {
private static final Month[] MONTHS = Month.values();

private final MeterstandController meterstandController;
private final CacheManager cacheManager;
private final Clock clock;

@Override
Expand All @@ -39,7 +42,9 @@ public void warmupCacheOnStartup() {
}

@Override
public void warmupCacheDaily() {
public void maintainCacheDaily() {
cacheManager.getCache(CACHE_NAME_MEEST_RECENTE_METERSTAND_OP_DAG).clear();

final LocalDate today = now(clock);
final LocalDate yesterday = today.minusDays(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import nl.homeserver.cache.CacheService;
import nl.homeserver.energy.verbruikkosten.VerbruikKostenOverzichtService;
import nl.homeserver.housekeeping.HousekeepingSchedule;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
Expand All @@ -17,6 +16,8 @@

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.groupingBy;
import static nl.homeserver.CachingConfiguration.CACHE_NAME_GAS_VERBRUIK_IN_PERIODE;
import static nl.homeserver.CachingConfiguration.CACHE_NAME_STROOM_VERBRUIK_IN_PERIODE;

@Slf4j
@Service
Expand Down Expand Up @@ -63,8 +64,8 @@ private void cleanup(final LocalDate day) {
}

private void clearCachesThatUsesPossibleDeletedMeterstanden() {
cacheService.clear(VerbruikKostenOverzichtService.CACHE_NAME_GAS_VERBRUIK_IN_PERIODE);
cacheService.clear(VerbruikKostenOverzichtService.CACHE_NAME_STROOM_VERBRUIK_IN_PERIODE);
cacheService.clear(CACHE_NAME_GAS_VERBRUIK_IN_PERIODE);
cacheService.clear(CACHE_NAME_STROOM_VERBRUIK_IN_PERIODE);
}

private void cleanupMeterStandenInOneHour(final int hour, final List<Meterstand> meterstandenInOneHour) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
import java.time.LocalDateTime;
import java.util.Optional;

import static nl.homeserver.CachingConfiguration.CACHE_NAME_MEEST_RECENTE_METERSTAND_OP_DAG;

@RequiredArgsConstructor
@Service
public class MostResentMeterstandOpDagService {

private static final String CACHE_NAME_MEEST_RECENTE_METERSTAND_OP_DAG = "meestRecenteMeterstandOpDag";

private final MeterstandRepository meterstandRepository;

@Cacheable(cacheNames = CACHE_NAME_MEEST_RECENTE_METERSTAND_OP_DAG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import nl.homeserver.cache.DailyCacheWarmer;
import nl.homeserver.cache.DailyCacheMaintainer;
import nl.homeserver.cache.StartupCacheWarmer;
import org.springframework.stereotype.Component;

import javax.cache.CacheManager;
import java.time.Clock;
import java.time.LocalDate;

import static java.time.LocalDate.now;
import static java.util.concurrent.TimeUnit.MINUTES;
import static nl.homeserver.CachingConfiguration.CACHE_NAME_OPGENOMEN_VERMOGEN_HISTORY;
import static nl.homeserver.DatePeriod.aPeriodWithToDate;

@Slf4j
@Component
@RequiredArgsConstructor
class OpgenomenVermogenCacheWarmer implements StartupCacheWarmer, DailyCacheWarmer {
class OpgenomenVermogenCacheMaintainer implements StartupCacheWarmer, DailyCacheMaintainer {
private static final long DEFAULT_SUB_PERIOD_LENGTH_IN_SECONDS = MINUTES.toMillis(3);

private final OpgenomenVermogenController opgenomenVermogenController;
private final CacheManager cacheManager;
private final Clock clock;

@Override
Expand All @@ -31,8 +34,10 @@ public void warmupCacheOnStartup() {
}

@Override
public void warmupCacheDaily() {
log.info("Warmup of cache opgenomenVermogenHistory");
public void maintainCacheDaily() {
log.info("Maintenance of cache %s".formatted(CACHE_NAME_OPGENOMEN_VERMOGEN_HISTORY));
cacheManager.getCache(CACHE_NAME_OPGENOMEN_VERMOGEN_HISTORY).clear();

final LocalDate yesterday = now(clock).minusDays(1);
warmupCacheForDay(yesterday);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.groupingBy;
import static nl.homeserver.CachingConfiguration.CACHE_NAME_OPGENOMEN_VERMOGEN_HISTORY;

@Slf4j
@Service
Expand All @@ -36,7 +37,7 @@ public class OpgenomenVermogenHousekeeping {
public void start() {
log.info("Start housekeeping of OpgenomenVermogen");
findDaysToCleanup().forEach(this::cleanup);
cacheService.clear(OpgenomenVermogenService.CACHE_NAME_OPGENOMEN_VERMOGEN_HISTORY);
cacheService.clear(CACHE_NAME_OPGENOMEN_VERMOGEN_HISTORY);
log.info("Finished housekeeping of OpgenomenVermogen");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@

import static java.time.LocalDateTime.from;
import static java.util.Comparator.comparingInt;
import static nl.homeserver.CachingConfiguration.CACHE_NAME_OPGENOMEN_VERMOGEN_HISTORY;
import static nl.homeserver.DateTimePeriod.aPeriodWithToDateTime;
import static nl.homeserver.DateTimeUtil.toMillisSinceEpoch;

@Service
@RequiredArgsConstructor
public class OpgenomenVermogenService {

static final String CACHE_NAME_OPGENOMEN_VERMOGEN_HISTORY = "opgenomenVermogenHistory";

static final String TOPIC = "/topic/opgenomen-vermogen";

private final OpgenomenVermogenRepository opgenomenVermogenRepository;
Expand Down
Loading

0 comments on commit 1f11b55

Please sign in to comment.