yanderong
2026-06-10 0a50085d6ef51274c28d3915446af6d9ddd91cd2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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.stereotype.Component;
 
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.message_push_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.message_push_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.message_push_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.message_push_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.message_push_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;
    }
}