0
    @Bean
    public CacheManager cacheManager() {
        CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
        caffeineCacheManager.getCache("addresses");
        caffeineCacheManager.setCacheLoader(cacheLoader());
        caffeineCacheManager.setCaffeine(caffeineConfig());
        return caffeineCacheManager;
    }
        Caffeine caffeineConfig() {
        return Caffeine.newBuilder().expireAfterAccess(Duration.ofSeconds(5));
    }

public CacheLoader<Object, Object> cacheLoader() {
        return string -> {
            return string;
        };
    }

And I'm using it like this: cacheManager is autowired: cacheManager.getCache("cacheKey").get(123)

How can I make change it to async?

  • Spring Cache does [not support](https://github.com/spring-projects/spring-framework/issues/17920) async methods. You can use Caffeine's `AsyncCache` api directly, though. – Ben Manes Apr 06 '21 at 08:16
  • Can you share an example? – Ankur Gupta Apr 06 '21 at 08:16
  • There is a [tutorial](https://www.baeldung.com/java-caching-caffeine#3-asynchronous-loading) with an example. You can think of it as a bounded `Map>` which will automatically removed a future if it fails. – Ben Manes Apr 06 '21 at 08:22

0 Answers0