package com.stylefeng.guns.modular.system.util;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.stylefeng.guns.core.util.ToolUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.MediaType;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
import org.springframework.web.client.RestTemplate;
|
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.JedisPool;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
|
/**
|
* Redis工具类
|
*/
|
@Component
|
public class RedisUtil {
|
|
|
|
/**
|
* 向redis中存储字符串没有过期时间
|
* @param key
|
* @param value
|
*/
|
public void setStrValue(String key, String value){
|
if(ToolUtil.isNotEmpty(key)){
|
//将请求头部和参数合成一个请求
|
Map<String,String> map = new HashMap<>();
|
map.put("key", key);
|
map.put("value", value);
|
String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/setValue_", map);
|
JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
|
if(jsonObject.getIntValue("code") != 200){
|
System.err.println("调用redis出错了");
|
}
|
}
|
|
}
|
|
|
/**
|
* 以分钟为单位设置存储值(设置过期时间)
|
* @param key
|
* @param value
|
* @param time 秒
|
*/
|
public void setStrValue(String key, String value, int time){
|
if(ToolUtil.isNotEmpty(key)){
|
Map<String,String> map = new HashMap<>();
|
map.put("key", key);
|
map.put("value", value);
|
map.put("time", String.valueOf(time));
|
String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/setValue", map);
|
JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
|
if(jsonObject.getIntValue("code") != 200){
|
System.err.println("调用redis出错了");
|
}
|
}
|
}
|
|
|
/**
|
* 从redis中获取值
|
* @param key
|
* @return
|
*/
|
public String getValue(String key){
|
if(ToolUtil.isNotEmpty(key)){
|
Map<String,String> map = new HashMap<>();
|
map.put("key", key);
|
String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/getValue", map);
|
JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
|
if(jsonObject.getIntValue("code") != 200){
|
System.err.println("调用redis出错了");
|
}
|
return jsonObject.getString("data");
|
}
|
return null;
|
}
|
|
|
|
/**
|
* 从redis中获取值
|
* @param key
|
* @return
|
*/
|
public String getValues(String key){
|
if(ToolUtil.isNotEmpty(key)){
|
Map<String,String> map = new HashMap<>();
|
map.put("keys", key);
|
String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/getValues", map);
|
JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
|
if(jsonObject.getIntValue("code") != 200){
|
System.err.println("调用redis出错了");
|
}
|
return jsonObject.getString("data");
|
}
|
return null;
|
}
|
|
|
/**
|
* 删除key
|
* @param key
|
*/
|
public String remove(String key){
|
if(ToolUtil.isNotEmpty(key)){
|
Map<String,String> map = new HashMap<>();
|
map.put("key", key);
|
String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/getValue", map);
|
JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
|
if(jsonObject.getIntValue("code") != 200){
|
System.err.println("调用redis出错了");
|
}
|
return jsonObject.getString("data");
|
}
|
return null;
|
}
|
}
|