syh 4 سال پیش
والد
کامیت
6aa0745146

+ 1 - 1
jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/controller/SysDataSourceController.java

@@ -13,7 +13,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.modules.ads.controller.JeecgController;
 import org.jeecg.modules.query.QueryGenerator;
-import org.jeecg.modules.db.dynamic.DataSourceCachePool;
+import cn.com.ctop.common.module.db.DataSourceCachePool;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.servlet.ModelAndView;

+ 92 - 0
module-common/src/main/java/cn/com/ctop/common/module/db/DataSourceCachePool.java

@@ -0,0 +1,92 @@
+package cn.com.ctop.common.module.db;
+
+import cn.com.ctop.common.module.service.ISysBaseApi;
+import com.alibaba.druid.pool.DruidDataSource;
+import org.jeecg.common.constant.CacheConstant;
+import org.jeecg.common.system.vo.DynamicDataSourceModel;
+import org.jeecg.common.util.SpringContextUtils;
+import org.springframework.data.redis.core.RedisTemplate;
+
+import java.util.HashMap;
+import java.util.Map;
+
+
+/**
+ * 数据源缓存池
+ */
+public class DataSourceCachePool {
+    /** 数据源连接池缓存【本地 class缓存 - 不支持分布式】 */
+    private static Map<String, DruidDataSource> dbSources = new HashMap<>();
+    private static RedisTemplate<String, Object> redisTemplate;
+
+    private static RedisTemplate<String, Object> getRedisTemplate() {
+        if (redisTemplate == null) {
+            redisTemplate = (RedisTemplate<String, Object>) SpringContextUtils.getBean("redisTemplate");
+        }
+        return redisTemplate;
+    }
+
+    /**
+     * 获取多数据源缓存
+     *
+     * @param dbKey
+     * @return
+     */
+    public static DynamicDataSourceModel getCacheDynamicDataSourceModel(String dbKey) {
+        String redisCacheKey = CacheConstant.SYS_DYNAMICDB_CACHE + dbKey;
+        if (getRedisTemplate().hasKey(redisCacheKey)) {
+            return (DynamicDataSourceModel) getRedisTemplate().opsForValue().get(redisCacheKey);
+        }
+        ISysBaseApi sysBaseAPI = SpringContextUtils.getBean(ISysBaseApi.class);
+        DynamicDataSourceModel dbSource = sysBaseAPI.getDynamicDbSourceByCode(dbKey);
+        if (dbSource != null) {
+            getRedisTemplate().opsForValue().set(redisCacheKey, dbSource);
+        }
+        return dbSource;
+    }
+
+    public static DruidDataSource getCacheBasicDataSource(String dbKey) {
+        return dbSources.get(dbKey);
+    }
+
+    /**
+     * put 数据源缓存
+     *
+     * @param dbKey
+     * @param db
+     */
+    public static void putCacheBasicDataSource(String dbKey, DruidDataSource db) {
+        dbSources.put(dbKey, db);
+    }
+
+    /**
+     * 清空数据源缓存
+     */
+    public static void cleanAllCache() {
+        //关闭数据源连接
+        for(Map.Entry<String, DruidDataSource> entry : dbSources.entrySet()){
+            String dbkey = entry.getKey();
+            DruidDataSource druidDataSource = entry.getValue();
+            if(druidDataSource!=null && druidDataSource.isEnable()){
+                druidDataSource.close();
+            }
+            //清空redis缓存
+            getRedisTemplate().delete(CacheConstant.SYS_DYNAMICDB_CACHE + dbkey);
+        }
+        //清空缓存
+        dbSources.clear();
+    }
+
+    public static void removeCache(String dbKey) {
+        //关闭数据源连接
+        DruidDataSource druidDataSource = dbSources.get(dbKey);
+        if(druidDataSource!=null && druidDataSource.isEnable()){
+            druidDataSource.close();
+        }
+        //清空redis缓存
+        getRedisTemplate().delete(CacheConstant.SYS_DYNAMICDB_CACHE + dbKey);
+        //清空缓存
+        dbSources.remove(dbKey);
+    }
+
+}

+ 2 - 1
jeecg-boot-base-common/src/main/java/org/jeecg/common/util/db/DynamicDBUtil.java

@@ -1,4 +1,4 @@
-package org.jeecg.common.util.db;
+package cn.com.ctop.common.module.db;
 
 import com.alibaba.druid.pool.DruidDataSource;
 import lombok.extern.slf4j.Slf4j;
@@ -6,6 +6,7 @@ import org.apache.commons.lang.ArrayUtils;
 import org.jeecg.common.exception.JeecgBootException;
 import org.jeecg.common.system.vo.DynamicDataSourceModel;
 import org.jeecg.common.util.ReflectHelper;
+import org.jeecg.common.util.db.FreemarkerParseFactory;
 import org.jeecg.common.util.oConvertUtils;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

+ 1 - 1
jeecg-boot-module-system/src/main/java/org/jeecg/modules/db/DataSourceCachePool.java

@@ -1,4 +1,4 @@
-package org.jeecg.modules.db;
+package cn.com.ctop.common.module.db.dynamic;
 
 import cn.com.ctop.common.module.service.ISysBaseApi;
 import com.alibaba.druid.pool.DruidDataSource;