@Cached.key与方法参数数的校验

This commit is contained in:
redkale
2024-05-29 09:44:46 +08:00
parent bf6213aca1
commit ec9a0bbaf0
2 changed files with 18 additions and 3 deletions

View File

@@ -66,6 +66,9 @@ public class CacheAction {
// 缓存的hash
private String hash;
// 模板key
String templetKey;
// 缓存的key
private MultiHashKey dynKey;
@@ -90,6 +93,7 @@ public class CacheAction {
this.paramNames = paramNames;
this.methodName = Objects.requireNonNull(methodName);
this.fieldName = Objects.requireNonNull(fieldName);
this.templetKey = cached.getKey();
this.async = CompletableFuture.class.isAssignableFrom(TypeToken.typeToClass(returnType));
this.resultType = this.async ? ((ParameterizedType) returnType).getActualTypeArguments()[0] : returnType;
}
@@ -99,6 +103,7 @@ public class CacheAction {
? CacheManager.DEFAULT_HASH
: environment.getPropertyValue(cached.getHash());
String key = environment.getPropertyValue(cached.getKey());
this.templetKey = key;
this.dynKey = MultiHashKey.create(paramNames, key);
this.localExpire = createDuration(cached.getLocalExpire());
this.remoteExpire = createDuration(cached.getRemoteExpire());

View File

@@ -3,8 +3,6 @@
*/
package org.redkale.cache.spi;
import static org.redkale.asm.Opcodes.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@@ -13,6 +11,8 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.redkale.asm.AnnotationVisitor;
import org.redkale.asm.AsmMethodBean;
import org.redkale.asm.AsmMethodBoost;
@@ -23,6 +23,7 @@ import org.redkale.asm.Handle;
import org.redkale.asm.Label;
import org.redkale.asm.MethodVisitor;
import org.redkale.asm.Opcodes;
import static org.redkale.asm.Opcodes.*;
import org.redkale.asm.Type;
import org.redkale.cache.Cached;
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 final Logger logger = Logger.getLogger(getClass().getSimpleName());
private Map<String, CacheAction> actionMap;
public CacheAsmMethodBoost(boolean remote, Class serviceType) {
@@ -227,6 +230,14 @@ public class CacheAsmMethodBoost extends AsmMethodBoost {
methodBean.fieldNameArray(),
method.getName(),
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);
}
}
@@ -236,7 +247,6 @@ public class CacheAsmMethodBoost extends AsmMethodBoost {
Field c = clazz.getDeclaredField(field);
c.setAccessible(true);
resourceFactory.inject(action);
action.init();
c.set(service, action);
RedkaleClassLoader.putReflectionField(clazz.getName(), c);
} catch (Exception e) {