ruoyi-admin/src/main/resources/application.yml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ruoyi-common/src/main/java/com/ruoyi/common/constant/CacheNames.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ruoyi-framework/src/main/java/com/ruoyi/framework/manager/PlusSpringCacheManager.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
ruoyi-admin/src/main/resources/application.yml
@@ -233,21 +233,6 @@ # çº¿ç¨æ± ç»´æ¤çº¿ç¨æå 许çç©ºé²æ¶é´ keepAliveSeconds: 300 --- # 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 æ¯«ç§ ruoyi-common/src/main/java/com/ruoyi/common/constant/CacheNames.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,23 @@ package com.ruoyi.common.constant; /** * ç¼åç»å称常é * <p> * key æ ¼å¼ä¸º cacheNames#ttl#maxIdleTime#maxSize * <p> * ttl è¿ææ¶é´ å¦æè®¾ç½®ä¸º0åä¸è¿æ é»è®¤ä¸º0 * maxIdleTime æå¤§ç©ºé²æ¶é´ æ ¹æ®LRUç®æ³æ¸ çç©ºé²æ°æ® å¦æè®¾ç½®ä¸º0å䏿£æµ é»è®¤ä¸º0 * maxSize ç»æå¤§é¿åº¦ æ ¹æ®LRUç®æ³æ¸ çæº¢åºæ°æ® å¦æè®¾ç½®ä¸º0åæ éé¿ é»è®¤ä¸º0 * <p> * ä¾å: test#60sãtest#0#60sãtest#0#1m#1000ãtest#1h#0#500 * * @author Lion Li */ public interface CacheNames { /** * æ¼ç¤ºæ¡ä¾ */ String DEMO_CACHE = "demo:cache#60s#10m#20"; } ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java
@@ -1,5 +1,6 @@ package com.ruoyi.demo.controller; import com.ruoyi.common.constant.CacheNames; import com.ruoyi.common.core.domain.R; import com.ruoyi.common.utils.redis.RedisUtils; import lombok.RequiredArgsConstructor; @@ -18,7 +19,7 @@ * @author Lion Li */ // ç±»çº§å« ç¼åç»ä¸é ç½® //@CacheConfig(cacheNames = "redissonCacheMap") //@CacheConfig(cacheNames = CacheNames.DEMO_CACHE) @RequiredArgsConstructor @RestController @RequestMapping("/demo/cache") @@ -36,9 +37,9 @@ * éç¹è¯´æ: ç¼å注解严谨ä¸å ¶ä»çéæ°æ®åè½ä¸èµ·ä½¿ç¨ * ä¾å¦: æ°æ®æé注解 ä¼é æ ç¼åå»ç©¿ ä¸ æ°æ®ä¸ä¸è´é®é¢ * <p> * cacheNames 为é ç½®æä»¶å groupId * cacheNames å½åè§å æ¥ç {@link CacheNames} æ³¨é æ¯æå¤åæ° */ @Cacheable(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null") @Cacheable(cacheNames = "demo:cache#60s#10m#20", key = "#key", condition = "#key != null") @GetMapping("/test1") public R<String> test1(String key, String value) { return R.ok("æä½æå", value); @@ -48,11 +49,11 @@ * æµè¯ @CachePut * <p> * å äº@CachePutæ³¨è§£çæ¹æ³,ä¼ææ¹æ³çè¿åå¼putå°ç¼åéé¢ç¼åèµ·æ¥,ä¾å ¶å®å°æ¹ä½¿ç¨ * å®ãé常ç¨å¨æ°å¢æ¹æ³ä¸ã * å®ãé常ç¨å¨æ°å¢æè 宿¶æ´æ°æ¹æ³ä¸ã * <p> * cacheNames 为 é ç½®æä»¶å groupId * cacheNames å½åè§å æ¥ç {@link CacheNames} æ³¨é æ¯æå¤åæ° */ @CachePut(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null") @CachePut(cacheNames = CacheNames.DEMO_CACHE, key = "#key", condition = "#key != null") @GetMapping("/test2") public R<String> test2(String key, String value) { return R.ok("æä½æå", value); @@ -62,11 +63,11 @@ * æµè¯ @CacheEvict * <p> * 使ç¨äºCacheEvictæ³¨è§£çæ¹æ³,伿¸ 空æå®ç¼å * ãä¸è¬ç¨å¨æ´æ°æè å é¤çæ¹æ³ä¸ã * ãä¸è¬ç¨å¨å é¤çæ¹æ³ä¸ã * <p> * cacheNames 为 é ç½®æä»¶å groupId * cacheNames å½åè§å æ¥ç {@link CacheNames} æ³¨é æ¯æå¤åæ° */ @CacheEvict(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null") @CacheEvict(cacheNames = CacheNames.DEMO_CACHE, key = "#key", condition = "#key != null") @GetMapping("/test3") public R<String> test3(String key, String value) { return R.ok("æä½æå", value); ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java
@@ -4,11 +4,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.ruoyi.framework.config.properties.RedissonProperties; import com.ruoyi.framework.handler.KeyPrefixHandler; import com.ruoyi.framework.manager.PlusSpringCacheManager; import lombok.extern.slf4j.Slf4j; import org.redisson.api.RedissonClient; import org.redisson.codec.JsonJacksonCodec; import org.redisson.spring.cache.CacheConfig; import org.redisson.spring.cache.RedissonSpringCacheManager; import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -17,10 +15,6 @@ import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.HashMap; import java.util.List; import java.util.Map; /** * redisé ç½® @@ -80,18 +74,11 @@ } /** * æ´åspring-cache * èªå®ä¹ç¼å管çå¨ æ´åspring-cache */ @Bean public CacheManager cacheManager(RedissonClient redissonClient) { List<RedissonProperties.CacheGroup> cacheGroup = redissonProperties.getCacheGroup(); Map<String, CacheConfig> config = new HashMap<>(); 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, new JsonJacksonCodec(objectMapper)); public CacheManager cacheManager() { return new PlusSpringCacheManager(); } /** ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java
@@ -7,8 +7,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List; /** * Redisson é ç½®å±æ§ * @@ -18,12 +16,12 @@ @Component @ConfigurationProperties(prefix = "redisson") public class RedissonProperties { /** * redisç¼åkeyåç¼ */ private String keyPrefix; /** * çº¿ç¨æ± æ°é,é»è®¤å¼ = å½åå¤çæ ¸æ°é * 2 */ @@ -43,11 +41,6 @@ * é群æå¡é ç½® */ private ClusterServersConfig clusterServersConfig; /** * ç¼åç» */ private List<CacheGroup> cacheGroup; @Data @NoArgsConstructor @@ -138,32 +131,6 @@ * è®¢é æ¨¡å¼ */ private SubscriptionMode subscriptionMode; } @Data @NoArgsConstructor public static class CacheGroup { /** * ç»id */ private String groupId; /** * ç»è¿ææ¶é´ */ private long ttl; /** * ç»æå¤§ç©ºé²æ¶é´ */ private long maxIdleTime; /** * ç»æå¤§é¿åº¦ */ private int maxSize; } ruoyi-framework/src/main/java/com/ruoyi/framework/manager/PlusSpringCacheManager.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,191 @@ /** * Copyright (c) 2013-2021 Nikita Koksharov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.ruoyi.framework.manager; import com.ruoyi.common.utils.redis.RedisUtils; import org.redisson.api.RMap; import org.redisson.api.RMapCache; import org.redisson.spring.cache.CacheConfig; import org.redisson.spring.cache.RedissonCache; import org.springframework.boot.convert.DurationStyle; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.cache.transaction.TransactionAwareCacheDecorator; import org.springframework.util.StringUtils; import java.util.Collection; import java.util.Collections; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; /** * A {@link org.springframework.cache.CacheManager} implementation * backed by Redisson instance. * <p> * ä¿®æ¹ RedissonSpringCacheManager æºç * éå cacheName å¤çæ¹æ³ æ¯æå¤åæ° * * @author Nikita Koksharov * */ @SuppressWarnings("unchecked") public class PlusSpringCacheManager implements CacheManager { private boolean dynamic = true; private boolean allowNullValues = true; private boolean transactionAware = true; Map<String, CacheConfig> configMap = new ConcurrentHashMap<>(); ConcurrentMap<String, Cache> instanceMap = new ConcurrentHashMap<>(); /** * Creates CacheManager supplied by Redisson instance */ public PlusSpringCacheManager() { } /** * Defines possibility of storing {@code null} values. * <p> * Default is <code>true</code> * * @param allowNullValues stores if <code>true</code> */ public void setAllowNullValues(boolean allowNullValues) { this.allowNullValues = allowNullValues; } /** * Defines if cache aware of Spring-managed transactions. * If {@code true} put/evict operations are executed only for successful transaction in after-commit phase. * <p> * Default is <code>false</code> * * @param transactionAware cache is transaction aware if <code>true</code> */ public void setTransactionAware(boolean transactionAware) { this.transactionAware = transactionAware; } /** * Defines 'fixed' cache names. * A new cache instance will not be created in dynamic for non-defined names. * <p> * `null` parameter setups dynamic mode * * @param names of caches */ public void setCacheNames(Collection<String> names) { if (names != null) { for (String name : names) { getCache(name); } dynamic = false; } else { dynamic = true; } } /** * Set cache config mapped by cache name * * @param config object */ public void setConfig(Map<String, ? extends CacheConfig> config) { this.configMap = (Map<String, CacheConfig>) config; } protected CacheConfig createDefaultConfig() { return new CacheConfig(); } @Override public Cache getCache(String name) { Cache cache = instanceMap.get(name); if (cache != null) { return cache; } if (!dynamic) { return cache; } CacheConfig config = configMap.get(name); if (config == null) { config = createDefaultConfig(); configMap.put(name, config); } // éå cacheName æ¯æå¤åæ° String[] array = StringUtils.delimitedListToStringArray(name, "#"); name = array[0]; if (array.length > 1) { config.setTTL(DurationStyle.detectAndParse(array[1]).toMillis()); } if (array.length > 2) { config.setMaxIdleTime(DurationStyle.detectAndParse(array[2]).toMillis()); } if (array.length > 3) { config.setMaxSize(Integer.parseInt(array[3])); } if (config.getMaxIdleTime() == 0 && config.getTTL() == 0 && config.getMaxSize() == 0) { return createMap(name, config); } return createMapCache(name, config); } private Cache createMap(String name, CacheConfig config) { RMap<Object, Object> map = RedisUtils.getClient().getMap(name); Cache cache = new RedissonCache(map, allowNullValues); if (transactionAware) { cache = new TransactionAwareCacheDecorator(cache); } Cache oldCache = instanceMap.putIfAbsent(name, cache); if (oldCache != null) { cache = oldCache; } return cache; } private Cache createMapCache(String name, CacheConfig config) { RMapCache<Object, Object> map = RedisUtils.getClient().getMapCache(name); Cache cache = new RedissonCache(map, config, allowNullValues); if (transactionAware) { cache = new TransactionAwareCacheDecorator(cache); } Cache oldCache = instanceMap.putIfAbsent(name, cache); if (oldCache != null) { cache = oldCache; } else { map.setMaxSize(config.getMaxSize()); } return cache; } @Override public Collection<String> getCacheNames() { return Collections.unmodifiableSet(configMap.keySet()); } }