|
|
package com.yoho.search.common.cache.aop;
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
...
|
...
|
@@ -66,32 +67,49 @@ public class SearhCacheAspect { |
|
|
return cacheKey;
|
|
|
}
|
|
|
|
|
|
public Class<? extends Serializable> getReturnClass(SearchCacheAble searchCacheAble,MethodSignature signature){
|
|
|
final Class<?> returnType = signature.getMethod().getReturnType();
|
|
|
final Class<? extends Serializable> returnClass = searchCacheAble.returnClass();
|
|
|
if(!returnType.getName().equals(returnClass.getName())){
|
|
|
return null;
|
|
|
}
|
|
|
return returnClass;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Around("@annotation(com.yoho.search.common.cache.aop.SearchCacheAble)")
|
|
|
public Object doCache(ProceedingJoinPoint pjp) throws Throwable {
|
|
|
MethodSignature signature = (MethodSignature) pjp.getSignature();
|
|
|
final Class<?> returnType = signature.getMethod().getReturnType();
|
|
|
//检查注解
|
|
|
SearchCacheAble searchCacheAble = signature.getMethod().getAnnotation(SearchCacheAble.class);
|
|
|
if (searchCacheAble == null || !returnType.isAssignableFrom(SearchApiResult.class)) {
|
|
|
if (searchCacheAble == null){
|
|
|
return pjp.proceed();
|
|
|
}
|
|
|
//检查返回类型
|
|
|
Class<? extends Serializable> returnClass = this.getReturnClass(searchCacheAble, signature);
|
|
|
if(returnClass==null){
|
|
|
return pjp.proceed();
|
|
|
}
|
|
|
//检查cacheKey
|
|
|
String cacheKey = this.getCacheKey(pjp, searchCacheAble);
|
|
|
if (StringUtils.isBlank(cacheKey)) {
|
|
|
return pjp.proceed();
|
|
|
}
|
|
|
SearchCache searchCache = searchCacheFactory.getAspectSearhCache(searchCacheAble);
|
|
|
SearchApiResult searchApiResult = searchCacheService.getSearchApiResultFromCache(searchCache, cacheKey);
|
|
|
if (searchApiResult != null) {
|
|
|
return searchApiResult;
|
|
|
Serializable cacheObject = searchCacheService.getSerializableObjectFromCache(searchCache, cacheKey,returnClass);
|
|
|
if (cacheObject != null) {
|
|
|
return cacheObject;
|
|
|
}
|
|
|
Object result = pjp.proceed();
|
|
|
if (result == null) {
|
|
|
return result;
|
|
|
}
|
|
|
searchApiResult = (SearchApiResult) result;
|
|
|
if (searchApiResult.getCode() != 200) {
|
|
|
return result;
|
|
|
cacheObject = (Serializable)result;
|
|
|
|
|
|
if(cacheObject instanceof SearchApiResult && ((SearchApiResult) cacheObject).getCode()!=200){
|
|
|
return cacheObject;
|
|
|
}
|
|
|
searchCacheService.addSearchApiResultToCache(searchCache, cacheKey, searchApiResult);
|
|
|
return result;
|
|
|
searchCacheService.addSerializableObjectToCache(searchCache, cacheKey, cacheObject);
|
|
|
return cacheObject;
|
|
|
}
|
|
|
} |
...
|
...
|
|