fix:加入本地缓存

上级 6d2aeb46
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
<name>SpringBoot-kwan</name> <name>SpringBoot-kwan</name>
<description>Demo project for Spring Boot</description> <description>Demo project for Spring Boot</description>
<properties> <properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<!-- 与1.8配置在一起,设置编码集--> <!-- 与1.8配置在一起,设置编码集-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...@@ -230,7 +232,12 @@ ...@@ -230,7 +232,12 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId> <artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>3.1.2</version> </dependency>
<!-- 引入依赖-->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<!-- <version>3.1.2</version>-->
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.kwan.springbootkwan.config;
import com.github.benmanes.caffeine.cache.Caffeine;
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 java.util.concurrent.TimeUnit;
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public Caffeine<Object, Object> caffeineConfig() {
return Caffeine.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES) // 设置缓存失效时间为10分钟
.maximumSize(10_000) // 设置缓存的最大容量为10_000
.recordStats(); // 启用统计信息,可通过Cache.stats()获取缓存命中率等统计数据
}
@Bean
public CacheManager cacheManager(Caffeine<Object, Object> caffeine) {
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
cacheManager.setCaffeine(caffeine);
return cacheManager;
}
}
...@@ -8,6 +8,7 @@ import com.kwan.springbootkwan.entity.Result; ...@@ -8,6 +8,7 @@ import com.kwan.springbootkwan.entity.Result;
import com.kwan.springbootkwan.entity.dto.ChatbotDTO; import com.kwan.springbootkwan.entity.dto.ChatbotDTO;
import com.kwan.springbootkwan.service.ChatbotService; import com.kwan.springbootkwan.service.ChatbotService;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
...@@ -60,6 +61,7 @@ public class ChatbotController { ...@@ -60,6 +61,7 @@ public class ChatbotController {
* *
* @return 所有数据 * @return 所有数据
*/ */
@Cacheable("chatbot-cache")
@GetMapping("/page") @GetMapping("/page")
public Result selectAll(@RequestParam Integer page public Result selectAll(@RequestParam Integer page
, @RequestParam Integer pageSize , @RequestParam Integer pageSize
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册