puzhibing
2023-11-25 53e7558400dcacecdce70e39ebfe1727740f9296
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
package com.dsh.other.util;
 
import com.alibaba.csp.sentinel.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
 
/**
 * Redis工具类
 */
@Component
public class RedisUtil {
 
    @Autowired
    private JedisPool jedisPool;
 
    private static final int TIMEOUT = 10 * 1000; // 10秒超时时间
    private static final int SLEEP_TIME = 1000; // 1秒休眠时间
 
    /**
     * 向redis中存储字符串没有过期时间
     * Storing strings in Redis without an expiration time.
     *
     * @param key
     * @param value
     */
    public void setStrValue(String key, String value) {
        if (StringUtil.isNotEmpty(key)) {
            Jedis resource = jedisPool.getResource();
            String set = resource.set(key, value);
            closeJedis(resource);
        }
    }
 
 
    /**
     * 以分钟为单位设置存储值(设置过期时间)
     * Set storage value in minutes (set expiration time) as units.
     *
     * @param key
     * @param value
     * @param time  秒
     */
    public void setStrValue(String key, String value, int time) {
        if (StringUtil.isNotEmpty(key)) {
            Jedis resource = jedisPool.getResource();
            String setex = resource.setex(key, time, value);
            closeJedis(resource);
        }
    }
 
 
    /**
     * 从redis中获取值
     *
     * @param key
     * @return
     */
    public String getValue(String key) {
        if (StringUtil.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 (StringUtil.isNotEmpty(key)) {
            Jedis resource = jedisPool.getResource();
            Long del = resource.del(key);
            closeJedis(resource);
        }
    }
 
 
    /**
     * 删除资源
     *
     * @param jedis
     */
    public void closeJedis(Jedis jedis) {
        if (null != jedis) {
            jedis.close();
        }
    }
 
 
    public boolean acquireLock(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            long start = System.currentTimeMillis();
            String[] split = key.split(";");
            for (String s : split) {
 
                while (true) {
                    // 尝试获取锁
                    String result = jedis.set(s, value, "NX", "PX", TIMEOUT);
                    if ("OK".equals(result)) {
                        return true;
                    }
                    // 超时则返回失败
                    if (System.currentTimeMillis() - start > TIMEOUT) {
                        return false;
                    }
                    // 休眠一段时间后重试
                    Thread.sleep(SLEEP_TIME);
                }
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return false;
    }
 
    public void releaseLock(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
 
 
}