Pu Zhibing
2025-04-18 77f5f06057b425fe96058113f906300d2b47752b
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;
 
import java.io.IOException;
import java.util.*;
 
 
/**
 * Redis工具类
 */
@Component
public class RedisUtil {
    
    @Autowired
    private JedisPool jedisPool;
    
    private Timer timer;
    
    
    /**
     * 向redis中存储字符串没有过期时间
     * @param key
     * @param value
     */
    public void setStrValue(String key, String value){
        if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){
            Jedis resource = jedisPool.getResource();
            String set = resource.set(key, value);
            closeJedis(resource);
        }
    }
    
    
    /**
     * 以分钟为单位设置存储值(设置过期时间)
     * @param key
     * @param value
     * @param time 秒
     */
    public void setStrValue(String key, String value, int time){
        if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){
            Jedis resource = jedisPool.getResource();
            String setex = resource.setex(key, time, value);
            closeJedis(resource);
        }
    }
    
    
    /**
     * 从redis中获取值
     * @param key
     * @return
     */
    public String getValue(String key){
        if(ToolUtil.isNotEmpty(key)){
            Jedis resource = jedisPool.getResource();
            String data = resource.get(key);
            closeJedis(resource);
            return data;
        }
        return null;
    }
    
    
    /**
     * 批量获取
     * @param kes
     * @return
     */
    public List<Object> getValues(List<String> kes){
        if(null != kes){
            Jedis resource = jedisPool.getResource();
            Pipeline pipelined = resource.pipelined();
            for(String key : kes){
                pipelined.get(key);
            }
            List<Object> list = pipelined.syncAndReturnAll();
            
            closeJedis(resource);
            pipelined.clear();
            try {
                pipelined.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            List<Object> data = new ArrayList<>();
            for(Object o : list){
                if(null != o){
                    data.add(o);
                }
            }
            return data;
        }
        return null;
    }
    
    
    /**
     * 删除key
     * @param key
     */
    public void remove(String key){
        if(ToolUtil.isNotEmpty(key)){
            Jedis resource = jedisPool.getResource();
            Long del = resource.del(key);
            closeJedis(resource);
        }
    }
    
    
    /**
     * 向集合key添加数据
     * @param key
     * @param members
     */
    public void addSetValue(String key, String...members){
        if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(members)){
            Jedis resource = jedisPool.getResource();
            Long sadd = resource.sadd(key, members);
            resource.close();
        }
    }
    
    
    /**
     * 返回Set集合数据
     * @param key
     * @return
     */
    public Set<String> getSetAllValue(String key){
        Set<String> smembers = new HashSet<>();
        if(ToolUtil.isNotEmpty(key)){
            Jedis resource = jedisPool.getResource();
            smembers = resource.smembers(key);
            resource.close();
        }
        return smembers;
    }
    
    
    /**
     * 删除Set集合中的值
     * @param key
     * @param members
     */
    public void delSetValue(String key, String...members){
        if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(members)){
            Jedis resource = jedisPool.getResource();
            Long sadd = resource.srem(key, members);
            resource.close();
        }
    }
    
    
    /**
     * 删除资源
     * @param jedis
     */
    public void closeJedis(Jedis jedis){
        if(null != jedis){
            jedis.close();
        }
    }
    
    
    /**
     * redis加锁
     * @param key
     * @param value
     * @param time
     * @return
     */
    public boolean lock(String key, String value, int time){
        if(!StringUtils.isEmpty(key)){
            key += "_lock";
            Jedis resource = jedisPool.getResource();
            String set = resource.set(key, value, "nx", "ex", time);
            if("OK".equals(set)){
                String finalKey = key;
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        System.err.println("定时任务启动");
                        Jedis resource = jedisPool.getResource();
                        resource.setex(finalKey, time, value);
                        resource.close();
                    }
                }, 1000, 500);
            }
            resource.close();
            return "OK".equals(set) ? true : false;
        }
        return false;
    }
    
    /**
     * 获取redis锁
     * @param time
     * @return
     */
    public boolean lock(int time){
        String uuid = UUID.randomUUID().toString();
        return lock("redis", uuid, time);
    }
    
    
    public boolean lock(String key, int time){
        String uuid = UUID.randomUUID().toString();
        return lock(key, uuid, time);
    }
    
    
    /**
     * redis释放锁
     * @param key
     * @return
     */
    public boolean unlock(String key){
        if(!StringUtils.isEmpty(key)){
            key += "_lock";
            Jedis resource = jedisPool.getResource();
            timer.cancel();//取消定时任务
            Long del = resource.del(key);
            resource.close();
            return del != 0 ? true : false;
        }
        return false;
    }
    
    /**
     * 删除锁
     * @return
     */
    public boolean unlock(){
        return unlock("redis");
    }
}