mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 15:55:18 +08:00
hibernate 配置迁移,并禁用二级缓存
This commit is contained in:
68
src/main/java/cn/lihongjie/coal/common/SpringUtils.java
Normal file
68
src/main/java/cn/lihongjie/coal/common/SpringUtils.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package cn.lihongjie.coal.common;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.EnumerablePropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@UtilityClass
|
||||
public class SpringUtils {
|
||||
|
||||
public static Map<String, Object> getPropertiesStartingWith(
|
||||
ConfigurableEnvironment aEnv, String aKeyPrefix) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
Map<String, Object> map = getAllProperties(aEnv);
|
||||
|
||||
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
|
||||
if (key.startsWith(aKeyPrefix)) {
|
||||
result.put(key, entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Map<String, Object> getAllProperties(ConfigurableEnvironment aEnv) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
aEnv.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps)));
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Map<String, Object> getAllProperties(PropertySource<?> aPropSource) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
if (aPropSource instanceof CompositePropertySource cps) {
|
||||
cps.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps)));
|
||||
return result;
|
||||
}
|
||||
|
||||
if (aPropSource instanceof EnumerablePropertySource<?>) {
|
||||
EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) aPropSource;
|
||||
Arrays.asList(ps.getPropertyNames())
|
||||
.forEach(key -> result.put(key, ps.getProperty(key)));
|
||||
return result;
|
||||
}
|
||||
|
||||
// note: Most descendants of PropertySource are EnumerablePropertySource. There are some
|
||||
// few others like JndiPropertySource or StubPropertySource
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void addAll(Map<String, Object> aBase, Map<String, Object> aToBeAdded) {
|
||||
for (Map.Entry<String, Object> entry : aToBeAdded.entrySet()) {
|
||||
if (aBase.containsKey(entry.getKey())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
aBase.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package cn.lihongjie.coal.spring.config;
|
||||
|
||||
import cn.lihongjie.coal.common.SpringUtils;
|
||||
|
||||
import org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy;
|
||||
import org.hibernate.boot.model.naming.Identifier;
|
||||
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
|
||||
@@ -9,6 +11,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -17,6 +21,8 @@ public class HibernateConfig {
|
||||
|
||||
@Autowired MyRedissonRegionFactory myRedissonRegionFactory;
|
||||
|
||||
@Autowired Environment environment;
|
||||
|
||||
@Bean
|
||||
HibernatePropertiesCustomizer hibernatePropertiesCustomizer() {
|
||||
return new HibernatePropertiesCustomizer() {
|
||||
@@ -28,23 +34,14 @@ public class HibernateConfig {
|
||||
// value="org.redisson.hibernate.RedissonRegionFactory" />
|
||||
hibernateProperties.put(
|
||||
"hibernate.cache.region.factory_class", myRedissonRegionFactory);
|
||||
hibernateProperties.put("hibernate.cache.use_structured_entries", true);
|
||||
// hibernateProperties.put(AvailableSettings.SCHEMA_MANAGEMENT_TOOL,
|
||||
// MultithreadHibernateSchemaManagementTool.class.getCanonicalName());
|
||||
|
||||
/**
|
||||
*
|
||||
* <!-- 2nd level cache activation -->
|
||||
* <property name="hibernate.cache.use_second_level_cache" value="true" /> <property
|
||||
* name="hibernate.cache.use_query_cache" value="true" />
|
||||
* <!-- Redisson can fallback on database if Redis cache is unavailable -->
|
||||
* <property name="hibernate.cache.redisson.fallback" value="true" />
|
||||
*/
|
||||
hibernateProperties.put("hibernate.cache.use_second_level_cache", "true");
|
||||
hibernateProperties.put("hibernate.cache.auto_evict_collection_cache", "true");
|
||||
|
||||
hibernateProperties.put("hibernate.cache.use_query_cache", "false");
|
||||
hibernateProperties.put("hibernate.cache.redisson.fallback", "true");
|
||||
Map<String, Object> propertiesStartingWith =
|
||||
SpringUtils.getPropertiesStartingWith(
|
||||
(ConfigurableEnvironment) environment, "hibernate.");
|
||||
propertiesStartingWith.forEach(
|
||||
(key, value) -> {
|
||||
hibernateProperties.put(key, value);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -15,6 +15,14 @@ system:
|
||||
- "/actuator/**"
|
||||
test-admin-token: "11111111222222"
|
||||
|
||||
hibernate:
|
||||
cache:
|
||||
redisson:
|
||||
fallback: true
|
||||
use_second_level_cache: false
|
||||
auto_evict_collection_cache: false
|
||||
use_query_cache: false
|
||||
use_structured_entries: true
|
||||
|
||||
|
||||
management:
|
||||
|
||||
Reference in New Issue
Block a user