1
0

feat: 增加接口缓存

This commit is contained in:
2025-10-11 16:24:28 +08:00
parent b0c2530e63
commit b9a02194e2
3 changed files with 48 additions and 0 deletions

View File

@@ -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<Stock> {
this.dailyRepository = dailyRepository;
}
@Cacheable(value = "long-cache", sync = true)
public Optional<FinanceIndicator> findFinanceIndicator(Long stockId, Integer year) {
return financeIndicatorRepository.findOne(
QFinanceIndicator.financeIndicator.year.eq(year)
@@ -41,6 +43,7 @@ public class StockService extends SimpleServiceSupport<Stock> {
);
}
@Cacheable(value = "long-cache", sync = true)
public List<FinanceIndicator> findFinanceIndicatorRecent(Long stockId, int years) {
var current = LocalDate.now();
return financeIndicatorRepository.findAll(
@@ -50,6 +53,7 @@ public class StockService extends SimpleServiceSupport<Stock> {
);
}
@Cacheable(value = "long-cache", sync = true)
public List<Daily> findDailyRecent(Long stockId, int days) {
var current = LocalDate.now();
return dailyRepository.findAll(
@@ -59,6 +63,7 @@ public class StockService extends SimpleServiceSupport<Stock> {
);
}
@Cacheable(value = "long-cache", sync = true)
public Optional<Daily> findDailyLatest(Long stockId) {
return dailyRepository.findLatest(stockId);
}

View File

@@ -31,6 +31,14 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>

View File

@@ -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;
}
}