From c007f0ca1785db093d48f4846cda82fe8e955765 Mon Sep 17 00:00:00 2001 From: kimi <kimi42345@gmail.com> Date: 星期三, 27 五月 2020 09:59:29 +0800 Subject: [PATCH] merage --- src/main/java/com/highdatas/mdm/util/RedisClient.java | 186 ++++++++++++++++++++++++++++++++++------------ 1 files changed, 136 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/highdatas/mdm/util/RedisClient.java b/src/main/java/com/highdatas/mdm/util/RedisClient.java index b1ffc39..28bda56 100644 --- a/src/main/java/com/highdatas/mdm/util/RedisClient.java +++ b/src/main/java/com/highdatas/mdm/util/RedisClient.java @@ -1,76 +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.url}") - String url; - - String prefix ; - + long timeout = 30 * 60; private RedisClient() { - this.prefix = "/api/datacvg/redis/"; + } - public boolean putRedisVal(String key, String value) { - String url = this.url + 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 = this.url + 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 = this.url + 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); + } } -- Gitblit v1.8.0