|
|
package com.tianbo.warehouse.util.redis;
|
|
|
package com.tianbo.warehouse.annotation.cache.util.redis;
|
|
|
|
|
|
import com.alibaba.druid.util.StringUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
...
|
...
|
@@ -31,6 +31,7 @@ public class DefaultKeyGenerator { |
|
|
}
|
|
|
|
|
|
Signature signature = pjp.getSignature();
|
|
|
|
|
|
if(cacheNames == null || cacheNames.length == 0){
|
|
|
cacheNames = new String[]{signature.getDeclaringTypeName() + "." + signature.getName()};
|
|
|
}
|
...
|
...
|
@@ -40,18 +41,91 @@ public class DefaultKeyGenerator { |
|
|
if (!(signature instanceof MethodSignature)) {
|
|
|
throw new IllegalArgumentException("This annotation can only be used for methods...");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 缓存KEY 的命名规范
|
|
|
* 类名+方法名+key名称+参数1名+参数1值+。。。
|
|
|
* (加上key名称是为了防止方法名重复)
|
|
|
* 开始生成缓存key名称
|
|
|
*/
|
|
|
MethodSignature methodSignature = (MethodSignature) signature;
|
|
|
String targetClassName = pjp.getTarget().getClass().getName();
|
|
|
String targetMethodName = methodSignature.getName();
|
|
|
//method参数列表
|
|
|
String[] parameterNames = methodSignature.getParameterNames();
|
|
|
Object[] args = pjp.getArgs();
|
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
//evaluationContext存储参数的kEY与Value
|
|
|
for(int i = 0; i < parameterNames.length; i++){
|
|
|
String parameterName = parameterNames[i];
|
|
|
sb.append(parameterName).append("_").append("Value").append("_").append(args[i]);
|
|
|
log.info("=============>>>generateKeys :{}",sb.toString());
|
|
|
evaluationContext.setVariable(parameterName, args[i]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 缓存key名称生成结束
|
|
|
*/
|
|
|
|
|
|
//通过cachekey的值,从evaluationContext中根据cachekey的值获取其value
|
|
|
for(int j = 0; j < cacheNames.length; j++){
|
|
|
results[j] = "RedisKey_CacheName_" + cacheNames[j] + "_CacheKey_" +
|
|
|
new SpelExpressionParser().parseExpression(cacheKey).getValue(evaluationContext, String.class);//暂时只使用String类型
|
|
|
results[j] = sb.insert(0,cacheNames).toString();
|
|
|
// results[j] = "RedisKey_CacheName_" + cacheNames[j] + "_CacheKey_" +
|
|
|
// new SpelExpressionParser().parseExpression(cacheKey).getValue(evaluationContext, String.class);//暂时只使用String类型
|
|
|
}
|
|
|
log.info("=============>>>generateKeys : " + Arrays.toString(results));
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
|
|
|
public String[] generateKey(ProceedingJoinPoint pjp,String cacheKey) throws NoSuchMethodException {
|
|
|
if (StringUtils.isEmpty(cacheKey)) {
|
|
|
throw new NullPointerException("CacheKey can not be null...");
|
|
|
}
|
|
|
|
|
|
Signature signature = pjp.getSignature();
|
|
|
|
|
|
EvaluationContext evaluationContext = new StandardEvaluationContext();
|
|
|
if (!(signature instanceof MethodSignature)) {
|
|
|
throw new IllegalArgumentException("This annotation can only be used for methods...");
|
|
|
}
|
|
|
String[] results = new String[1];
|
|
|
/**
|
|
|
* 缓存KEY 的命名规范
|
|
|
* 类名+方法名+key名称+参数1名+参数1值+。。。
|
|
|
* (加上key名称是为了防止方法名重复)
|
|
|
* 开始生成缓存key名称
|
|
|
*/
|
|
|
MethodSignature methodSignature = (MethodSignature) signature;
|
|
|
String targetClassName = pjp.getTarget().getClass().getName();
|
|
|
String targetMethodName = methodSignature.getName();
|
|
|
//method参数列表
|
|
|
String[] parameterNames = methodSignature.getParameterNames();
|
|
|
Object[] args = pjp.getArgs();
|
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
sb.append(targetClassName).append("_")
|
|
|
.append(targetMethodName).append("_")
|
|
|
.append(cacheKey).append("_");
|
|
|
//evaluationContext存储参数的kEY与Value
|
|
|
for(int i = 0; i < parameterNames.length; i++){
|
|
|
String parameterName = parameterNames[i];
|
|
|
sb.append("P").append(parameterName).append("_")
|
|
|
.append("V").append(args[i]);
|
|
|
evaluationContext.setVariable(parameterName,args[i]);
|
|
|
}
|
|
|
|
|
|
results[0] = sb.toString();
|
|
|
//通过cachekey的值,从evaluationContext中根据cachekey的值获取其value,
|
|
|
//类名+方法名+key名称+参数1名+参数1值+。。。
|
|
|
|
|
|
// new SpelExpressionParser().parseExpression(cacheKey).getValue(evaluationContext, String.class);//暂时只使用String类型
|
|
|
|
|
|
/**
|
|
|
* 缓存key名称生成结束
|
|
|
* com.tianbo.warehouse.service.imp.PermissionServiceImp_findAll_findAllMenus_PpageNum_V1PpageSize_V500Pname_V
|
|
|
*/
|
|
|
log.info("=============>>>generateKeys : " + Arrays.toString(results));
|
|
|
return results;
|
|
|
}
|
...
|
...
|
|