ソースを参照

CglibProxyFactory 之中的缓存代码抽取到 InterceptorCache

James 2 年 前
コミット
a4a848723a

+ 4 - 5
src/main/java/com/jfinal/ext/proxy/CglibCallback.java

@@ -22,8 +22,7 @@ import java.util.Set;
 import com.jfinal.aop.Interceptor;
 import com.jfinal.aop.InterceptorManager;
 import com.jfinal.aop.Invocation;
-import com.jfinal.ext.proxy.CglibProxyFactory.IntersCache;
-import com.jfinal.ext.proxy.CglibProxyFactory.MethodKey;
+import com.jfinal.ext.proxy.InterceptorCache.MethodKey;
 import net.sf.cglib.proxy.MethodInterceptor;
 import net.sf.cglib.proxy.MethodProxy;
 
@@ -46,11 +45,11 @@ class CglibCallback implements MethodInterceptor {
 		}
 		
 		
-		MethodKey key = IntersCache.getMethodKey(targetClass, method);
-		Interceptor[] inters = IntersCache.get(key);
+		MethodKey key = InterceptorCache.getMethodKey(targetClass, method);
+		Interceptor[] inters = InterceptorCache.get(key);
 		if (inters == null) {
 			inters = interMan.buildServiceMethodInterceptor(targetClass, method);
-			IntersCache.put(key, inters);
+			InterceptorCache.put(key, inters);
 		}
 		
 		Invocation invocation = new Invocation(target, method, inters,

+ 0 - 63
src/main/java/com/jfinal/ext/proxy/CglibProxyFactory.java

@@ -16,12 +16,6 @@
 
 package com.jfinal.ext.proxy;
 
-import java.lang.reflect.Method;
-import java.util.Map;
-import java.util.Objects;
-import com.jfinal.aop.Interceptor;
-import com.jfinal.kit.HashKit;
-import com.jfinal.kit.SyncWriteMap;
 import com.jfinal.proxy.ProxyFactory;
 
 /**
@@ -40,63 +34,6 @@ public class CglibProxyFactory extends ProxyFactory {
 	public <T> T get(Class<T> target) {
 		return (T)net.sf.cglib.proxy.Enhancer.create(target, new CglibCallback());
 	}
-	
-	static class IntersCache {
-		private static final Map<MethodKey, Interceptor[]> cache = new SyncWriteMap<>(2048, 0.25F);
-		
-		public static void put(MethodKey methodKey, Interceptor[] inters) {
-			Objects.requireNonNull(methodKey, "methodKey can not be null");
-			Objects.requireNonNull(inters, "inters can not be null");
-			
-			cache.putIfAbsent(methodKey, inters);
-		}
-		
-		public static Interceptor[] get(MethodKey methodKey) {
-			return cache.get(methodKey);
-		}
-		
-		public static MethodKey getMethodKey(Class<?> target, Method method) {
-			long paraHash = HashKit.FNV_OFFSET_BASIS_64;
-			Class<?>[] paraTypes = method.getParameterTypes();
-			for (Class<?> pt : paraTypes) {
-				paraHash ^= pt.getName().hashCode();
-				paraHash *= HashKit.FNV_PRIME_64;
-			}
-			
-			return new MethodKey(target.getName().hashCode(), method.getName().hashCode(), paraHash);
-		}
-	}
-	
-	static class MethodKey {
-		final int classHash;
-		final int methodHash;
-		final long paraHash;
-		
-		MethodKey(int classHash, int methodHash, long paraHash) {
-			this.classHash = classHash;
-			this.methodHash = methodHash;
-			this.paraHash = paraHash;
-		}
-		
-		public int hashCode() {
-			return classHash ^ methodHash ^ ((int)paraHash);
-		}
-		
-		/**
-		 * 通过比较三部分 hash 值,避免超大规模场景下可能的 key 值碰撞
-		 * 
-		 * 不必判断 if (methodKey instanceof MethodKey),因为所有 key 类型必须要相同
-		 * 不必判断 if (this == methodKey),因为每次用于取值的 methodKey 都是新建的
-		 */
-		public boolean equals(Object methodKey) {
-			MethodKey mk = (MethodKey)methodKey;
-			return classHash == mk.classHash && methodHash == mk.methodHash && paraHash == mk.paraHash;
-		}
-		
-		public String toString() {
-			return "classHash = " + classHash + "\nmethodHash = " + methodHash + "\nparaHash = " + paraHash;
-		}
-	}
 }