From c26073afeb59ff898bb46a59031728fb99985a32 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期日, 04 七月 2021 02:13:24 +0800 Subject: [PATCH] update 优化缓存配置 增加spring-cache演示案例 --- ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java | 8 ++ ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java | 70 +++++++++++++++++++++++ ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java | 34 +++++++++++ ruoyi-admin/src/main/resources/application.yml | 15 +++++ 4 files changed, 125 insertions(+), 2 deletions(-) diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 9fec4ae..1d3415e 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -262,6 +262,21 @@ circuitbreaker: enabled: true +--- # redisson 缂撳瓨閰嶇疆 +redisson: + cacheGroup: + # 鐢ㄤ緥: @Cacheable(cacheNames="groupId", key="#XXX") 鏂瑰彲浣跨敤缂撳瓨缁勯厤缃� + - groupId: redissonCacheMap + # 缁勮繃鏈熸椂闂�(鑴氭湰鐩戞帶) + ttl: 60000 + # 缁勬渶澶х┖闂叉椂闂�(鑴氭湰鐩戞帶) + maxIdleTime: 60000 + # 缁勬渶澶ч暱搴� + maxSize: 0 + - groupId: testCache + ttl: 1000 + maxIdleTime: 500 + --- # 鍒嗗竷寮忛攣 lock4j 鍏ㄥ眬閰嶇疆 lock4j: # 鑾峰彇鍒嗗竷寮忛攣瓒呮椂鏃堕棿锛岄粯璁や负 3000 姣 diff --git a/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java b/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java new file mode 100644 index 0000000..4588f13 --- /dev/null +++ b/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java @@ -0,0 +1,70 @@ +package com.ruoyi.demo.controller; + +import com.ruoyi.common.core.domain.AjaxResult; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.CachePut; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * spring-cache 婕旂ず妗堜緥 + * + * @author Lion Li + */ +// 绫荤骇鍒� 缂撳瓨缁熶竴閰嶇疆 +//@CacheConfig(cacheNames = "redissonCacheMap") +@RequiredArgsConstructor(onConstructor_ = @Autowired) +@RestController +@RequestMapping("/demo/cache") +public class RedisCacheController { + + /** + * 娴嬭瘯 @Cacheable + * + * 琛ㄧず杩欎釜鏂规硶鏈変簡缂撳瓨鐨勫姛鑳�,鏂规硶鐨勮繑鍥炲�间細琚紦瀛樹笅鏉� + * 涓嬩竴娆¤皟鐢ㄨ鏂规硶鍓�,浼氬幓妫�鏌ユ槸鍚︾紦瀛樹腑宸茬粡鏈夊�� + * 濡傛灉鏈夊氨鐩存帴杩斿洖,涓嶈皟鐢ㄦ柟娉� + * 濡傛灉娌℃湁,灏辫皟鐢ㄦ柟娉�,鐒跺悗鎶婄粨鏋滅紦瀛樿捣鏉� + * 杩欎釜娉ㄨВ銆屼竴鑸敤鍦ㄦ煡璇㈡柟娉曚笂銆� + * + * cacheNames 涓洪厤缃枃浠跺唴 groupId + */ + @Cacheable(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null") + @GetMapping("/test1") + public AjaxResult<String> test1(String key, String value){ + return AjaxResult.success("鎿嶄綔鎴愬姛", value); + } + + /** + * 娴嬭瘯 @CachePut + * + * 鍔犱簡@CachePut娉ㄨВ鐨勬柟娉�,浼氭妸鏂规硶鐨勮繑鍥炲�紁ut鍒扮紦瀛橀噷闈㈢紦瀛樿捣鏉�,渚涘叾瀹冨湴鏂逛娇鐢� + * 瀹冦�岄�氬父鐢ㄥ湪鏂板鏂规硶涓娿�� + * + * cacheNames 涓� 閰嶇疆鏂囦欢鍐� groupId + */ + @CachePut(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null") + @GetMapping("/test2") + public AjaxResult<String> test2(String key, String value){ + return AjaxResult.success("鎿嶄綔鎴愬姛", value); + } + + /** + * 娴嬭瘯 @CacheEvict + * + * 浣跨敤浜咰acheEvict娉ㄨВ鐨勬柟娉�,浼氭竻绌烘寚瀹氱紦瀛� + * 銆屼竴鑸敤鍦ㄦ洿鏂版垨鑰呭垹闄ょ殑鏂规硶涓娿�� + * + * cacheNames 涓� 閰嶇疆鏂囦欢鍐� groupId + */ + @CacheEvict(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null") + @GetMapping("/test3") + public AjaxResult<String> test3(String key, String value){ + return AjaxResult.success("鎿嶄綔鎴愬姛", value); + } + +} diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java index 95c7572..bc28657 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -78,8 +79,13 @@ */ @Bean public CacheManager cacheManager(RedissonClient redissonClient) { + List<RedissonProperties.CacheGroup> cacheGroup = redissonProperties.getCacheGroup(); Map<String, CacheConfig> config = new HashMap<>(); - config.put("redissonCacheMap", new CacheConfig(30*60*1000, 10*60*1000)); + for (RedissonProperties.CacheGroup group : cacheGroup) { + CacheConfig cacheConfig = new CacheConfig(group.getTtl(), group.getMaxIdleTime()); + cacheConfig.setMaxSize(group.getMaxSize()); + config.put(group.getGroupId(), cacheConfig); + } return new RedissonSpringCacheManager(redissonClient, config, JsonJacksonCodec.INSTANCE); } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java index 99db89e..b6e289d 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java @@ -2,10 +2,11 @@ import lombok.Data; import lombok.NoArgsConstructor; -import org.redisson.client.codec.Codec; import org.redisson.config.TransportMode; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; + +import java.util.List; /** * Redisson 閰嶇疆灞炴�� @@ -36,6 +37,11 @@ * 鍗曟満鏈嶅姟閰嶇疆 */ private SingleServerConfig singleServerConfig; + + /** + * 缂撳瓨缁� + */ + private List<CacheGroup> cacheGroup; @Data @NoArgsConstructor @@ -98,4 +104,30 @@ } + @Data + @NoArgsConstructor + public static class CacheGroup { + + /** + * 缁刬d + */ + private String groupId; + + /** + * 缁勮繃鏈熸椂闂� + */ + private long ttl; + + /** + * 缁勬渶澶х┖闂叉椂闂� + */ + private long maxIdleTime; + + /** + * 缁勬渶澶ч暱搴� + */ + private int maxSize; + + } + } -- Gitblit v1.9.3