注解 获取 存入redis

来源:互联网 发布:淘宝英文版网址 编辑:程序博客网 时间:2024/06/08 15:35

<!-- 创建注解 -->

@Target({ ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CreditSourceCache {
    /**
     * 缓存key
     * @return
     */
    String  keyName() default "";
    /**
     * 缓存值
     * @return
     */
    String  keyValue() default "";
    /**
     * 是否收费 FEE-收费,NOTFEE-免费
     * @return
     */
    String  fee() default "FEE";
    /**
     * 缓存时间
     * @return
     */
    int  cacheTime()  default  60*60*24;


}


<!-- 加注解 -->

@CreditSourceType(mainName = CreditSourceName.BaiDu, childName = "risk")

@CreditSourceCache(keyName = "baiDuRisk_id#{#entity.getIdCard()}_phone_#{#entity.getPhoneNumber()}", cacheTime = 86401)
public OriginalResult<String> queryPanShiRiskList(BaiDuPanShiEntity baiDuPanShiEntity){}


@Aspect

@Component
public class BusinessInterceptor {

    public static final String BUSINESS_CREDIT_SOURCE = "execution(* com.mobanker.feature.credit.business.creditSource.api.*.*(..))";

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CreditSendBigDataBusiness creditSendBigDataBusiness;

    @Autowired
    private RedisBusiness redisBusiness;

    @Around(value = BUSINESS_CREDIT_SOURCE)
    public  Object   sendLogData(ProceedingJoinPoint jp){

        logger.debug("pointcut start...");

        long startTime = System.currentTimeMillis();
        CreditSourceBigData  bigData = new CreditSourceBigData();

        //缓存信息
        int cacheTime = 60*60*24;
        String keyName = null;
        String fee = "FEE";

        //获取当前类
        Class<?> classTarget = jp.getTarget().getClass();
        //获取目标方法体参数
        Object[] params = jp.getArgs();
        //获取方法名
        String methodName = jp.getSignature().getName();
        //获取方法签名类型
        Class<?>[] par = ((MethodSignature) jp.getSignature()).getParameterTypes();
        //方法前面第一个参数
        Object firstParamObj = params[0];
        //方法返回值
        Object returnValue = null;
        Method objMethod = null;

        try {
            objMethod = classTarget.getMethod(methodName, par);
        } catch (NoSuchMethodException e) {
            logger.error("", e);
            return null;
        }

        //获取缓存注解信息
        CreditSourceCache creditSourceCache = objMethod.getAnnotation(CreditSourceCache.class);
        if (creditSourceCache != null){
            String spelExpress = creditSourceCache.keyName();
            fee = creditSourceCache.fee();
            cacheTime = creditSourceCache.cacheTime();

            if (StringUtils.isNotBlank(spelExpress)){

                keyName = getCacheKeyBySpEL(firstParamObj, spelExpress);
                logger.debug("spelExpress:{}, fee:{}, cacheTime:{}, cacheKeyName:{}", spelExpress, fee, cacheTime, keyName);
                //从缓存获取值
                String cacheValue = redisBusiness.getValue(keyName);
                if (StringUtils.isNotBlank(cacheValue)){
                    OriginalResult<String> oResult = JSONObject.parseObject(cacheValue, new TypeReference<OriginalResult<String>>(){});
                    logger.debug("from cache, value = {}", cacheValue);
                    return oResult;
                }
            }
        }


        try {
            returnValue = jp.proceed();
        } catch (Throwable throwable) {
            logger.error("切面调用方法error", throwable);
            return null;
        }
        if (creditSourceCache != null){
            long endTime = System.currentTimeMillis();
            if (params != null && params.length > 0){
                String paramClassName = firstParamObj.getClass().getName();
                //组装BigData数据
                bigData = assembleCreditSourceBigData(bigData, paramClassName, firstParamObj, objMethod);
            }
            bigData.setClacTime(endTime - startTime);
            bigData.setInStr(JSONObject.toJSONString(firstParamObj));
            bigData.setOutStr(returnValue == null ? "null" : JSONObject.toJSONString(returnValue));
            bigData.setCreateTime(new Date());
            //TODO 发送到大数据
            creditSendBigDataBusiness.sendBigData(bigData);

            //TODO 解析各征信源返回报文,做缓存信息,剔除错误信息,只缓存正确的信息
            if (returnValue != null){
                OriginalResult<String>  originalResult = (OriginalResult<String>)returnValue;
                boolean checkFlag = false;
                try {
                    checkFlag = checkResultCanCache(originalResult, bigData.getCreditMainName(), bigData.getCreditChildName());
                }catch (Exception e) {
                    logger.error("", e);
                }
                if(checkFlag){
                    logger.debug("缓存到redis, keyName={}, value={}, cacheTime={}", keyName, originalResult, cacheTime);
                    redisBusiness.setValue(keyName, JSONObject.toJSONString(originalResult), cacheTime);
                }
            }
        }
        logger.debug("切点结束。。。");
        return returnValue;
    }
原创粉丝点击