From 8b56bbc50b90cb0e8d6c29da6eeb5780b6dd3ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A5=8A=E6=85=B6=E5=A0=82?= Date: Mon, 8 Oct 2018 19:02:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=B8=8A=20synchronized=20=E6=B8=9B?= =?UTF-8?q?=E5=B0=91=E8=B3=87=E6=96=99=E5=BA=AB=E6=9F=A5=E8=A9=A2=E6=AC=A1?= =?UTF-8?q?=E6=95=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hibernate/I18NReadEventListener.java | 63 +++++++++---------- 1 file changed, 29 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/ylhealth/ym/springtest/hibernate/I18NReadEventListener.java b/src/main/java/org/ylhealth/ym/springtest/hibernate/I18NReadEventListener.java index cb68134..7d2970e 100644 --- a/src/main/java/org/ylhealth/ym/springtest/hibernate/I18NReadEventListener.java +++ b/src/main/java/org/ylhealth/ym/springtest/hibernate/I18NReadEventListener.java @@ -22,6 +22,8 @@ import org.slf4j.LoggerFactory; import org.springframework.context.i18n.LocaleContextHolder; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import com.google.common.collect.Interner; +import com.google.common.collect.Interners; /** */ public class I18NReadEventListener implements PostLoadEventListener { @@ -32,6 +34,8 @@ public class I18NReadEventListener implements PostLoadEventListener { private SessionFactoryImplementor sessionFactory; + private Interner pool = Interners.newWeakInterner(); + @SuppressWarnings("rawtypes") private Cache cache = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES).build(); @@ -80,7 +84,7 @@ public class I18NReadEventListener implements PostLoadEventListener { * @throws InvocationTargetException */ private Map getTranslate( - StatelessSession session, I18NTranslates i18n, Object entity, Class clazz) + StatelessSession session, I18NTranslates i18n, Object entity, @SuppressWarnings("rawtypes") Class clazz) throws IllegalAccessException, InvocationTargetException { Object id = getPKValue(clazz, entity); String language = LocaleContextHolder.getLocale().toString(); @@ -93,6 +97,7 @@ public class I18NReadEventListener implements PostLoadEventListener { return translateTable.get(id); } + @SuppressWarnings("unchecked") private Map> translateTable( StatelessSession session, String table, @@ -102,25 +107,33 @@ public class I18NReadEventListener implements PostLoadEventListener { String key = "Table:" + table + "_" + language; Map> result = cache.getIfPresent(key); if (result == null) { - logger.trace("translate query, table: {}", table); - String sql = String.format("select * from %s where languageCode = ?", table); - SQLQuery query1 = session.createSQLQuery(sql); - query1.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); - query1.addScalar(idColumn); - for (I18NTranslate i18nTranslate : mapping) { - query1.addScalar(i18nTranslate.column()); - } - query1.setParameter(0, language); - List> result1 = query1.list(); - result = new HashMap<>(); - for (Map map : result1) { - result.put(map.get(idColumn), map); + synchronized (pool.intern(key)) { + result = cache.getIfPresent(key); + if (result == null) { + logger.trace("translate query, table: {}", table); + String sql = String.format("select * from %s where languageCode = ?", table); + SQLQuery query1 = session.createSQLQuery(sql); + query1.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); + query1.addScalar(idColumn); + query1.addScalar("languageCode"); + for (I18NTranslate i18nTranslate : mapping) { + query1.addScalar(i18nTranslate.column()); + } + query1.setParameter(0, language); + List> result1 = query1.list(); + result = new HashMap<>(); + for (Map map : result1) { + map.remove("languageCode"); + result.put(map.get(idColumn), map); + } + cache.put(key, result); + } } - cache.put(key, result); } return result; } + private Object getPKValue(Class clazz, Object entity) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Collection fields = getAllDeclaredFields(clazz); @@ -132,29 +145,11 @@ public class I18NReadEventListener implements PostLoadEventListener { } for (Method method : clazz.getDeclaredMethods()) { if (method.getAnnotation(Id.class) != null) { - return method.invoke(entity, null); + return method.invoke(entity); } } return null; } - /** - * Returns the entity's @{@link I18NTranslate} fields. - * - *

These fields are made accessible. - */ - private static Collection getLocalizedFields(Class clazz) { - Collection fields = getAllDeclaredFields(clazz); - List localizedFields = new ArrayList<>(); - fields - .stream() - .filter(field -> field.getAnnotation(I18NTranslate.class) != null) - .forEach( - field -> { - field.setAccessible(true); - localizedFields.add(field); - }); - return localizedFields; - } private static Collection getAllDeclaredFields(Class clazz) { List fields = new ArrayList<>(); -- 2.26.2