From ec9a0bbaf0a3a82eb0c321c01dbc00872fc272f8 Mon Sep 17 00:00:00 2001 From: redkale Date: Wed, 29 May 2024 09:44:46 +0800 Subject: [PATCH] =?UTF-8?q?@Cached.key=E4=B8=8E=E6=96=B9=E6=B3=95=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E6=95=B0=E7=9A=84=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/redkale/cache/spi/CacheAction.java | 5 +++++ .../redkale/cache/spi/CacheAsmMethodBoost.java | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/redkale/cache/spi/CacheAction.java b/src/main/java/org/redkale/cache/spi/CacheAction.java index 8854e8ca4..f936d5db5 100644 --- a/src/main/java/org/redkale/cache/spi/CacheAction.java +++ b/src/main/java/org/redkale/cache/spi/CacheAction.java @@ -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()); diff --git a/src/main/java/org/redkale/cache/spi/CacheAsmMethodBoost.java b/src/main/java/org/redkale/cache/spi/CacheAsmMethodBoost.java index 94534647f..55613647a 100644 --- a/src/main/java/org/redkale/cache/spi/CacheAsmMethodBoost.java +++ b/src/main/java/org/redkale/cache/spi/CacheAsmMethodBoost.java @@ -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> FILTER_ANN = List.of(Cached.class, DynForCache.class); + private final Logger logger = Logger.getLogger(getClass().getSimpleName()); + private Map 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) {