Pu Zhibing
2024-12-15 40b94a2b7afb9acc38fcda17b5375a47ec928da8
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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;
 
 
/**
 * Redis工具类
 */
@Component
public class RedisUtil {
 
    @Autowired
    private RestTemplate internalRestTemplate;
 
 
    /**
     * 向redis中存储字符串没有过期时间
     * @param key
     * @param value
     */
    public void setStrValue(String key, String value){
        if(ToolUtil.isNotEmpty(key)){
            //发送验证码短信
            HttpHeaders headers = new HttpHeaders();
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //将请求头部和参数合成一个请求
            MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
            params.add("key", key);
            params.add("value", value);
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
            String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/setValue_", requestEntity, String.class);
            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)){
            //发送验证码短信
            HttpHeaders headers = new HttpHeaders();
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //将请求头部和参数合成一个请求
            MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
            params.add("key", key);
            params.add("value", value);
            params.add("time", String.valueOf(time));
            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
            String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/setValue", requestEntity, String.class);
            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)){
            HttpHeaders headers = new HttpHeaders();
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //将请求头部和参数合成一个请求
            MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
            params.add("key", key);
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
            String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/getValue",requestEntity , String.class);
            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)){
            HttpHeaders headers = new HttpHeaders();
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //将请求头部和参数合成一个请求
            MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
            params.add("key", key);
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
            String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/remove",requestEntity , String.class);
            JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
            if(jsonObject.getIntValue("code") != 200){
                System.err.println("调用redis出错了");
            }
            return jsonObject.getString("data");
        }
        return null;
    }
    
    /**
     * 添加数据到list集合
     * @param key
     * @param value
     */
    public void addListRight(String key, String value){
        //发送验证码短信
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("key", key);
        params.add("value", value);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/addListRight", requestEntity, String.class);
        JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
        if(jsonObject.getIntValue("code") != 200){
            System.err.println("调用redis出错了");
        }
    }
    
    
    /**
     * 添加数据到list集合
     * @param key
     * @param value
     */
    public void addListLeft(String key, String value){
        //发送验证码短信
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("key", key);
        params.add("value", value);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/addListLeft", requestEntity, String.class);
        JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
        if(jsonObject.getIntValue("code") != 200){
            System.err.println("调用redis出错了");
        }
    }
    
    
    
    /**
     * 获取list集合中第一个数据
     * @param key
     * @return
     */
    public String getListFirstValue(String key){
        if(ToolUtil.isNotEmpty(key)){
            HttpHeaders headers = new HttpHeaders();
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //将请求头部和参数合成一个请求
            MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
            params.add("key", key);
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
            String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/getListFirstValue",requestEntity , String.class);
            return s;
        }
        return null;
    }
}