Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

caffeine cache refreshAfterWrite method throwing refreshAfterWrite requires a LoadingCache exception

I have a requirement on re cache the expired cache by calling the API again

The below is my cache manager configuration

private CaffeineCache buildCache(
    String name,
    Ticker ticker,
    int minutesToExpire
) {
    return new CaffeineCache(name, Caffeine.newBuilder()
                .refreshAfterWrite(minutesToExpire, TimeUnit.MINUTES)
                .maximumSize(100)
                .ticker(ticker)
                .build());
}

But while starting the application it is throwing the below exception

Caused by: java.lang.IllegalStateException: refreshAfterWrite requires a LoadingCache

like image 651
anoop c Avatar asked Mar 17 '26 22:03

anoop c


1 Answers

You need to provide a CacheLoader to the build method.

return new CaffeineCache(
        name,
        Caffeine.newBuilder()
                .refreshAfterWrite(minutesToExpire, TimeUnit.MINUTES)
                .maximumSize(100)
                .ticker(ticker)
                .build(key -> createExpensiveObject(key))
);

The CacheLoader is a class/method that has to be implemented by you.

like image 169
Stefan Birkner Avatar answered Mar 20 '26 17:03

Stefan Birkner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!