From 3035eb4a54b91e810a5ffd32ce8a446b393679eb Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期四, 04 一月 2024 09:54:03 +0800
Subject: [PATCH] update 优化 全局创建 caffeine 实例

---
 ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/CacheConfig.java                                      |   45 ++++++++++++++++++++++
 ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/PlusCacheWrapper.java                                |   13 +-----
 ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/RedisConfig.java                                      |   12 ------
 ruoyi-common/ruoyi-common-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports |    1 
 4 files changed, 49 insertions(+), 22 deletions(-)

diff --git a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/CacheConfig.java b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/CacheConfig.java
new file mode 100644
index 0000000..d57ba4e
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/CacheConfig.java
@@ -0,0 +1,45 @@
+package org.dromara.common.redis.config;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.dromara.common.redis.manager.PlusSpringCacheManager;
+import org.springframework.boot.autoconfigure.AutoConfiguration;
+import org.springframework.cache.CacheManager;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.context.annotation.Bean;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 缂撳瓨閰嶇疆
+ *
+ * @author Lion Li
+ */
+@AutoConfiguration
+@EnableCaching
+public class CacheConfig {
+
+    /**
+     * caffeine 鏈湴缂撳瓨澶勭悊鍣�
+     */
+    @Bean
+    public Cache<Object, Object> caffeine() {
+        return Caffeine.newBuilder()
+            // 璁剧疆鏈�鍚庝竴娆″啓鍏ユ垨璁块棶鍚庣粡杩囧浐瀹氭椂闂磋繃鏈�
+            .expireAfterWrite(30, TimeUnit.SECONDS)
+            // 鍒濆鐨勭紦瀛樼┖闂村ぇ灏�
+            .initialCapacity(100)
+            // 缂撳瓨鐨勬渶澶ф潯鏁�
+            .maximumSize(1000)
+            .build();
+    }
+
+    /**
+     * 鑷畾涔夌紦瀛樼鐞嗗櫒 鏁村悎spring-cache
+     */
+    @Bean
+    public CacheManager cacheManager() {
+        return new PlusSpringCacheManager();
+    }
+
+}
diff --git a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/RedisConfig.java b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/RedisConfig.java
index 7999f2b..0833d2b 100644
--- a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/RedisConfig.java
+++ b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/RedisConfig.java
@@ -8,7 +8,6 @@
 import lombok.extern.slf4j.Slf4j;
 import org.dromara.common.redis.config.properties.RedissonProperties;
 import org.dromara.common.redis.handler.KeyPrefixHandler;
-import org.dromara.common.redis.manager.PlusSpringCacheManager;
 import org.redisson.client.codec.StringCodec;
 import org.redisson.codec.CompositeCodec;
 import org.redisson.codec.TypedJsonJacksonCodec;
@@ -16,8 +15,6 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.AutoConfiguration;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
-import org.springframework.cache.CacheManager;
-import org.springframework.cache.annotation.EnableCaching;
 import org.springframework.context.annotation.Bean;
 
 /**
@@ -27,7 +24,6 @@
  */
 @Slf4j
 @AutoConfiguration
-@EnableCaching
 @EnableConfigurationProperties(RedissonProperties.class)
 public class RedisConfig {
 
@@ -84,14 +80,6 @@
             }
             log.info("鍒濆鍖� redis 閰嶇疆");
         };
-    }
-
-    /**
-     * 鑷畾涔夌紦瀛樼鐞嗗櫒 鏁村悎spring-cache
-     */
-    @Bean
-    public CacheManager cacheManager() {
-        return new PlusSpringCacheManager();
     }
 
     /**
diff --git a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/PlusCacheWrapper.java b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/PlusCacheWrapper.java
index 65a3e89..8ba4719 100644
--- a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/PlusCacheWrapper.java
+++ b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/PlusCacheWrapper.java
@@ -1,11 +1,10 @@
 package org.dromara.common.redis.manager;
 
 import cn.hutool.core.lang.Console;
-import com.github.benmanes.caffeine.cache.Caffeine;
+import org.dromara.common.core.utils.SpringUtils;
 import org.springframework.cache.Cache;
 
 import java.util.concurrent.Callable;
-import java.util.concurrent.TimeUnit;
 
 /**
  * Cache 瑁呴グ鍣�(鐢ㄤ簬鎵╁睍涓�绾х紦瀛�)
@@ -14,14 +13,8 @@
  */
 public class PlusCacheWrapper implements Cache {
 
-    private static final com.github.benmanes.caffeine.cache.Cache<Object, Object> CAFFEINE = Caffeine.newBuilder()
-        // 璁剧疆鏈�鍚庝竴娆″啓鍏ユ垨璁块棶鍚庣粡杩囧浐瀹氭椂闂磋繃鏈�
-        .expireAfterWrite(30, TimeUnit.SECONDS)
-        // 鍒濆鐨勭紦瀛樼┖闂村ぇ灏�
-        .initialCapacity(100)
-        // 缂撳瓨鐨勬渶澶ф潯鏁�
-        .maximumSize(1000)
-        .build();
+    private static final com.github.benmanes.caffeine.cache.Cache<Object, Object>
+        CAFFEINE = SpringUtils.getBean("caffeine");
 
     private final Cache cache;
 
diff --git a/ruoyi-common/ruoyi-common-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/ruoyi-common/ruoyi-common-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
index 59192c5..0475b19 100644
--- a/ruoyi-common/ruoyi-common-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
+++ b/ruoyi-common/ruoyi-common-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -1 +1,2 @@
 org.dromara.common.redis.config.RedisConfig
+org.dromara.common.redis.config.CacheConfig

--
Gitblit v1.9.3