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 allStockQuotes = new CaffeineCache("allStockQuotes", Caffeine.newBuilder().expireAfterWrite(5, MINUTES).build()); CaffeineCache stockQuotesQuery = new CaffeineCache("stockQuotesQuery", Caffeine.newBuilder().expireAfterWrite(5, MINUTES).build()); CaffeineCache stockQuotes = new CaffeineCache("stockQuotes", Caffeine.newBuilder().expireAfterWrite(5, MINUTES).build()); CaffeineCache stockInfo = new CaffeineCache("stockInfo", Caffeine.newBuilder().expireAfterWrite(5, MINUTES).build()); SimpleCacheManager manager = new SimpleCacheManager(); manager.setCaches(List.of(exchangeRates, allStockQuotes, stockQuotesQuery, stockQuotes, stockInfo)); return manager; } }