package com.yeshi.fanli.aspect;
|
|
import java.io.IOException;
|
import java.io.PrintWriter;
|
import java.lang.reflect.Field;
|
import java.lang.reflect.Method;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
import javax.annotation.Resource;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.Signature;
|
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.reflect.MethodSignature;
|
import org.springframework.cache.Cache;
|
import org.springframework.cache.Cache.ValueWrapper;
|
import org.springframework.cache.ehcache.EhCacheCacheManager;
|
|
import com.yeshi.fanli.log.LogHelper;
|
import com.yeshi.fanli.util.StringUtil;
|
import com.yeshi.fanli.util.annotation.RedisCache;
|
|
//@Component
|
//@Aspect
|
//@Order(4)
|
public class RedisCacheAspect {
|
@Resource
|
private EhCacheCacheManager cacheManager;
|
|
@Around("execution(public * com.yeshi.xcx.bainian.controller.api.client.*.*(..))")
|
public Object requestSerializable(ProceedingJoinPoint joinPoint) throws IOException {
|
Signature signature = joinPoint.getSignature();
|
MethodSignature methodSignature = (MethodSignature) signature;
|
Method targetMethod = methodSignature.getMethod();
|
String cacheKey = null;
|
try {
|
Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(joinPoint.getSignature().getName(),
|
targetMethod.getParameterTypes());
|
|
if (realMethod.isAnnotationPresent(RedisCache.class)) {
|
|
RedisCache rs = realMethod.getAnnotation(RedisCache.class);
|
String[] strings = methodSignature.getParameterNames();
|
Class<?>[] types = methodSignature.getParameterTypes();
|
Map<String, ParamsTypeValue> map = new HashMap<>();
|
Object[] args = joinPoint.getArgs();
|
PrintWriter out = null;
|
for (int i = 0; i < strings.length; i++) {
|
map.put(strings[i], new ParamsTypeValue(types[i], args[i]));
|
if (args[i] instanceof PrintWriter) {
|
out = (PrintWriter) args[i];
|
}
|
}
|
String key = rs.key();
|
ParamsTypeValue tv = null;
|
if (key.indexOf(".") > -1) {
|
tv = map.get(key.split("\\.")[0]);
|
} else {
|
tv = map.get(key);
|
|
}
|
Object value = tv.value;
|
Class<?> type = tv.type;
|
// 暂时只支持2层嵌套
|
if (key.indexOf(".") > -1) {
|
String child = key.split("\\.")[1];
|
try {
|
Field f = type.getDeclaredField(child);
|
f.setAccessible(true);
|
Object obj = f.get(value);
|
if (obj != null) {
|
cacheKey = obj.toString();
|
}
|
} catch (NoSuchFieldException e) {
|
e.printStackTrace();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
} else {
|
if (value != null) {
|
cacheKey = value.toString();
|
}
|
}
|
|
if (!StringUtil.isNullOrEmpty(cacheKey)) {
|
cacheKey = joinPoint.getTarget().getClass().getName() + "." + targetMethod.getName() + "-"
|
+ cacheKey;
|
Cache cache = cacheManager.getCache("rsCache");
|
ValueWrapper element = cache.get(cacheKey);
|
if (element != null && element.get() != null) {
|
|
return null;
|
} else {
|
cache.put(cacheKey, "1");
|
}
|
}
|
}
|
|
} catch (NoSuchMethodException e) {
|
e.printStackTrace();
|
} catch (SecurityException e) {
|
e.printStackTrace();
|
}
|
|
try {
|
return joinPoint.proceed();
|
} catch (Throwable e) {
|
LogHelper.errorDetailInfo(e);
|
|
} finally {
|
if (!StringUtil.isNullOrEmpty(cacheKey)) {
|
Cache cache = cacheManager.getCache("rsCache");
|
cache.put(cacheKey, null);
|
}
|
}
|
return null;
|
}
|
|
}
|
|
class ParamsTypeValue {
|
Class<?> type;
|
Object value;
|
|
public ParamsTypeValue(Class<?> type, Object value) {
|
this.type = type;
|
this.value = value;
|
}
|
|
public ParamsTypeValue() {
|
|
}
|
}
|