| | |
| | | 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.StringUtils;
|
| | |
|
| | | import java.util.LinkedHashMap;
|
| | | import java.util.Map;
|
| | | import java.util.List;
|
| | | import java.util.Set;
|
| | | import java.util.concurrent.TimeUnit;
|
| | |
|
| | | @ConfigurationProperties(prefix = "redis")
|
| | | @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";
|
| | | Map<String, String> params = new LinkedHashMap<>();
|
| | | params.put("key",key);
|
| | | params.put("value",value);
|
| | | params.put("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) {
|
| | | String url = this.url + prefix + "get";
|
| | | Map<String, String> params = new LinkedHashMap<>();
|
| | | params.put("key",key);
|
| | | params.put("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)) {
|
| | | try {
|
| | | 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) {
|
| | | 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) {
|
| | | String url = this.url + prefix + "delete";
|
| | | Map<String, String> params = new LinkedHashMap<>();
|
| | | params.put("key",key);
|
| | | params.put("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;
|
| | | 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 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);
|
| | | }
|
| | | }
|