diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/constant/CacheNames.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/constant/CacheNames.java index 519034cf2..c38f39b47 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/constant/CacheNames.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/constant/CacheNames.java @@ -3,13 +3,14 @@ package org.dromara.common.core.constant; /** * 缓存组名称常量 *

- * key 格式为 cacheNames#ttl#maxIdleTime#maxSize + * key 格式为 cacheNames#ttl#maxIdleTime#maxSize#local *

* ttl 过期时间 如果设置为0则不过期 默认为0 * maxIdleTime 最大空闲时间 根据LRU算法清理空闲数据 如果设置为0则不检测 默认为0 * maxSize 组最大长度 根据LRU算法清理溢出数据 如果设置为0则无限长 默认为0 + * local 默认开启本地缓存为1 关闭本地缓存为0 *

- * 例子: test#60s、test#0#60s、test#0#1m#1000、test#1h#0#500 + * 例子: test#60s、test#0#60s、test#0#1m#1000、test#1h#0#500、test#1h#0#500#0 * * @author Lion Li */ diff --git a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/PlusSpringCacheManager.java b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/PlusSpringCacheManager.java index 740e2a13b..8428ef725 100644 --- a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/PlusSpringCacheManager.java +++ b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/PlusSpringCacheManager.java @@ -145,18 +145,25 @@ public class PlusSpringCacheManager implements CacheManager { if (array.length > 3) { config.setMaxSize(Integer.parseInt(array[3])); } - - if (config.getMaxIdleTime() == 0 && config.getTTL() == 0 && config.getMaxSize() == 0) { - return createMap(name, config); + int local = 1; + if (array.length > 4) { + local = Integer.parseInt(array[4]); } - return createMapCache(name, config); + if (config.getMaxIdleTime() == 0 && config.getTTL() == 0 && config.getMaxSize() == 0) { + return createMap(name, config, local); + } + + return createMapCache(name, config, local); } - private Cache createMap(String name, CacheConfig config) { + private Cache createMap(String name, CacheConfig config, int local) { RMap map = RedisUtils.getClient().getMap(name); - Cache cache = new CaffeineCacheDecorator(name, new RedissonCache(map, allowNullValues)); + Cache cache = new RedissonCache(map, allowNullValues); + if (local == 1) { + cache = new CaffeineCacheDecorator(name, cache); + } if (transactionAware) { cache = new TransactionAwareCacheDecorator(cache); } @@ -167,10 +174,13 @@ public class PlusSpringCacheManager implements CacheManager { return cache; } - private Cache createMapCache(String name, CacheConfig config) { + private Cache createMapCache(String name, CacheConfig config, int local) { RMapCache map = RedisUtils.getClient().getMapCache(name); - Cache cache = new CaffeineCacheDecorator(name, new RedissonCache(map, config, allowNullValues)); + Cache cache = new RedissonCache(map, config, allowNullValues); + if (local == 1) { + cache = new CaffeineCacheDecorator(name, cache); + } if (transactionAware) { cache = new TransactionAwareCacheDecorator(cache); } diff --git a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/RedisCacheController.java b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/RedisCacheController.java index 303cf885d..2335da4cd 100644 --- a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/RedisCacheController.java +++ b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/RedisCacheController.java @@ -40,7 +40,7 @@ public class RedisCacheController { *

* cacheNames 命名规则 查看 {@link CacheNames} 注释 支持多参数 */ - @Cacheable(cacheNames = "demo:cache#60s#10m#20", key = "#key", condition = "#key != null") + @Cacheable(cacheNames = "demo:cache#60s#10m#20#1", key = "#key", condition = "#key != null") @GetMapping("/test1") public R test1(String key, String value) { return R.ok("操作成功", value);