zoukankan      html  css  js  c++  java
  • 切面类 糖不苦

    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;
    }
    }

  • 相关阅读:
    搭建armlinuxgcc交叉编译工具链环境(Android原生(JNI)开发环境搭建)
    linux vi命令详解
    Android手机在开发调试时logcat不显示输出信息的解决办法
    2012的总结和13的展望
    Gvim编码学习笔记
    vue自定义过滤器格式化时间为年、月、日、小时、分钟、刚刚 J
    学校网站群建设理念
    何为真正网站群?
    手机网站——移动互联网新趋势
    建站是浮云,We7很给力
  • 原文地址:https://www.cnblogs.com/wwyydd/p/14116999.html
Copyright © 2011-2022 走看看