feat: 增加接口缓存
This commit is contained in:
@@ -15,6 +15,7 @@ import java.time.LocalDate;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -34,6 +35,7 @@ public class StockService extends SimpleServiceSupport<Stock> {
|
|||||||
this.dailyRepository = dailyRepository;
|
this.dailyRepository = dailyRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Cacheable(value = "long-cache", sync = true)
|
||||||
public Optional<FinanceIndicator> findFinanceIndicator(Long stockId, Integer year) {
|
public Optional<FinanceIndicator> findFinanceIndicator(Long stockId, Integer year) {
|
||||||
return financeIndicatorRepository.findOne(
|
return financeIndicatorRepository.findOne(
|
||||||
QFinanceIndicator.financeIndicator.year.eq(year)
|
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) {
|
public List<FinanceIndicator> findFinanceIndicatorRecent(Long stockId, int years) {
|
||||||
var current = LocalDate.now();
|
var current = LocalDate.now();
|
||||||
return financeIndicatorRepository.findAll(
|
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) {
|
public List<Daily> findDailyRecent(Long stockId, int days) {
|
||||||
var current = LocalDate.now();
|
var current = LocalDate.now();
|
||||||
return dailyRepository.findAll(
|
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) {
|
public Optional<Daily> findDailyLatest(Long stockId) {
|
||||||
return dailyRepository.findLatest(stockId);
|
return dailyRepository.findLatest(stockId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,14 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-jetty</artifactId>
|
<artifactId>spring-boot-starter-jetty</artifactId>
|
||||||
</dependency>
|
</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>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user