@Cached.key与方法参数数的校验
This commit is contained in:
@@ -66,6 +66,9 @@ public class CacheAction {
|
|||||||
// 缓存的hash
|
// 缓存的hash
|
||||||
private String hash;
|
private String hash;
|
||||||
|
|
||||||
|
// 模板key
|
||||||
|
String templetKey;
|
||||||
|
|
||||||
// 缓存的key
|
// 缓存的key
|
||||||
private MultiHashKey dynKey;
|
private MultiHashKey dynKey;
|
||||||
|
|
||||||
@@ -90,6 +93,7 @@ public class CacheAction {
|
|||||||
this.paramNames = paramNames;
|
this.paramNames = paramNames;
|
||||||
this.methodName = Objects.requireNonNull(methodName);
|
this.methodName = Objects.requireNonNull(methodName);
|
||||||
this.fieldName = Objects.requireNonNull(fieldName);
|
this.fieldName = Objects.requireNonNull(fieldName);
|
||||||
|
this.templetKey = cached.getKey();
|
||||||
this.async = CompletableFuture.class.isAssignableFrom(TypeToken.typeToClass(returnType));
|
this.async = CompletableFuture.class.isAssignableFrom(TypeToken.typeToClass(returnType));
|
||||||
this.resultType = this.async ? ((ParameterizedType) returnType).getActualTypeArguments()[0] : returnType;
|
this.resultType = this.async ? ((ParameterizedType) returnType).getActualTypeArguments()[0] : returnType;
|
||||||
}
|
}
|
||||||
@@ -99,6 +103,7 @@ public class CacheAction {
|
|||||||
? CacheManager.DEFAULT_HASH
|
? CacheManager.DEFAULT_HASH
|
||||||
: environment.getPropertyValue(cached.getHash());
|
: environment.getPropertyValue(cached.getHash());
|
||||||
String key = environment.getPropertyValue(cached.getKey());
|
String key = environment.getPropertyValue(cached.getKey());
|
||||||
|
this.templetKey = key;
|
||||||
this.dynKey = MultiHashKey.create(paramNames, key);
|
this.dynKey = MultiHashKey.create(paramNames, key);
|
||||||
this.localExpire = createDuration(cached.getLocalExpire());
|
this.localExpire = createDuration(cached.getLocalExpire());
|
||||||
this.remoteExpire = createDuration(cached.getRemoteExpire());
|
this.remoteExpire = createDuration(cached.getRemoteExpire());
|
||||||
|
|||||||
@@ -3,8 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.cache.spi;
|
package org.redkale.cache.spi;
|
||||||
|
|
||||||
import static org.redkale.asm.Opcodes.*;
|
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@@ -13,6 +11,8 @@ import java.util.LinkedHashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
import org.redkale.asm.AnnotationVisitor;
|
import org.redkale.asm.AnnotationVisitor;
|
||||||
import org.redkale.asm.AsmMethodBean;
|
import org.redkale.asm.AsmMethodBean;
|
||||||
import org.redkale.asm.AsmMethodBoost;
|
import org.redkale.asm.AsmMethodBoost;
|
||||||
@@ -23,6 +23,7 @@ import org.redkale.asm.Handle;
|
|||||||
import org.redkale.asm.Label;
|
import org.redkale.asm.Label;
|
||||||
import org.redkale.asm.MethodVisitor;
|
import org.redkale.asm.MethodVisitor;
|
||||||
import org.redkale.asm.Opcodes;
|
import org.redkale.asm.Opcodes;
|
||||||
|
import static org.redkale.asm.Opcodes.*;
|
||||||
import org.redkale.asm.Type;
|
import org.redkale.asm.Type;
|
||||||
import org.redkale.cache.Cached;
|
import org.redkale.cache.Cached;
|
||||||
import org.redkale.inject.ResourceFactory;
|
import org.redkale.inject.ResourceFactory;
|
||||||
@@ -39,6 +40,8 @@ public class CacheAsmMethodBoost extends AsmMethodBoost {
|
|||||||
|
|
||||||
private static final List<Class<? extends Annotation>> FILTER_ANN = List.of(Cached.class, DynForCache.class);
|
private static final List<Class<? extends Annotation>> FILTER_ANN = List.of(Cached.class, DynForCache.class);
|
||||||
|
|
||||||
|
private final Logger logger = Logger.getLogger(getClass().getSimpleName());
|
||||||
|
|
||||||
private Map<String, CacheAction> actionMap;
|
private Map<String, CacheAction> actionMap;
|
||||||
|
|
||||||
public CacheAsmMethodBoost(boolean remote, Class serviceType) {
|
public CacheAsmMethodBoost(boolean remote, Class serviceType) {
|
||||||
@@ -227,6 +230,14 @@ public class CacheAsmMethodBoost extends AsmMethodBoost {
|
|||||||
methodBean.fieldNameArray(),
|
methodBean.fieldNameArray(),
|
||||||
method.getName(),
|
method.getName(),
|
||||||
dynFieldName);
|
dynFieldName);
|
||||||
|
action.init();
|
||||||
|
if (action.templetKey.indexOf('{') < 0 && method.getParameterCount() > 0) {
|
||||||
|
// 一般有参数的方法,Cached.key应该是动态的
|
||||||
|
logger.log(
|
||||||
|
Level.WARNING,
|
||||||
|
method + " has parameters but @" + Cached.class.getSimpleName()
|
||||||
|
+ ".key not contains parameter");
|
||||||
|
}
|
||||||
actionMap.put(dynFieldName, action);
|
actionMap.put(dynFieldName, action);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -236,7 +247,6 @@ public class CacheAsmMethodBoost extends AsmMethodBoost {
|
|||||||
Field c = clazz.getDeclaredField(field);
|
Field c = clazz.getDeclaredField(field);
|
||||||
c.setAccessible(true);
|
c.setAccessible(true);
|
||||||
resourceFactory.inject(action);
|
resourceFactory.inject(action);
|
||||||
action.init();
|
|
||||||
c.set(service, action);
|
c.set(service, action);
|
||||||
RedkaleClassLoader.putReflectionField(clazz.getName(), c);
|
RedkaleClassLoader.putReflectionField(clazz.getName(), c);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user