package com.danielbohry.stocks.config; import com.github.benmanes.caffeine.cache.Caffeine; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.caffeine.CaffeineCache; import org.springframework.cache.support.SimpleCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.List; import static java.util.concurrent.TimeUnit.HOURS; import static java.util.concurrent.TimeUnit.MINUTES; @Configuration @EnableCaching public class CacheConfig { @Bean public CacheManager cacheManager() { CaffeineCache exchangeRates = new CaffeineCache("exchangeRates", Caffeine.newBuilder().expireAfterWrite(6, HOURS).build()); CaffeineCache stockQuotes = new CaffeineCache("stockQuotes", Caffeine.newBuilder().expireAfterWrite(10, MINUTES).build()); CaffeineCache portfolio = new CaffeineCache("portfolio", Caffeine.newBuilder().expireAfterWrite(10, MINUTES).build()); SimpleCacheManager manager = new SimpleCacheManager(); manager.setCaches(List.of(exchangeRates, stockQuotes, portfolio)); return manager; } }