Browse Source

重构部分方法, ColumnCache 缓存的装配改为懒加载;增加注释

hcl 6 years ago
parent
commit
ed3d78020f

+ 27 - 34
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtils.java

@@ -48,11 +48,13 @@ public final class LambdaUtils {
     private static final Map<Class<?>, WeakReference<SerializedLambda>> FUNC_CACHE = new ConcurrentHashMap<>();
 
     /**
-     * 解析 lambda 表达式
+     * 解析 lambda 表达式, 该方法只是调用了 {@link SerializedLambda#resolve(SFunction)} 中的方法,在此基础上加了缓存。
+     * 该缓存可能会在任意不定的时间被清除
      *
      * @param func 需要解析的 lambda 对象
      * @param <T>  类型,被调用的 Function 对象的目标类型
      * @return 返回解析后的结果
+     * @see SerializedLambda#resolve(SFunction)
      */
     public static <T> SerializedLambda resolve(SFunction<T, ?> func) {
         Class<?> clazz = func.getClass();
@@ -65,35 +67,6 @@ public final class LambdaUtils {
             });
     }
 
-    /**
-     * 将传入的表信息加入缓存
-     *
-     * @param tableInfo 表信息
-     */
-    public static void installCache(TableInfo tableInfo) {
-        LAMBDA_MAP.put(tableInfo.getClazz(), createColumnCacheMap(tableInfo));
-    }
-
-    /**
-     * 缓存实体字段 MAP 信息
-     *
-     * @param info 表信息
-     * @return 缓存 map
-     */
-    private static Map<String, ColumnCache> createColumnCacheMap(TableInfo info) {
-        Map<String, ColumnCache> map = new HashMap<>();
-
-        String kp = info.getKeyProperty();
-        if (null != kp) {
-            map.put(formatKey(kp), new ColumnCache(info.getKeyColumn(), info.getKeySqlSelect()));
-        }
-
-        info.getFieldList().forEach(i ->
-            map.put(formatKey(i.getProperty()), new ColumnCache(i.getColumn(), i.getSqlSelect(info.getDbType())))
-        );
-        return map;
-    }
-
     /**
      * 格式化 key 将传入的 key 变更为大写格式
      *
@@ -116,11 +89,31 @@ public final class LambdaUtils {
      * @return 如果存在,返回字段信息;否则返回 null
      */
     public static ColumnCache getColumnOfProperty(Class<?> clazz, String fieldName) {
-        Map<String, ColumnCache> map = LAMBDA_MAP.get(clazz);
-        if (null != map) {
-            return map.get(formatKey(fieldName));
+        Map<String, ColumnCache> map = LAMBDA_MAP.computeIfAbsent(clazz, c -> {
+            TableInfo ti = TableInfoHelper.getTableInfo(c);
+            return null == ti ? null : createColumnCacheMap(ti);
+        });
+        return null == map ? null : map.get(formatKey(fieldName));
+    }
+
+    /**
+     * 缓存实体字段 MAP 信息
+     *
+     * @param info 表信息
+     * @return 缓存 map
+     */
+    private static Map<String, ColumnCache> createColumnCacheMap(TableInfo info) {
+        Map<String, ColumnCache> map = new HashMap<>(info.getFieldList().size() + 1);
+
+        String kp = info.getKeyProperty();
+        if (null != kp) {
+            map.put(formatKey(kp), new ColumnCache(info.getKeyColumn(), info.getKeySqlSelect()));
         }
-        return null;
+
+        info.getFieldList().forEach(i ->
+            map.put(formatKey(i.getProperty()), new ColumnCache(i.getColumn(), i.getSqlSelect(info.getDbType())))
+        );
+        return map;
     }
 
 }

+ 0 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/TableInfoHelper.java

@@ -141,8 +141,6 @@ public class TableInfoHelper {
         /* 放入缓存 */
         TABLE_INFO_CACHE.put(clazz, tableInfo);
 
-        /* 缓存 Lambda 映射关系 */
-        LambdaUtils.installCache(tableInfo);
         return tableInfo;
     }