yujian
2020-05-09 7e7db2fa55a9a3af46d4fd8ede0dee147f101d64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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() {
 
    }
}