15 changed files with 202 additions and 34 deletions
@ -0,0 +1,13 @@ |
|||
package com.ccsens.cloudutil.annotation; |
|||
|
|||
import java.lang.annotation.*; |
|||
|
|||
|
|||
/** |
|||
* @author 逗 |
|||
*/ |
|||
@Documented |
|||
@Retention(RetentionPolicy.RUNTIME) |
|||
@Target(ElementType.METHOD) |
|||
public @interface CacheMd5 { |
|||
} |
@ -0,0 +1,83 @@ |
|||
package com.ccsens.cache_test.aspect; |
|||
|
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.ccsens.cache_test.bean.dto.CacheTest; |
|||
import com.ccsens.cache_test.bean.vo.CacheTestVo; |
|||
import com.ccsens.cache_test.util.JsonResponse; |
|||
import com.ccsens.cache_test.util.Md5Util; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.aspectj.lang.ProceedingJoinPoint; |
|||
import org.aspectj.lang.annotation.*; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
|
|||
@Slf4j |
|||
@Aspect |
|||
@Component |
|||
public class CacheMd5Aspect { |
|||
|
|||
@Pointcut("@annotation(com.ccsens.cloudutil.annotation.CacheMd5)") |
|||
public void cacheMd5(){ |
|||
|
|||
} |
|||
//
|
|||
// @After("cacheMd5()")
|
|||
// public void cacheMd5TestAspect(JoinPoint joinPoint) throws JsonProcessingException {
|
|||
// Object[] args = joinPoint.getArgs();
|
|||
//// Object proceed = joinPoint.(args);
|
|||
// Signature signature = joinPoint.getSignature();
|
|||
// String kind = joinPoint.getKind();
|
|||
// SourceLocation sourceLocation = joinPoint.getSourceLocation();
|
|||
// JoinPoint.StaticPart staticPart = joinPoint.getStaticPart();
|
|||
// Object target = joinPoint.getTarget();
|
|||
// Object aThis = joinPoint.getThis();
|
|||
// System.out.println(joinPoint);
|
|||
//
|
|||
// System.out.println("============================================---");
|
|||
// }
|
|||
|
|||
@Around("cacheMd5()") |
|||
public Object cacheMd5TestAspect1(ProceedingJoinPoint joinPoint) throws Throwable { |
|||
long s = System.currentTimeMillis(); |
|||
//拿到入参
|
|||
Object[] args = joinPoint.getArgs(); |
|||
|
|||
CacheTest cacheTest = args == null || args.length < 1 ? null : (CacheTest)args[0]; |
|||
//获取入参的md5值
|
|||
String inMd5 = cacheTest == null ? null : cacheTest.getMd5(); |
|||
//获取返回值
|
|||
JsonResponse<Object> proceed = (JsonResponse<Object>) joinPoint.proceed(args); |
|||
//入参没有md5直接返回
|
|||
if(inMd5 == null){ |
|||
log.info("cache---入参md5为空"); |
|||
long e = System.currentTimeMillis(); |
|||
log.info("切面时间:{}",e-s); |
|||
return proceed; |
|||
} |
|||
//异常直接返回
|
|||
if(proceed.getCode() != 200){ |
|||
log.info("cache---接口异常"); |
|||
long e = System.currentTimeMillis(); |
|||
log.info("切面时间:{}",e-s); |
|||
return proceed; |
|||
} |
|||
//获取返回数据
|
|||
if(proceed.getData() == null){ |
|||
log.info("cache---返回数据为空"); |
|||
long e = System.currentTimeMillis(); |
|||
log.info("切面时间:{}",e-s); |
|||
return proceed; |
|||
} |
|||
//将date转成json并加密
|
|||
String outMd5 = Md5Util.stringTo(JSONObject.toJSONString(proceed.getData())); |
|||
log.info("md5:{}",outMd5); |
|||
if(inMd5.equalsIgnoreCase(outMd5)){ |
|||
log.info("md5相同则返回空"); |
|||
proceed.setData(null); |
|||
} |
|||
long e = System.currentTimeMillis(); |
|||
log.info("切面时间:{}",e-s); |
|||
return proceed; |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue