All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.luues.redis.util.CommontUtil Maven / Gradle / Ivy

package com.luues.redis.util;

import cn.luues.tool.core.util.ObjectUtil;
import com.luues.exception.core.exception.RedisSynchronizedException;
import com.luues.redis.single.service.JedisTemplate;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

@Slf4j(topic = "c.l.r.u.c")
public class CommontUtil {

    private CommontUtil(){}
    private static class CommontHoler{
        private static LocalVariableTableParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
        private static ExpressionParser parser = new SpelExpressionParser();
    }
    public static LocalVariableTableParameterNameDiscoverer getLocalVariableTableParameterNameDiscoverer() {
        return CommontHoler.parameterNameDiscoverer;
    }
    public static ExpressionParser getExpressionParser() {
        return CommontHoler.parser;
    }


    @SneakyThrows
    public static String getConditionOrKey(String condition, Method objMethod, Object[] objects, String an) {
        String condition_ = "";
        if (condition.contains("#{")) {
            String[] k = condition.split("#\\{");
            for (String s : k) {
                if (s.contains("}")) {
                    String k_ = s.split("}")[0];
                    condition_ += parseKey("#" + k_, objMethod, objects, an) + (s.split("}").length > 1 ? s.split("}")[1] : "");
                } else {
                    condition_ += s;
                }
            }
        } else {
            condition_ = condition;
        }
        return condition_;
    }

    @SneakyThrows
    public static Object getRedisCacheInfo(JedisTemplate jedisTemplate, String key_, ProceedingJoinPoint joinPoint, int expire) {
        if (jedisTemplate.exists(key_.getBytes())) {
            log.debug("\n{\n     This method gets the value from the redis cache\n}");
            return ObjectUtil.deserialize(jedisTemplate.get(key_.getBytes()));
        } else {
            Object object = joinPoint.proceed();
            if (expire >= 0) {
                jedisTemplate.setex(key_.getBytes(), ObjectUtil.serialize(object), expire);
            } else {
                jedisTemplate.set(key_.getBytes(), ObjectUtil.serialize(object));
            }
            log.debug("\n{\n{     The return data of this method has been stored in the redis cache}\n}");
            return object;
        }
    }

    @SneakyThrows
    public static String parseKey(String key, Method objMethod, Object[] objects, String an) {
        //获取被拦截方法参数名列表(使用Spring支持类库)
        String[] paraNameArr = getLocalVariableTableParameterNameDiscoverer().getParameterNames(objMethod);
        //SPEL上下文
        StandardEvaluationContext context = new StandardEvaluationContext();
        //把方法参数放入SPEL上下文中
        if(null == paraNameArr){
            String str = "";
            for(Object object : objects){
                str += object + ",";
            }
            log.info("{},{},{},{}", objMethod.getDeclaringClass().getName(), objMethod.getName(), an, str);
            Parameter[] parameters = objMethod.getParameters();
            for(Parameter parameter : parameters){
                log.info("==={}", parameter.getName());
            }
        }else{
            for (int i = 0; i < paraNameArr.length; i++) {
                context.setVariable(paraNameArr[i], objects[i]);
            }
        }
        //使用SPEL进行key的解析
        Object value = getExpressionParser().parseExpression(key).getValue(context, Object.class);
        if (null == value) {
            throw new RedisSynchronizedException(an + " key() #{" + key.split("#")[1] + "} not is null!");
        }
        return value.toString();
    }

    @SneakyThrows
    public static String valiCondition(String condition, String an) {
        //使用SPEL进行key的解析
        Expression expression = getExpressionParser().parseExpression(condition);
        try {
            Object value = expression.getValue();
            if (null == value) {
                throw new RedisSynchronizedException(an + " condition() #{" + condition.split("#")[1] + "} error!");
            }
            return value.toString();
        } catch (Exception e) {
            throw new RedisSynchronizedException(an + " condition() '" + condition + "' is error!");
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy