SearchCacheAspect.java
5.1 KB
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
package com.yoho.search.aop.cache;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.MD5Util;
import com.yoho.search.cache.CacheType;
import com.yoho.search.cache.beans.SearchCacheFactory;
import com.yoho.search.cache.beans.SearchCacheService;
import com.yoho.search.cache.model.SearchCache;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Component
@Aspect
@Order(Integer.MAX_VALUE - 1)
public class SearchCacheAspect {
@Autowired
private SearchCacheService searchCacheService;
@Autowired
private SearchCacheFactory searchCacheFactory;
/**
* @目前参数只支持Map和HttpRequest!!!!!!!!!!!!!!!!!!!!!!!!!!!
* @param pjp
* @param <T>
* @return
* @throws Throwable
*/
@Around("@annotation(com.yoho.search.aop.cache.SearchCacheAble)")
public <T> T doCache(ProceedingJoinPoint pjp) throws Throwable {
MethodSignature signature = (MethodSignature) pjp.getSignature();
// 1、检查注解
SearchCacheAble searchCacheAble = signature.getMethod().getAnnotation(SearchCacheAble.class);
if (searchCacheAble == null) {
return (T) pjp.proceed();
}
// 2、获取Type的返回类型
Type type = signature.getMethod().getGenericReturnType();
// 3、检查cacheKey
RedisKeyBuilder redisKeyBuilder = this.getRedisKeyBuilder(pjp, searchCacheAble);
if (StringUtils.isBlank(redisKeyBuilder.getKey())) {
return (T) pjp.proceed();
}
// 4、缓存获取
SearchCache searchCache = searchCacheFactory.getAspectSearhCache(searchCacheAble);
boolean useJsonSerializable = searchCacheAble.cacheType().equals(CacheType.EHCACHE) ? false : true;
T result = searchCacheService.getSerializableObjectFromCache(searchCache, redisKeyBuilder, type, useJsonSerializable);
if (result != null) {
return result;
}
//5、执行
result = (T) pjp.proceed();
if (result == null) {
return result;
}
if (result instanceof SearchApiResult && ((SearchApiResult) result).getCode() != 200) {
return result;
}
//6、加入缓存
searchCacheService.addSerializableObjectToCache(searchCache, redisKeyBuilder, result, useJsonSerializable);
return result;
}
// 获取请求的参数
private RedisKeyBuilder getRedisKeyBuilder(ProceedingJoinPoint pjp, SearchCacheAble searchCacheAble) {
Object[] arges = pjp.getArgs();
Object object = arges[0];
if (object instanceof HttpServletRequest) {
return this.getRedisKeyBuilder((HttpServletRequest) object, searchCacheAble);
}
if (object instanceof Map) {
return this.getRedisKeyBuilder((Map<?, ?>) object, searchCacheAble);
}
return null;
}
// 获取httpServletRequest的缓存key
private RedisKeyBuilder getRedisKeyBuilder(HttpServletRequest httpServletRequest, SearchCacheAble searchCacheAble) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(httpServletRequest);
return this.getRedisKeyBuilder(paramMap, searchCacheAble);
}
// 获取请求的参数
private RedisKeyBuilder getRedisKeyBuilder(Map<?, ?> paramMap, SearchCacheAble searchCacheAble) {
List<String> includeParams = Arrays.asList(searchCacheAble.includeParams());
List<String> excludeParams = Arrays.asList(searchCacheAble.excludeParams());
String paramKey = "";
if (!includeParams.isEmpty()) {
paramKey = HttpServletRequestUtils.genParamStringWithIncludeParams(paramMap, includeParams);
} else if (!excludeParams.isEmpty()) {
paramKey = HttpServletRequestUtils.genParamStringWithExcludeParams(paramMap, excludeParams);
} else {
paramKey = HttpServletRequestUtils.genParamString(paramMap);
}
return this.getRedisKeyBuilderByParamKey(paramKey, searchCacheAble);
}
private RedisKeyBuilder getRedisKeyBuilderByParamKey(String paramKey, SearchCacheAble searchCacheAble) {
RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance();
redisKeyBuilder.appendFixed("YOHOSEARCH:AOP:NEW:");
redisKeyBuilder.appendFixed(searchCacheAble.cacheName()).appendFixed(":");
redisKeyBuilder.appendVar(MD5Util.string2MD5(paramKey));
return redisKeyBuilder;
}
}