Browse Source

add cache for portfolios

Daniel Bohry 8 months ago
parent
commit
ba6d246d1a

+ 0 - 1
src/main/java/com/danielbohry/stocks/api/stock/StockController.java

@@ -17,7 +17,6 @@ import java.util.Objects;
 
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static java.time.LocalDateTime.now;
-import static java.util.Collections.emptyList;
 
 @Slf4j
 @RestController

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

@@ -25,8 +25,11 @@ public class CacheConfig {
         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));
+        manager.setCaches(List.of(exchangeRates, stockQuotes, portfolio));
         return manager;
     }
 

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

@@ -10,6 +10,7 @@ import com.danielbohry.stocks.repository.PortfolioRepository;
 import com.danielbohry.stocks.service.ExchangeService.ExchangeRateResponse;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
 
 import java.math.BigDecimal;
@@ -39,6 +40,7 @@ public class PortfolioService {
         return repository.findAllPortfolioIds();
     }
 
+    @Cacheable(value = "portfolio", key = "#username + '_' + #currency")
     public List<Portfolio> getByUser(String username, String currency) {
         List<Portfolio> portfolios = repository.findAllByUsername(username);