-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisUtils.java
More file actions
193 lines (180 loc) · 4.26 KB
/
Copy pathRedisUtils.java
File metadata and controls
193 lines (180 loc) · 4.26 KB
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
package redis;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import redis.clients.jedis.Jedis;
public class RedisUtils {
/**
* 锁的有效时间(单位:毫秒)
*/
public static final int expired = 100;
private static Jedis jedis = null;
private static Lock lock = null;
/**
* 锁超时时间(单位:秒)
*/
public static final int lockTimeoutS = 1;
public static void init() {
jedis = new Jedis("192.168.96.140", 9010, 1000);
jedis.auth("yangxp");
System.out.println("连接成功");
// 查看服务是否运行
System.out.println("服务正在运行: " + jedis.ping());
}
/**
* 获取Jedis实例
*
* @return Jedis实例, 获取失败时返回null.
*/
public synchronized static Jedis getJedis() {
if (jedis == null) {
init();
return jedis;
} else {
return jedis;
}
}
/**
* Increment the number stored at field in the hash at key by value
*
* @param key
* @param field
* @param value
* @return
*/
public static long hincrBy(String key, String field, long value) {
Jedis redis = RedisUtils.getJedis();
if (redis == null) {
return 0;
}
long result = 0;
try {
result = redis.hincrBy(key, field, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(redis);
}
return result;
}
/**
* 获取指定的hash field
*
* @param key
* @param field
* @return 关联的值, 没有值为null.
* @see Jedis#hget(String, String)
*/
public static String hget(String key, String field) {
Jedis redis = RedisUtils.getJedis();
if (redis == null) {
return null;
}
String value = null;
try {
value = redis.hget(key, field);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(redis);
}
return value;
}
/**
* 设置hash field为指定值,如果key不存在,则先创建
*
* @param key 决定使用哪个HashMap
* @param field 相当于HashMap的key
* @param value 相当于HashMap的值
* @see Jedis#hset(String, String, String)
*/
public static void hset(String key, String field, String value) {
Jedis redis = RedisUtils.getJedis();
if (redis == null) {
return;
}
try {
redis.hset(key, field, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(redis);
}
}
/**
* 获取在哈希表中指定 key 的所有字段和值
*
* @param key
* @return
* @see Jedis#hgetAll(String)
*/
public static Map<String, String> hgetAll(String key) {
Jedis redis = RedisUtils.getJedis();
if (redis == null) {
return null;
}
Map<String, String> values = null;
try {
values = redis.hgetAll(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(redis);
}
return values;
}
/**
* 在使用完后关闭
*
* @param redis
*/
public static void close(Jedis redis) {
if (redis == null) {
return;
}
try {
redis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 尝试索取分布锁()
*
* @param lockName
* @return 是否拿到锁
*/
public static boolean acquireLock(String lockName, Jedis redis) {
if (redis == null) {
return false;
}
boolean isSuccess = false;
long value = System.currentTimeMillis() + expired;
long time = redis.setnx(lockName, String.valueOf(value));
// 成功获得锁
if (time == 1) {
// 过期时间1s 保证准确性
redis.expire(lockName, lockTimeoutS);
isSuccess = true;
} else {
// 防止多个线程同时竞争
String string = redis.get(lockName);
if (string == null) {
return isSuccess;
}
long lockTime = Long.parseLong(redis.get(lockName));
// 超时删除锁
if (lockTime < System.currentTimeMillis()) {
redis.del(lockName);
isSuccess = true;
}
try {
// 线程休眠 防止饥饿
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return isSuccess;
}
}