Daniel Bohry 8 месяцев назад
Родитель
Сommit
907f92ced4

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

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

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

@@ -8,6 +8,7 @@ import com.danielbohry.stocks.repository.StockInfoRepository;
 import com.danielbohry.stocks.repository.StockRepository;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
@@ -25,6 +26,7 @@ public class StockInfoService {
     private StockRepository stockRepository;
     private StockInfoRepository infoRepository;
 
+    @Cacheable(value = "stockInfo", key = "#code")
     public StockInfo get(String code) {
         return infoRepository.findById(code)
             .orElseThrow(() -> new NotFoundException("No stock found with id: " + code));