66import org .slf4j .LoggerFactory ;
77import org .springframework .data .redis .connection .RedisClusterConnection ;
88import org .springframework .data .redis .connection .RedisConnection ;
9- import org .springframework .data .redis .connection .jedis . JedisConnectionFactory ;
9+ import org .springframework .data .redis .connection .RedisConnectionFactory ;
1010import redis .clients .jedis .Jedis ;
1111import redis .clients .jedis .JedisCluster ;
1212
1616 * Function: distributed lock
1717 *
1818 * @author crossoverJie
19- * Date: 26/03/2018 11:09
19+ * Date: 26/03/2018 11:09
2020 * @since JDK 1.8
2121 */
2222public class RedisLock {
@@ -34,8 +34,8 @@ public class RedisLock {
3434
3535 private int sleepTime ;
3636
37- private JedisConnectionFactory jedisConnectionFactory ;
38- private int type ;
37+ private RedisConnectionFactory redisConnectionFactory ;
38+ private int type ;
3939
4040 /**
4141 * time millisecond
@@ -48,8 +48,8 @@ public class RedisLock {
4848 private String script ;
4949
5050 private RedisLock (Builder builder ) {
51- this .jedisConnectionFactory = builder .jedisConnectionFactory ;
52- this .type = builder .type ;
51+ this .redisConnectionFactory = builder .redisConnectionFactory ;
52+ this .type = builder .type ;
5353 this .lockPrefix = builder .lockPrefix ;
5454 this .sleepTime = builder .sleepTime ;
5555
@@ -59,16 +59,17 @@ private RedisLock(Builder builder) {
5959
6060 /**
6161 * get Redis connection
62+ *
6263 * @return
6364 */
6465 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 ();
6869 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 ();
7273 }
7374 return connection ;
7475 }
@@ -82,7 +83,7 @@ private Object getConnection() {
8283 * false lock fail
8384 */
8485 public boolean tryLock (String key , String request ) {
85- return tryLock (key ,request ,10 * TIME );
86+ return tryLock (key , request , 10 * TIME );
8687 }
8788
8889 /**
@@ -94,15 +95,15 @@ public boolean tryLock(String key, String request) {
9495 public void lock (String key , String request ) throws InterruptedException {
9596 //get connection
9697 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 )) {
102103 ((Jedis ) connection ).close ();
103104 }
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 );
106107 }
107108
108109 if (LOCK_MSG .equals (result )) {
@@ -127,15 +128,15 @@ public boolean lock(String key, String request, int blockTime) throws Interrupte
127128
128129 //get connection
129130 Object connection = getConnection ();
130- String result ;
131+ String result ;
131132 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 )) {
135136 ((Jedis ) connection ).close ();
136137 }
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 );
139140 }
140141 if (LOCK_MSG .equals (result )) {
141142 return true ;
@@ -160,12 +161,12 @@ public boolean lock(String key, String request, int blockTime) throws Interrupte
160161 public boolean tryLock (String key , String request , int expireTime ) {
161162 //get connection
162163 Object connection = getConnection ();
163- String result ;
164+ String result ;
164165
165- if (connection instanceof Jedis ){
166+ if (connection instanceof Jedis ) {
166167 result = ((Jedis ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , expireTime );
167168 ((Jedis ) connection ).close ();
168- }else {
169+ } else {
169170 result = ((JedisCluster ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , expireTime );
170171 }
171172
@@ -225,15 +226,15 @@ public static class Builder {
225226 */
226227 private static final int DEFAULT_SLEEP_TIME = 100 ;
227228
228- private JedisConnectionFactory jedisConnectionFactory = null ;
229+ private RedisConnectionFactory redisConnectionFactory = null ;
229230
230- private int type ;
231+ private int type ;
231232
232233 private String lockPrefix = DEFAULT_LOCK_PREFIX ;
233234 private int sleepTime = DEFAULT_SLEEP_TIME ;
234235
235- public Builder (JedisConnectionFactory jedisConnectionFactory , int type ) {
236- this .jedisConnectionFactory = jedisConnectionFactory ;
236+ public Builder (RedisConnectionFactory redisConnectionFactory , int type ) {
237+ this .redisConnectionFactory = redisConnectionFactory ;
237238 this .type = type ;
238239 }
239240
0 commit comments