Răsfoiți Sursa

add cache for portfolio history

Daniel Bohry 2 luni în urmă
părinte
comite
c3da287292

+ 4 - 1
src/main/java/com/danielbohry/stocks/config/CacheConfig.java

@@ -37,8 +37,11 @@ public class CacheConfig {
         CaffeineCache stockHistory = new CaffeineCache("stockHistory",
             Caffeine.newBuilder().expireAfterWrite(5, MINUTES).build());
 
+        CaffeineCache portfolioHistory = new CaffeineCache("portfolioHistory",
+            Caffeine.newBuilder().expireAfterWrite(24, HOURS).build());
+
         SimpleCacheManager manager = new SimpleCacheManager();
-        manager.setCaches(List.of(exchangeRates, allStockQuotes, stockQuotesQuery, stockQuotes, stockInfo, stockHistory));
+        manager.setCaches(List.of(exchangeRates, allStockQuotes, stockQuotesQuery, stockQuotes, stockInfo, stockHistory, portfolioHistory));
         return manager;
     }
 

+ 2 - 0
src/main/java/com/danielbohry/stocks/service/portfolio/PortfolioHistoryService.java

@@ -9,6 +9,7 @@ import com.danielbohry.stocks.service.stock.StockEncryptService;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
@@ -43,6 +44,7 @@ public class PortfolioHistoryService {
         saveSnapshots(portfolios);
     }
 
+    @Cacheable(value = "portfolioHistory", key = "#portfolioId + '-' + #currency")
     public List<PortfolioHistory> getSnapshots(String portfolioId, String currency) {
         List<PortfolioHistory> history = repository.findAllByPortfolioId(portfolioId).stream()
             .map(entity -> PortfolioHistory.builder()