puzhibing
2 天以前 ba6b508a44cb1f0730c6a27a5d73b8d2ae8f1d4b
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
package com.supersavedriving.user.modular.system.util;
 
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
 
 
/**
 * Redis工具类
 */
@Component
public class RedisUtil {
 
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
 
 
 
    /**
     * 向redis中存储字符串没有过期时间
     * @param key
     * @param value
     */
    public void setStrValue(String key, String value){
        redisTemplate.opsForValue().set(key, value);
    }
 
 
    /**
     * 以分钟为单位设置存储值(设置过期时间)
     * @param key
     * @param value
     * @param time 秒
     */
    public void setStrValue(String key, String value, int time){
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }
 
 
    /**
     * 从redis中获取值
     * @param key
     * @return
     */
    public String getValue(String key){
        return (String) redisTemplate.opsForValue().get(key);
    }
 
 
 
    /**
     * 删除key
     * @param key
     */
    public void remove(String key){
        redisTemplate.delete(key);
    }
 
 
 
 
    /**
     * 删除Set集合中的值
     * @param key
     * @param members
     */
    public void delSetValue(String key, String...members){
        redisTemplate.opsForSet().remove(key, members);
    }
 
 
 
    /**
     * redis加锁
     * @param key
     * @param value
     * @param time
     * @return
     */
    public boolean lock(String key, String value, int time){
        key += "_lock";
        return redisTemplate.opsForValue().setIfAbsent(key, value);
    }
 
    /**
     * 获取redis锁
     * @return
     */
    public boolean lock(){
        try {
            boolean b = lock(5);
            if(!b){
                int num1 = 1;
                while (num1 <= 10){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    b = lock(5);
                    if(b){
                        return true;
                    }else{
                        num1++;
                    }
                }
                return false;
            }
            return b;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            unlock();
        }
        return false;
    }
 
 
    public boolean lock(String key){
        try {
            boolean b = lock(key,5);
            if(!b){
                int num1 = 1;
                while (num1 <= 10){
                    try {
                        Thread.sleep(1000);//等待1秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    b = lock(5);
                    if(b){
                        return true;
                    }else{
                        num1++;
                    }
                }
                return false;
            }
            return b;
        }catch (Exception e){
            e.printStackTrace();
            unlock();
        }
        return false;
    }
 
 
    /**
     * 获取redis锁
     * @param time
     * @return
     */
    public boolean lock(String key, int time){
        String uuid = UUID.randomUUID().toString();
        return lock(key, uuid, time);
    }
 
 
 
    /**
     * 获取redis锁
     * @param time
     * @return
     */
    public boolean lock(int time){
        String uuid = UUID.randomUUID().toString();
        return lock("redis", uuid, time);
    }
 
 
    /**
     * redis释放锁
     * @param key
     * @return
     */
    public boolean unlock(String key){
        return redisTemplate.delete(key);
    }
 
    /**
     * 删除锁
     * @return
     */
    public boolean unlock(){
        return unlock("redis");
    }
}