package com.ks.lucky.aspect;
|
|
import com.ks.lucky.manager.RedisManager;
|
import com.ks.lucky.util.annotation.RedisCache;
|
import com.ks.lucky.util.annotation.RedisCacheEvict;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.Signature;
|
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.reflect.MethodSignature;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.core.DefaultParameterNameDiscoverer;
|
import org.springframework.core.annotation.Order;
|
import org.springframework.expression.EvaluationContext;
|
import org.springframework.expression.Expression;
|
import org.springframework.expression.ExpressionParser;
|
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
import org.springframework.stereotype.Component;
|
import org.yeshi.utils.StringUtil;
|
|
import javax.annotation.Resource;
|
import java.lang.reflect.Method;
|
|
@Component
|
@Aspect
|
@Order(4)
|
public class RedisCacheAspect {
|
Logger logger = LoggerFactory.getLogger(RedisCacheAspect.class);
|
@Resource
|
private RedisManager redisManager;
|
|
|
private ExpressionParser parser = new SpelExpressionParser();
|
|
private DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();
|
|
/**
|
* 获取表达的值
|
*
|
* @param spELString
|
* @param joinPoint
|
* @return
|
*/
|
public String generateKeyBySpEL(String spELString, ProceedingJoinPoint joinPoint) {
|
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
String[] paramNames = nameDiscoverer.getParameterNames(methodSignature.getMethod());
|
Expression expression = parser.parseExpression(spELString);
|
EvaluationContext context = new StandardEvaluationContext();
|
Object[] args = joinPoint.getArgs();
|
for (int i = 0; i < args.length; i++) {
|
context.setVariable(paramNames[i], args[i]);
|
}
|
return expression.getValue(context).toString();
|
}
|
|
private String getRedisKey(RedisCache cache, ProceedingJoinPoint joinPoint) {
|
return cache.cate() + "#" + generateKeyBySpEL(cache.key(), joinPoint);
|
}
|
|
private String getRedisKey(RedisCacheEvict cache, ProceedingJoinPoint joinPoint) {
|
String key = cache.key();
|
String keyPrefix = cache.keyPrefix();
|
String cacheKey = cache.cate() + "#" + (StringUtil.isNullOrEmpty(key) ? generateKeyBySpEL(keyPrefix, joinPoint) : generateKeyBySpEL(key, joinPoint));
|
return cacheKey;
|
}
|
|
@Around("execution(public * com.ks.lucky.service..*.*(..))")
|
public Object requestSerializable(ProceedingJoinPoint joinPoint) throws Throwable {
|
Signature signature = joinPoint.getSignature();
|
MethodSignature methodSignature = (MethodSignature) signature;
|
Method targetMethod = methodSignature.getMethod();
|
|
|
Method realMethod = null;
|
RedisCache redisCache = null;
|
RedisCacheEvict[] redisCacheEvicts = null;
|
try {
|
realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(joinPoint.getSignature().getName(),
|
targetMethod.getParameterTypes());
|
if (realMethod.isAnnotationPresent(RedisCache.class)) {
|
redisCache = realMethod.getAnnotation(RedisCache.class);
|
} else {
|
redisCacheEvicts = realMethod.getAnnotationsByType(RedisCacheEvict.class);
|
}
|
} catch (NoSuchMethodException e) {
|
e.printStackTrace();
|
} catch (SecurityException e) {
|
e.printStackTrace();
|
}
|
if (redisCache != null) {
|
String cacheKey = getRedisKey(redisCache, joinPoint);
|
if (!StringUtil.isNullOrEmpty(cacheKey)) {
|
try {
|
Object result = redisManager.getDataFromRedis(cacheKey);
|
if (result != null) {
|
return result;
|
}
|
} catch (Exception e) {
|
logger.error("redis获取缓存出错", e.getMessage(), "key:" + cacheKey);
|
}
|
}
|
}
|
|
Object object = joinPoint.proceed();
|
//不异常
|
if (realMethod != null) {
|
//不缓存空值
|
if (redisCache != null && object != null) {
|
String cacheKey = getRedisKey(redisCache, joinPoint);
|
if (!StringUtil.isNullOrEmpty(cacheKey)) {
|
try {
|
redisManager.setDataToRedis(cacheKey, object, redisCache.second());
|
} catch (Exception e) {
|
logger.error("redis保存缓存数据出错", e.getMessage(), "key:" + cacheKey);
|
}
|
}
|
} else if (redisCacheEvicts != null && redisCacheEvicts.length > 0) {
|
for (RedisCacheEvict redisCacheEvict : redisCacheEvicts) {
|
String key = redisCacheEvict.key();
|
String cacheKey = getRedisKey(redisCacheEvict, joinPoint);
|
if (!StringUtil.isNullOrEmpty(cacheKey)) {
|
try {
|
if (!StringUtil.isNullOrEmpty(key)) {
|
redisManager.remove(cacheKey);
|
} else {
|
redisManager.removePrefix(cacheKey);
|
}
|
} catch (Exception e) {
|
logger.error("redis清除缓存数据出错", e.getMessage(), "key:" + cacheKey);
|
}
|
}
|
}
|
}
|
}
|
return object;
|
|
}
|
|
}
|
|
class ParamsTypeValue {
|
Class<?> type;
|
Object value;
|
|
public ParamsTypeValue(Class<?> type, Object value) {
|
this.type = type;
|
this.value = value;
|
}
|
|
public ParamsTypeValue() {
|
|
}
|
}
|