|
|
@@ -7,42 +7,44 @@ import org.springframework.cache.support.SimpleCacheManager;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
+import java.time.Duration;
|
|
|
import java.util.List;
|
|
|
|
|
|
import static com.github.benmanes.caffeine.cache.Caffeine.newBuilder;
|
|
|
-import static java.util.concurrent.TimeUnit.HOURS;
|
|
|
-import static java.util.concurrent.TimeUnit.MINUTES;
|
|
|
+import static java.time.Duration.ofHours;
|
|
|
+import static java.time.Duration.ofMinutes;
|
|
|
|
|
|
@Configuration
|
|
|
@EnableCaching
|
|
|
public class CacheConfig {
|
|
|
|
|
|
+ private static final Duration FIVE_MINUTES = ofMinutes(5);
|
|
|
+ private static final Duration SIX_HOURS = ofHours(6);
|
|
|
+
|
|
|
@Bean
|
|
|
public CacheManager cacheManager() {
|
|
|
- CaffeineCache exchangeRates = new CaffeineCache("exchangeRates",
|
|
|
- newBuilder().expireAfterWrite(6, HOURS).build());
|
|
|
-
|
|
|
- CaffeineCache allStockQuotes = new CaffeineCache("allStockQuotes",
|
|
|
- newBuilder().expireAfterWrite(5, MINUTES).build());
|
|
|
-
|
|
|
- CaffeineCache stockQuotesQuery = new CaffeineCache("stockQuotesQuery",
|
|
|
- newBuilder().expireAfterWrite(5, MINUTES).build());
|
|
|
-
|
|
|
- CaffeineCache stockQuotes = new CaffeineCache("stockQuotes",
|
|
|
- newBuilder().expireAfterWrite(5, MINUTES).build());
|
|
|
-
|
|
|
- CaffeineCache stockInfo = new CaffeineCache("stockInfo",
|
|
|
- newBuilder().expireAfterWrite(5, MINUTES).build());
|
|
|
-
|
|
|
- CaffeineCache stockHistory = new CaffeineCache("stockHistory",
|
|
|
- newBuilder().expireAfterWrite(5, MINUTES).build());
|
|
|
-
|
|
|
- CaffeineCache portfolioHistory = new CaffeineCache("portfolioHistory",
|
|
|
- newBuilder().expireAfterWrite(6, HOURS).build());
|
|
|
+ CaffeineCache exchangeRates = build("exchangeRates", SIX_HOURS);
|
|
|
+ CaffeineCache allStockQuotes = build("allStockQuotes", FIVE_MINUTES);
|
|
|
+ CaffeineCache stockQuotesQuery = build("stockQuotesQuery", FIVE_MINUTES);
|
|
|
+ CaffeineCache stockQuotes = build("stockQuotes", FIVE_MINUTES);
|
|
|
+ CaffeineCache stockInfo = build("stockInfo", FIVE_MINUTES);
|
|
|
+ CaffeineCache stockHistory = build("stockHistory", FIVE_MINUTES);
|
|
|
+ CaffeineCache portfolioHistory = build("portfolioHistory", SIX_HOURS, 100);
|
|
|
|
|
|
SimpleCacheManager manager = new SimpleCacheManager();
|
|
|
manager.setCaches(List.of(exchangeRates, allStockQuotes, stockQuotesQuery, stockQuotes, stockInfo, stockHistory, portfolioHistory));
|
|
|
return manager;
|
|
|
}
|
|
|
|
|
|
+ private CaffeineCache build(String name, Duration duration) {
|
|
|
+ return build(name, duration, 500);
|
|
|
+ }
|
|
|
+
|
|
|
+ private CaffeineCache build(String name, Duration duration, long size) {
|
|
|
+ return new CaffeineCache(name, newBuilder()
|
|
|
+ .expireAfterWrite(duration)
|
|
|
+ .maximumSize(size)
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+
|
|
|
}
|