/
AutoExpireRedisLock.java
58 lines (51 loc) · 1.57 KB
/
AutoExpireRedisLock.java
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
package love.wangqi.version3_2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author: wangqi
* @description:
* @date: Created in 2018/8/7 下午3:36
*/
public class AutoExpireRedisLock extends RedisLock {
final static Logger logger = LoggerFactory.getLogger(AutoExpireRedisLock.class);
private ThreadLocal<Thread> expireThread;
public AutoExpireRedisLock() {
expireThread = new ThreadLocal<>();
}
@Override
public Boolean lock(String key, long timeout) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
if (isLocked(key)) {
expire(key, timeout);
}
long sleeptime = timeout / 2 > 0 ? 1000 * (timeout / 2) : 600;
try {
Thread.sleep(sleeptime);
} catch (InterruptedException e) {
break;
}
}
}
});
Boolean result = super.lock(key, timeout);
if (result) {
if (expireThread.get() == null) {
thread.setDaemon(true);
thread.start();
expireThread.set(thread);
}
}
return result;
}
@Override
public void unlock(String key) {
if (expireThread.get() != null) {
expireThread.get().interrupt();
expireThread.remove();
}
super.unlock(key);
}
}