I have it currently set up to expire after 12 hours. However, it expires 12 hours after each cache is first written too. I would like it to refresh at 12am and 12pm only. Is this possible? In my cacheConfig file I have:
@Component
@EnableCaching
public class CacheConfig {
@Bean
public Caffeine defaultCacheConfig() {
return Caffeine.newBuilder()
.expireAfterWrite(12, TimeUnit.HOURS);
}
}
I am using Caffeine Cache library.
Caffeine supports variable expiration, where the duration for an entry has to be calculated independently. If you wanted to all entries expire at the same time you might write,
Caffeine.newBuilder()
.expireAfter(new Expiry<K, V>() {
public long expireAfterCreate(K key, V value, long currentTime) {
var toMidnight = Duration.between(LocalDate.now(),
LocalDate.now().plusDays(1).atStartOfDay());
var toNoon = Duration.between(LocalTime.now(), LocalTime.NOON);
return toNoon.isNegative() ? toMidnight.toNanos() : toNoon.toNanos();
}
public long expireAfterUpdate(K key, V value,
long currentTime, long currentDuration) {
return currentDuration;
}
public long expireAfterRead(K key, V value,
long currentTime, long currentDuration) {
return currentDuration;
}
}).build();
Using expiration might be overkill for such a simple task. Instead if you wanted to clear the cache then a scheduled task can do that instead, as @alexzander-zharkov suggested.
@Scheduled(cron = "0 0,12 * * *")
public void clear() {
cache.invalidateAll();
}
As this empties the cache there will be a performance penalty as the entries are loaded anew. Instead you might refresh the cache asynchronously, so that the entries are reloaded without penalizing any callers.
@Scheduled(cron = "0 0,12 * * *")
public void refresh() {
cache.refreshAll(cache.asMap().keySet());
}