kimi
2020-05-27 c007f0ca1785db093d48f4846cda82fe8e955765
src/main/java/com/highdatas/mdm/util/RedisClient.java
@@ -1,78 +1,162 @@
package com.highdatas.mdm.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpMethod;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
@ConfigurationProperties(prefix = "redis")
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisClient {
    @Value("${redis.host}")
    String host;
    @Value("${redis.port}")
    String port;
    String prefix ;
    long timeout = 30 * 60;
    private RedisClient() {
        this.prefix = "/api/datacvg/redis/";
    }
    public  boolean putRedisVal(String key, String value) {
        String url = host+ Constant.colon +  port + prefix + "put";
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.set("key",key);
        params.set("value",value);
        params.set("type",Constant.MainData);
        String s = HttpUtils.HttpRestClient(url, HttpMethod.POST, params);
        JSONObject result = (JSONObject) JSON.parse(s);
        String sucess = result.getString(Constant.Success);
        if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
    public boolean putRedisVal(String key, String value, long timeout) {
        try {
            RedisUtil.set(getRealRedisKey(key), value, timeout);
        }
        catch (Exception e) {
            return false;
        }else {
        }
        return true;
    }
    public boolean putRedisVal(String key, String value) {
        try {
            if (value == null || StringUtils.isEmpty(value)) {
                return true;
            }
            RedisUtil.set(getRealRedisKey(key), value, timeout);
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    public boolean putRedisValObj(String key, Object value) {
        try {
            if (value == null || StringUtils.isEmpty(value.toString())) {
                return true;
            }
            RedisUtil.set(getRealRedisKey(key), value, timeout);
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    public String getRedisTimeOutVal(String key) {
        try{
            Object o = RedisUtil.get(getRealRedisKey(key));
            if (o == null) {
                return null;
            } else {
                return o.toString();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public String getRedisVal(String key) {
        try {
            String url =  host+ Constant.colon +  port + prefix + "get";
            MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
            params.set("key",key);
            params.set("type",Constant.MainData);
            String s = HttpUtils.HttpRestClient(url, HttpMethod.POST, params);
            JSONObject result = (JSONObject) JSON.parse(s);
            String sucess = result.getString(Constant.Success);
            if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
            Object o = RedisUtil.get(getRealRedisKey(key));
            if (o == null) {
                return null;
            }else {
                return result.getString(Constant.Data);
            } else {
                RedisUtil.expire(getRealRedisKey(key), timeout, TimeUnit.MINUTES);
                return o.toString();
            }
        }catch (Exception e){
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public Object getRedisValObj(String key) {
        try {
            Object o = RedisUtil.get(getRealRedisKey(key));
            if (o == null) {
                return null;
            } else {
                RedisUtil.expire(getRealRedisKey(key), timeout, TimeUnit.MINUTES);
                return o;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public boolean deleteRedisVal(String key) {
        try {
            boolean del = RedisUtil.del(getRealRedisKey(key));
            return del;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    public boolean putRedisList(String key, List values) {
        try {
            if (values == null || values.isEmpty()) {
                return true;
            }
            RedisUtil.lPushAll(getRealRedisKey(key),values);
            RedisUtil.expire(getRealRedisKey(key), timeout, TimeUnit.MINUTES);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    public boolean deleteRedisVal(String key) {
        String url =  host+ Constant.colon +  port + prefix + "delete";
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.set("key",key);
        params.set("type",Constant.MainData);
        String s = HttpUtils.HttpRestClient(url, HttpMethod.POST, params);
        JSONObject result = (JSONObject) JSON.parse(s);
        String sucess = result.getString(Constant.Success);
        if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
            return false;
        }else {
            return true;
    public List<Object> getRedisList(String key) {
        try {
            List<Object> objects = RedisUtil.lGet(getRealRedisKey(key), 0, -1);
            if (objects == null || objects.isEmpty()) {
                return null;
            } else {
                RedisUtil.expire(getRealRedisKey(key), timeout, TimeUnit.MINUTES);
                return objects;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static String getRealRedisKey(String key) {
        String s = DbUtils.StrJoinLink(Constant.UnderLine, Constant.MainData, key);
        return s;
    }
    public void delByCharacter(String characterId) {
        String realRedisKey = getRealRedisKey(characterId);
        Set<String> keys = RedisUtil.getKeys(realRedisKey);
        if (keys != null && !keys.isEmpty()) {
            RedisUtil.del(keys);
        }
    }
    public void delByField() {
        Set<String> keys = RedisUtil.getKeys("*field");
        RedisUtil.del(keys);
    }
}