Pu Zhibing
2025-04-25 20fb7c22fd9d4a936a2e9f4b003da51a0c2a0217
ManagementIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java
@@ -1,21 +1,11 @@
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.data.redis.core.RedisTemplate;
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;
import java.util.concurrent.TimeUnit;
/**
@@ -23,6 +13,9 @@
 */
@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate redisTemplate;
@@ -32,18 +25,9 @@
     * @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出错了");
        if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){
            redisTemplate.opsForValue().set(key, value);
            }
        }
    }
@@ -54,16 +38,8 @@
     * @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出错了");
            }
        if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){
            redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
        }
    }
@@ -75,55 +51,22 @@
     */
    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");
            String data = (String) redisTemplate.opsForValue().get(key);
            return 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){
    public void 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/remove", map);
            JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
            if(jsonObject.getIntValue("code") != 200){
                System.err.println("调用redis出错了");
            redisTemplate.delete(key);
            }
            return jsonObject.getString("data");
        }
        return null;
    }
}