949084b3da57672b19c560b67bae13be69017a76.svn-base 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package org.jeecg.common.util.dynamic.db;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import org.jeecg.common.api.CommonAPI;
  4. import org.jeecg.common.constant.CacheConstant;
  5. import org.jeecg.common.system.vo.DynamicDataSourceModel;
  6. import org.jeecg.common.util.RedisUtil;
  7. import org.jeecg.common.util.SpringContextUtils;
  8. import org.springframework.data.redis.core.RedisTemplate;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. /**
  12. * 数据源缓存池
  13. */
  14. public class DataSourceCachePool {
  15. /** 数据源连接池缓存【本地 class缓存 - 不支持分布式】 */
  16. private static Map<String, DruidDataSource> dbSources = new HashMap<>();
  17. private static RedisTemplate<String, Object> redisTemplate;
  18. private static RedisTemplate<String, Object> getRedisTemplate() {
  19. if (redisTemplate == null) {
  20. redisTemplate = (RedisTemplate<String, Object>) SpringContextUtils.getBean("redisTemplate");
  21. }
  22. return redisTemplate;
  23. }
  24. /**
  25. * 获取多数据源缓存
  26. *
  27. * @param dbKey
  28. * @return
  29. */
  30. public static DynamicDataSourceModel getCacheDynamicDataSourceModel(String dbKey) {
  31. String redisCacheKey = CacheConstant.SYS_DYNAMICDB_CACHE + dbKey;
  32. if (getRedisTemplate().hasKey(redisCacheKey)) {
  33. return (DynamicDataSourceModel) getRedisTemplate().opsForValue().get(redisCacheKey);
  34. }
  35. CommonAPI commonAPI = SpringContextUtils.getBean(CommonAPI.class);
  36. DynamicDataSourceModel dbSource = commonAPI.getDynamicDbSourceByCode(dbKey);
  37. if (dbSource != null) {
  38. getRedisTemplate().opsForValue().set(redisCacheKey, dbSource);
  39. }
  40. return dbSource;
  41. }
  42. public static DruidDataSource getCacheBasicDataSource(String dbKey) {
  43. return dbSources.get(dbKey);
  44. }
  45. /**
  46. * put 数据源缓存
  47. *
  48. * @param dbKey
  49. * @param db
  50. */
  51. public static void putCacheBasicDataSource(String dbKey, DruidDataSource db) {
  52. dbSources.put(dbKey, db);
  53. }
  54. /**
  55. * 清空数据源缓存
  56. */
  57. public static void cleanAllCache() {
  58. //关闭数据源连接
  59. for(Map.Entry<String, DruidDataSource> entry : dbSources.entrySet()){
  60. String dbkey = entry.getKey();
  61. DruidDataSource druidDataSource = entry.getValue();
  62. if(druidDataSource!=null && druidDataSource.isEnable()){
  63. druidDataSource.close();
  64. }
  65. //清空redis缓存
  66. getRedisTemplate().delete(CacheConstant.SYS_DYNAMICDB_CACHE + dbkey);
  67. }
  68. //清空缓存
  69. dbSources.clear();
  70. }
  71. public static void removeCache(String dbKey) {
  72. //关闭数据源连接
  73. DruidDataSource druidDataSource = dbSources.get(dbKey);
  74. if(druidDataSource!=null && druidDataSource.isEnable()){
  75. druidDataSource.close();
  76. }
  77. //清空redis缓存
  78. getRedisTemplate().delete(CacheConstant.SYS_DYNAMICDB_CACHE + dbKey);
  79. //清空缓存
  80. dbSources.remove(dbKey);
  81. }
  82. }