Sfoglia il codice sorgente

update 优化 缓存注解支持关闭本地缓存

疯狂的狮子Li 1 anno fa
parent
commit
33e1d34ce5

+ 3 - 2
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;
 /**
  * 缓存组名称常量
  * <p>
- * key 格式为 cacheNames#ttl#maxIdleTime#maxSize
+ * key 格式为 cacheNames#ttl#maxIdleTime#maxSize#local
  * <p>
  * ttl 过期时间 如果设置为0则不过期 默认为0
  * maxIdleTime 最大空闲时间 根据LRU算法清理空闲数据 如果设置为0则不检测 默认为0
  * maxSize 组最大长度 根据LRU算法清理溢出数据 如果设置为0则无限长 默认为0
+ * local 默认开启本地缓存为1 关闭本地缓存为0
  * <p>
- * 例子: 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
  */

+ 16 - 6
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]));
         }
+        int local = 1;
+        if (array.length > 4) {
+            local = Integer.parseInt(array[4]);
+        }
 
         if (config.getMaxIdleTime() == 0 && config.getTTL() == 0 && config.getMaxSize() == 0) {
-            return createMap(name, config);
+            return createMap(name, config, local);
         }
 
-        return createMapCache(name, config);
+        return createMapCache(name, config, local);
     }
 
-    private Cache createMap(String name, CacheConfig config) {
+    private Cache createMap(String name, CacheConfig config, int local) {
         RMap<Object, Object> 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<Object, Object> 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);
         }

+ 1 - 1
ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/RedisCacheController.java

@@ -40,7 +40,7 @@ public class RedisCacheController {
      * <p>
      * 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<String> test1(String key, String value) {
         return R.ok("操作成功", value);