diff --git a/pom.xml b/pom.xml index 8313aacc829f1bc40c5be228774269b7f00a56ad..c8dd86a586e4c2820814ed41798de2a006808e31 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,8 @@ SpringBoot-kwan Demo project for Spring Boot + 1.8 + 1.8 1.8 UTF-8 @@ -230,7 +232,12 @@ org.springframework.boot spring-boot-starter-thymeleaf - 3.1.2 + + + + com.github.ben-manes.caffeine + caffeine + diff --git a/src/main/java/com/kwan/springbootkwan/config/CacheConfig.java b/src/main/java/com/kwan/springbootkwan/config/CacheConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..fb2c478aaed946f542b32f6dd33bb763a740dd87 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/config/CacheConfig.java @@ -0,0 +1,30 @@ +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 caffeineConfig() { + return Caffeine.newBuilder() + .expireAfterWrite(10, TimeUnit.MINUTES) // 设置缓存失效时间为10分钟 + .maximumSize(10_000) // 设置缓存的最大容量为10_000 + .recordStats(); // 启用统计信息,可通过Cache.stats()获取缓存命中率等统计数据 + } + + @Bean + public CacheManager cacheManager(Caffeine caffeine) { + CaffeineCacheManager cacheManager = new CaffeineCacheManager(); + cacheManager.setCaffeine(caffeine); + return cacheManager; + } +} diff --git a/src/main/java/com/kwan/springbootkwan/controller/ChatbotController.java b/src/main/java/com/kwan/springbootkwan/controller/ChatbotController.java index feb5e6f3b7d125f403f8a58e2147d2b1a5b00074..76e0e70a78f7fa77238cc79062e11413b1c18382 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/ChatbotController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/ChatbotController.java @@ -8,6 +8,7 @@ import com.kwan.springbootkwan.entity.Result; import com.kwan.springbootkwan.entity.dto.ChatbotDTO; import com.kwan.springbootkwan.service.ChatbotService; 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.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -60,6 +61,7 @@ public class ChatbotController { * * @return 所有数据 */ + @Cacheable("chatbot-cache") @GetMapping("/page") public Result selectAll(@RequestParam Integer page , @RequestParam Integer pageSize