Authored by foxxy

切面压缩

package com.yoho.search.common.compress.aop;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface CommpressAble {
public boolean isCommpress() default false;
}
... ...
package com.yoho.search.common.compress.aop;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
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.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.common.utils.SnappyUtils;
import com.yoho.search.models.SearchApiResult;
@Component
@Aspect
public class CommpressAspect {
// 获取请求的参数
private boolean isCommpress() {
HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(httpServletRequest);
if(paramMap.containsKey("isDebug")){
return false;
}
return true;
}
@Around("@annotation(com.yoho.search.common.compress.aop.CommpressAble)")
public Object downGrade(ProceedingJoinPoint pjp) throws Throwable {
// 获取方法签名
MethodSignature signature = (MethodSignature) pjp.getSignature();
final Class<?> returnType = signature.getMethod().getReturnType();
if (!returnType.isAssignableFrom(SearchApiResult.class)) {
return pjp.proceed();
}
// 从降级缓存中可以获取结果,则直接返回[说明此刻是降级状态]
Object result = pjp.proceed();
if (result == null) {
return result;
}
CommpressAble commpressAble = signature.getMethod().getAnnotation(CommpressAble.class);
SearchApiResult searchApiResult=(SearchApiResult)result;
if(commpressAble.isCommpress() && isCommpress()){
searchApiResult.setData(SnappyUtils.compress(searchApiResult.getData())).setCompress(true);
}
return searchApiResult;
}
}
... ...
... ... @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yoho.search.common.compress.aop.CommpressAble;
import com.yoho.search.common.downgrade.aop.DownGradeAble;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
... ... @@ -50,6 +51,7 @@ public class SearchLikeSecneController {
}
//@DownGradeAble(key = "searchLikeNotInShop")
@CommpressAble(isCommpress=true)
@RequestMapping(method = RequestMethod.GET, value = "/searchLikeNotInShop")
@ResponseBody
public SearchApiResult searchLikeNotInShop(HttpServletRequest request) {
... ...
... ... @@ -19,7 +19,6 @@ import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.common.utils.SnappyUtils;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchDynamicConfigService;
... ... @@ -70,7 +69,7 @@ public class SearchLikeNotInShopService extends AbstractCacheAbleService {
String redisCacheKey = searchCacheKeyHelper.getSearchLikeNotInShopKeyCache(productSkn, pageSize);
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, redisCacheKey);
if (cacheObject != null) {
return new SearchApiResult().setData(SnappyUtils.compress(cacheObject)).setCompress(true);
return new SearchApiResult().setData(cacheObject);
}
// 4、获取当前查询的SKN的基本信息
... ... @@ -106,7 +105,7 @@ public class SearchLikeNotInShopService extends AbstractCacheAbleService {
// 8、结果加入缓存
searchCacheService.addJSONObjectToCache(this.searchCache, redisCacheKey, result);
return new SearchApiResult().setData(SnappyUtils.compress(result)).setCompress(true);
return new SearchApiResult().setData(result);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setData(null).setMessage("searchLikeNotInShop Exception").setCode(500);
... ...