首页 > 其他 > 详细

切面类

时间:2020-12-10 09:28:44      阅读:33      评论:0      收藏:0      [点我收藏+]

切面类:
RedisCacheAspect.java

/**

  • @author: Administrator
    */
    @Component
    @Aspect
    public class RedisCacheAspect {

    @Autowired
    private RedisUtil redisUtil;

    private static final ExpressionParser parser = new SpelExpressionParser();

    /**

    • 插入缓存

    • @param joinPoint

    • @param redisCache
      */
      @Before("@annotation(redisCache)")
      public void insertCache(JoinPoint joinPoint,RedisCache redisCache){
      // 获取参数
      Object[] args = joinPoint.getArgs();

      // 获取参数名字
      MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
      String[] parameterNames = methodSignature.getParameterNames();
      // 获取 EvaluationContext
      StandardEvaluationContext evaluationContext = getEvaluationContext(parameterNames, args);
      // 获取 redis 存储 key
      if(!redisCache.batch()){
      String cacheKey = redisCache.cacheKey();
      String cacheValue = redisCache.cacheValue();
      // 解析 redis key
      Expression keyExpression = parser.parseExpression(cacheKey,ParserContext.TEMPLATE_EXPRESSION);
      String key = keyExpression.getValue(evaluationContext, String.class);

       // 解析 值
       Expression valueExpression = parser.parseExpression(cacheValue);
       Object value = valueExpression.getValue(evaluationContext);
       // 存入 缓存
       redisUtil.setKey(key,value);
      

      }else{
      // 获取 迭代对象
      String batchValue = redisCache.cacheValue();
      String itemName = redisCache.itemName();
      String cacheKey = redisCache.cacheKey();
      Iterable value = parser.parseExpression(batchValue).getValue(evaluationContext, Iterable.class);
      // 迭代生成 缓存
      for(Object it: value){
      evaluationContext.setVariable(itemName,it);
      // 解析 key
      String redisCacheKey = parser.parseExpression(cacheKey,ParserContext.TEMPLATE_EXPRESSION).getValue(evaluationContext, String.class);
      redisUtil.setKey(redisCacheKey,it);
      }
      }
      }

    public StandardEvaluationContext getEvaluationContext(String[] parameterNames,Object[] args){
    StandardEvaluationContext context = new StandardEvaluationContext();
    for(int i = 0; i< parameterNames.length ; i++){
    // 设置参数
    context.setVariable(parameterNames[i],args[i]);
    }
    return context;
    }
    }

切面类

原文:https://www.cnblogs.com/LinYanyan1024-6285/p/14111913.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!