6
6
import org .slf4j .LoggerFactory ;
7
7
import org .springframework .data .redis .connection .RedisClusterConnection ;
8
8
import org .springframework .data .redis .connection .RedisConnection ;
9
- import org .springframework .data .redis .connection .jedis . JedisConnectionFactory ;
9
+ import org .springframework .data .redis .connection .RedisConnectionFactory ;
10
10
import redis .clients .jedis .Jedis ;
11
11
import redis .clients .jedis .JedisCluster ;
12
12
16
16
* Function: distributed lock
17
17
*
18
18
* @author crossoverJie
19
- * Date: 26/03/2018 11:09
19
+ * Date: 26/03/2018 11:09
20
20
* @since JDK 1.8
21
21
*/
22
22
public class RedisLock {
@@ -34,8 +34,8 @@ public class RedisLock {
34
34
35
35
private int sleepTime ;
36
36
37
- private JedisConnectionFactory jedisConnectionFactory ;
38
- private int type ;
37
+ private RedisConnectionFactory redisConnectionFactory ;
38
+ private int type ;
39
39
40
40
/**
41
41
* time millisecond
@@ -48,8 +48,8 @@ public class RedisLock {
48
48
private String script ;
49
49
50
50
private RedisLock (Builder builder ) {
51
- this .jedisConnectionFactory = builder .jedisConnectionFactory ;
52
- this .type = builder .type ;
51
+ this .redisConnectionFactory = builder .redisConnectionFactory ;
52
+ this .type = builder .type ;
53
53
this .lockPrefix = builder .lockPrefix ;
54
54
this .sleepTime = builder .sleepTime ;
55
55
@@ -59,16 +59,17 @@ private RedisLock(Builder builder) {
59
59
60
60
/**
61
61
* get Redis connection
62
+ *
62
63
* @return
63
64
*/
64
65
private Object getConnection () {
65
- Object connection ;
66
- if (type == RedisToolsConstant .SINGLE ){
67
- RedisConnection redisConnection = jedisConnectionFactory .getConnection ();
66
+ Object connection ;
67
+ if (type == RedisToolsConstant .SINGLE ) {
68
+ RedisConnection redisConnection = redisConnectionFactory .getConnection ();
68
69
connection = redisConnection .getNativeConnection ();
69
- }else {
70
- RedisClusterConnection clusterConnection = jedisConnectionFactory .getClusterConnection ();
71
- connection = clusterConnection .getNativeConnection () ;
70
+ } else {
71
+ RedisClusterConnection clusterConnection = redisConnectionFactory .getClusterConnection ();
72
+ connection = clusterConnection .getNativeConnection ();
72
73
}
73
74
return connection ;
74
75
}
@@ -82,7 +83,7 @@ private Object getConnection() {
82
83
* false lock fail
83
84
*/
84
85
public boolean tryLock (String key , String request ) {
85
- return tryLock (key ,request ,10 * TIME );
86
+ return tryLock (key , request , 10 * TIME );
86
87
}
87
88
88
89
/**
@@ -94,15 +95,15 @@ public boolean tryLock(String key, String request) {
94
95
public void lock (String key , String request ) throws InterruptedException {
95
96
//get connection
96
97
Object connection = getConnection ();
97
- String result ;
98
- for (; ;) {
99
- if (connection instanceof Jedis ){
100
- result = ((Jedis )connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME );
101
- if (LOCK_MSG .equals (result )){
98
+ String result ;
99
+ for (; ; ) {
100
+ if (connection instanceof Jedis ) {
101
+ result = ((Jedis ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME );
102
+ if (LOCK_MSG .equals (result )) {
102
103
((Jedis ) connection ).close ();
103
104
}
104
- }else {
105
- result = ((JedisCluster )connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME );
105
+ } else {
106
+ result = ((JedisCluster ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME );
106
107
}
107
108
108
109
if (LOCK_MSG .equals (result )) {
@@ -127,15 +128,15 @@ public boolean lock(String key, String request, int blockTime) throws Interrupte
127
128
128
129
//get connection
129
130
Object connection = getConnection ();
130
- String result ;
131
+ String result ;
131
132
while (blockTime >= 0 ) {
132
- if (connection instanceof Jedis ){
133
- result = ((Jedis ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME ) ;
134
- if (LOCK_MSG .equals (result )){
133
+ if (connection instanceof Jedis ) {
134
+ result = ((Jedis ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME );
135
+ if (LOCK_MSG .equals (result )) {
135
136
((Jedis ) connection ).close ();
136
137
}
137
- }else {
138
- result = ((JedisCluster ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME ) ;
138
+ } else {
139
+ result = ((JedisCluster ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME );
139
140
}
140
141
if (LOCK_MSG .equals (result )) {
141
142
return true ;
@@ -160,12 +161,12 @@ public boolean lock(String key, String request, int blockTime) throws Interrupte
160
161
public boolean tryLock (String key , String request , int expireTime ) {
161
162
//get connection
162
163
Object connection = getConnection ();
163
- String result ;
164
+ String result ;
164
165
165
- if (connection instanceof Jedis ){
166
+ if (connection instanceof Jedis ) {
166
167
result = ((Jedis ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , expireTime );
167
168
((Jedis ) connection ).close ();
168
- }else {
169
+ } else {
169
170
result = ((JedisCluster ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , expireTime );
170
171
}
171
172
@@ -225,15 +226,15 @@ public static class Builder {
225
226
*/
226
227
private static final int DEFAULT_SLEEP_TIME = 100 ;
227
228
228
- private JedisConnectionFactory jedisConnectionFactory = null ;
229
+ private RedisConnectionFactory redisConnectionFactory = null ;
229
230
230
- private int type ;
231
+ private int type ;
231
232
232
233
private String lockPrefix = DEFAULT_LOCK_PREFIX ;
233
234
private int sleepTime = DEFAULT_SLEEP_TIME ;
234
235
235
- public Builder (JedisConnectionFactory jedisConnectionFactory , int type ) {
236
- this .jedisConnectionFactory = jedisConnectionFactory ;
236
+ public Builder (RedisConnectionFactory redisConnectionFactory , int type ) {
237
+ this .redisConnectionFactory = redisConnectionFactory ;
237
238
this .type = type ;
238
239
}
239
240
0 commit comments