11package com .crossoverjie .distributed .lock ;
22
3+ import com .crossoverjie .distributed .constant .RedisToolsConstant ;
4+ import com .crossoverjie .distributed .limit .RedisLimit ;
35import com .crossoverjie .distributed .util .ScriptUtil ;
6+ import org .slf4j .Logger ;
7+ import org .slf4j .LoggerFactory ;
8+ import org .springframework .data .redis .connection .RedisClusterConnection ;
9+ import org .springframework .data .redis .connection .RedisConnection ;
10+ import org .springframework .data .redis .connection .jedis .JedisConnectionFactory ;
411import redis .clients .jedis .Jedis ;
512import redis .clients .jedis .JedisCluster ;
613import redis .clients .jedis .JedisCommands ;
714
15+ import java .io .IOException ;
816import java .util .Collections ;
917
1018/**
1523 * @since JDK 1.8
1624 */
1725public class RedisLock {
18-
26+ private static Logger logger = LoggerFactory . getLogger ( RedisLock . class );
1927
2028 private static final String LOCK_MSG = "OK" ;
2129
@@ -27,11 +35,10 @@ public class RedisLock {
2735
2836 private String lockPrefix ;
2937
30-
3138 private int sleepTime ;
3239
33-
34- private JedisCommands jedis ;
40+ private JedisConnectionFactory jedisConnectionFactory ;
41+ private int type ;
3542
3643 /**
3744 * time millisecond
@@ -44,13 +51,31 @@ public class RedisLock {
4451 private String script ;
4552
4653 private RedisLock (Builder builder ) {
47- this .jedis = builder .jedis ;
54+ this .jedisConnectionFactory = builder .jedisConnectionFactory ;
55+ this .type = builder .type ;
4856 this .lockPrefix = builder .lockPrefix ;
4957 this .sleepTime = builder .sleepTime ;
5058
5159 buildScript ();
5260 }
5361
62+
63+ /**
64+ * get Redis connection
65+ * @return
66+ */
67+ private Object getConnection () {
68+ Object connection ;
69+ if (type == RedisToolsConstant .SINGLE ){
70+ RedisConnection redisConnection = jedisConnectionFactory .getConnection ();
71+ connection = redisConnection .getNativeConnection ();
72+ }else {
73+ RedisClusterConnection clusterConnection = jedisConnectionFactory .getClusterConnection ();
74+ connection = clusterConnection .getNativeConnection () ;
75+ }
76+ return connection ;
77+ }
78+
5479 /**
5580 * Non-blocking lock
5681 *
@@ -60,7 +85,20 @@ private RedisLock(Builder builder) {
6085 * false lock fail
6186 */
6287 public boolean tryLock (String key , String request ) {
63- String result = this .jedis .set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME );
88+ //get connection
89+ Object connection = getConnection ();
90+ String result ;
91+ if (connection instanceof Jedis ){
92+ result = ((Jedis ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME );
93+ ((Jedis ) connection ).close ();
94+ }else {
95+ result = ((JedisCluster ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME );
96+ try {
97+ ((JedisCluster ) connection ).close ();
98+ } catch (IOException e ) {
99+ logger .error ("IOException" ,e );
100+ }
101+ }
64102
65103 if (LOCK_MSG .equals (result )) {
66104 return true ;
@@ -76,9 +114,22 @@ public boolean tryLock(String key, String request) {
76114 * @param request
77115 */
78116 public void lock (String key , String request ) throws InterruptedException {
117+ //get connection
118+ Object connection = getConnection ();
119+ String result ;
120+ for (; ;) {
121+ if (connection instanceof Jedis ){
122+ result = ((Jedis )connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME );
123+ ((Jedis ) connection ).close ();
124+ }else {
125+ result = ((JedisCluster )connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME );
126+ try {
127+ ((JedisCluster ) connection ).close ();
128+ } catch (IOException e ) {
129+ logger .error ("IOException" ,e );
130+ }
131+ }
79132
80- for (; ; ) {
81- String result = this .jedis .set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME );
82133 if (LOCK_MSG .equals (result )) {
83134 break ;
84135 }
@@ -99,9 +150,21 @@ public void lock(String key, String request) throws InterruptedException {
99150 */
100151 public boolean lock (String key , String request , int blockTime ) throws InterruptedException {
101152
153+ //get connection
154+ Object connection = getConnection ();
155+ String result ;
102156 while (blockTime >= 0 ) {
103-
104- String result = this .jedis .set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME );
157+ if (connection instanceof Jedis ){
158+ result = ((Jedis ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME ) ;
159+ ((Jedis ) connection ).close ();
160+ }else {
161+ result = ((JedisCluster ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , 10 * TIME ) ;
162+ try {
163+ ((JedisCluster ) connection ).close ();
164+ } catch (IOException e ) {
165+ logger .error ("IOException" ,e );
166+ }
167+ }
105168 if (LOCK_MSG .equals (result )) {
106169 return true ;
107170 }
@@ -123,7 +186,21 @@ public boolean lock(String key, String request, int blockTime) throws Interrupte
123186 * false lock fail
124187 */
125188 public boolean tryLock (String key , String request , int expireTime ) {
126- String result = this .jedis .set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , expireTime );
189+ //get connection
190+ Object connection = getConnection ();
191+ String result ;
192+
193+ if (connection instanceof Jedis ){
194+ result = ((Jedis ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , expireTime );
195+ ((Jedis ) connection ).close ();
196+ }else {
197+ result = ((JedisCluster ) connection ).set (lockPrefix + key , request , SET_IF_NOT_EXIST , SET_WITH_EXPIRE_TIME , expireTime );
198+ try {
199+ ((JedisCluster ) connection ).close ();
200+ } catch (IOException e ) {
201+ logger .error ("IOException" ,e );
202+ }
203+ }
127204
128205 if (LOCK_MSG .equals (result )) {
129206 return true ;
@@ -141,13 +218,22 @@ public boolean tryLock(String key, String request, int expireTime) {
141218 * @return
142219 */
143220 public boolean unlock (String key , String request ) {
221+
222+ //get connection
223+ Object connection = getConnection ();
144224 //lua script
145225
146226 Object result = null ;
147- if (jedis instanceof Jedis ) {
148- result = ((Jedis ) this .jedis ).eval (script , Collections .singletonList (lockPrefix + key ), Collections .singletonList (request ));
149- } else if (jedis instanceof JedisCluster ) {
150- result = ((JedisCluster ) this .jedis ).eval (script , Collections .singletonList (lockPrefix + key ), Collections .singletonList (request ));
227+ if (connection instanceof Jedis ) {
228+ result = ((Jedis ) connection ).eval (script , Collections .singletonList (lockPrefix + key ), Collections .singletonList (request ));
229+ ((Jedis ) connection ).close ();
230+ } else if (connection instanceof JedisCluster ) {
231+ result = ((JedisCluster ) connection ).eval (script , Collections .singletonList (lockPrefix + key ), Collections .singletonList (request ));
232+ try {
233+ ((JedisCluster ) connection ).close ();
234+ } catch (IOException e ) {
235+ logger .error ("IOException" ,e );
236+ }
151237 } else {
152238 //throw new RuntimeException("instance is error") ;
153239 return false ;
@@ -169,20 +255,23 @@ private void buildScript() {
169255 }
170256
171257
172- public static class Builder < T extends JedisCommands > {
258+ public static class Builder {
173259 private static final String DEFAULT_LOCK_PREFIX = "lock_" ;
174260 /**
175261 * default sleep time
176262 */
177263 private static final int DEFAULT_SLEEP_TIME = 100 ;
178264
179- private T jedis ;
265+ private JedisConnectionFactory jedisConnectionFactory = null ;
266+
267+ private int type ;
180268
181269 private String lockPrefix = DEFAULT_LOCK_PREFIX ;
182270 private int sleepTime = DEFAULT_SLEEP_TIME ;
183271
184- public Builder (T jedis ) {
185- this .jedis = jedis ;
272+ public Builder (JedisConnectionFactory jedisConnectionFactory , int type ) {
273+ this .jedisConnectionFactory = jedisConnectionFactory ;
274+ this .type = type ;
186275 }
187276
188277 public Builder lockPrefix (String lockPrefix ) {
0 commit comments