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

  • 相关阅读:
    Linux dd 命令
    excel合并单元格数据读取
    判断字符串是否以中文字符开头
    列表嵌套字典去重统计
    【转载】【DBDK】dpdk大页内存原理
    【LinuxShell】ps 命令浅析
    【LinuxShell】free 命令详解
    【网络安全】IOC概念浅析
    【转载】【网络安全】渗透中 PoC、Exp、Payload 与 Shellcode 的区别
    【SVN】windows 下的SVN常见问题及其解决方法
  • 原文地址:https://www.cnblogs.com/wwyydd/p/14116999.html
Copyright © 2011-2022 走看看