From b9a02194e267b8de8e9cd2b76896bb742c9e6b0e Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Sat, 11 Oct 2025 16:24:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leopard/core/service/StockService.java | 5 +++ leopard-server/pom.xml | 8 +++++ .../server/configuration/CacheProvider.java | 35 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/configuration/CacheProvider.java diff --git a/leopard-core/src/main/java/com/lanyuanxiaoyao/leopard/core/service/StockService.java b/leopard-core/src/main/java/com/lanyuanxiaoyao/leopard/core/service/StockService.java index c4eeb0f..cbc72dd 100644 --- a/leopard-core/src/main/java/com/lanyuanxiaoyao/leopard/core/service/StockService.java +++ b/leopard-core/src/main/java/com/lanyuanxiaoyao/leopard/core/service/StockService.java @@ -15,6 +15,7 @@ import java.time.LocalDate; import java.util.List; import java.util.Optional; import lombok.extern.slf4j.Slf4j; +import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; @@ -34,6 +35,7 @@ public class StockService extends SimpleServiceSupport { this.dailyRepository = dailyRepository; } + @Cacheable(value = "long-cache", sync = true) public Optional findFinanceIndicator(Long stockId, Integer year) { return financeIndicatorRepository.findOne( QFinanceIndicator.financeIndicator.year.eq(year) @@ -41,6 +43,7 @@ public class StockService extends SimpleServiceSupport { ); } + @Cacheable(value = "long-cache", sync = true) public List findFinanceIndicatorRecent(Long stockId, int years) { var current = LocalDate.now(); return financeIndicatorRepository.findAll( @@ -50,6 +53,7 @@ public class StockService extends SimpleServiceSupport { ); } + @Cacheable(value = "long-cache", sync = true) public List findDailyRecent(Long stockId, int days) { var current = LocalDate.now(); return dailyRepository.findAll( @@ -59,6 +63,7 @@ public class StockService extends SimpleServiceSupport { ); } + @Cacheable(value = "long-cache", sync = true) public Optional findDailyLatest(Long stockId) { return dailyRepository.findLatest(stockId); } diff --git a/leopard-server/pom.xml b/leopard-server/pom.xml index 3eae2c1..24d4aed 100644 --- a/leopard-server/pom.xml +++ b/leopard-server/pom.xml @@ -31,6 +31,14 @@ org.springframework.boot spring-boot-starter-jetty + + org.springframework.boot + spring-boot-starter-cache + + + com.github.ben-manes.caffeine + caffeine + org.springframework.boot spring-boot-configuration-processor diff --git a/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/configuration/CacheProvider.java b/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/configuration/CacheProvider.java new file mode 100644 index 0000000..3a5c5f1 --- /dev/null +++ b/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/configuration/CacheProvider.java @@ -0,0 +1,35 @@ +package com.lanyuanxiaoyao.leopard.server.configuration; + +import com.github.benmanes.caffeine.cache.Caffeine; +import java.util.concurrent.TimeUnit; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; + +/** + * 缓存提供 + * + * @author lanyuanxiaoyao + * @date 2023-04-23 + */ +@Configuration +@EnableCaching +public class CacheProvider { + @Primary + @Bean("short-cache") + public CacheManager normalCache() { + CaffeineCacheManager manager = new CaffeineCacheManager(); + manager.setCaffeine(Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES)); + return manager; + } + + @Bean("long-cache") + public CacheManager longCache() { + CaffeineCacheManager manager = new CaffeineCacheManager(); + manager.setCaffeine(Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS)); + return manager; + } +}