注解 获取 存入redis

<!-- 创建注解 -->

@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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麦香鸡翅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值