Authored by hugufei

缓存优化,支持指定返回类型

package com.yoho.search.common.cache.aop;
import java.io.Serializable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import com.yoho.search.common.cache.CacheType;
import com.yoho.search.models.SearchApiResult;
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
... ... @@ -39,4 +41,11 @@ public @interface SearchCacheAble {
*/
public String[] removedParams() default {};
/**
* 返回值类型
*
* @return
*/
public Class<? extends Serializable> returnClass() default SearchApiResult.class;
}
\ No newline at end of file
... ...
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;
}
}
... ...
package com.yoho.search.service.base;
import java.io.Serializable;
import java.util.List;
import org.elasticsearch.search.builder.SearchSourceBuilder;
... ... @@ -14,7 +15,6 @@ import com.yoho.search.common.cache.model.CacheObject;
import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.utils.SearchParamUtils;
import com.yoho.search.models.SearchApiResult;
@Service
public class SearchCacheService {
... ... @@ -194,22 +194,22 @@ public class SearchCacheService {
}
/*********************************** SearchApiResult *****************************************/
public SearchApiResult getSearchApiResultFromCache(SearchCache searchCache, String cacheKey) {
public <T extends Serializable> T getSerializableObjectFromCache(SearchCache searchCache,String cacheKey, Class<T> clazz) {
try {
CacheObject cacheObject = this.getCacheObjectFromCache(cacheKey, searchCache);
if (cacheObject == null) {
return null;
}
JSONObject searchApiResultJSONObject = cacheObject.toJSONObject();
return JSON.toJavaObject(searchApiResultJSONObject, SearchApiResult.class);
return JSON.toJavaObject(searchApiResultJSONObject,clazz);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void addSearchApiResultToCache(SearchCache searchCache, String cacheKey,SearchApiResult searchApiResult) {
JSONObject jsonObject = (JSONObject)JSON.toJSON(searchApiResult);
public <T extends Serializable> void addSerializableObjectToCache(SearchCache searchCache, String cacheKey,T object) {
JSONObject jsonObject = (JSONObject)JSON.toJSON(object);
this.addJSONObjectToCache(cacheKey, jsonObject, searchCache);
}
}
... ...