Skip to content

Commit aaf3a82

Browse files
authored
Merge pull request #5 from crossoverJie/dev-1.0.4
Dev 1.0.4
2 parents 4f87966 + b8d5775 commit aaf3a82

File tree

9 files changed

+415
-130
lines changed

9 files changed

+415
-130
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<groupId>top.crossoverjie.opensource</groupId>
1515
<artifactId>distributed-redis-tool</artifactId>
1616
<packaging>jar</packaging>
17-
<version>1.0.3</version>
17+
<version>1.0.4</version>
1818
<name>distributed-tools</name>
1919
<description>This is a simple distributed tools based on Redis.</description>
2020
<inceptionYear>2018</inceptionYear>

src/main/java/com/crossoverjie/distributed/constant/RedisToolsConstant.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.crossoverjie.distributed.constant;
22

33
/**
4-
* Function:
4+
* Function: Redis Cluster or Single Redis
55
*
66
* @author crossoverJie
77
* Date: 06/05/2018 01:03

src/main/java/com/crossoverjie/distributed/limit/RedisLimit.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import java.util.Collections;
1717

1818
/**
19-
* Function:
19+
* Function: limit util
2020
*
2121
* @author crossoverJie
2222
* Date: 22/04/2018 15:54
@@ -81,6 +81,10 @@ private Object limitRequest(Object connection) {
8181
return result;
8282
}
8383

84+
/**
85+
* get Redis connection
86+
* @return
87+
*/
8488
private Object getConnection() {
8589
Object connection ;
8690
if (type == RedisToolsConstant.SINGLE){

src/main/java/com/crossoverjie/distributed/lock/RedisLock.java

Lines changed: 108 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package com.crossoverjie.distributed.lock;
22

3+
import com.crossoverjie.distributed.constant.RedisToolsConstant;
4+
import com.crossoverjie.distributed.limit.RedisLimit;
35
import 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;
411
import redis.clients.jedis.Jedis;
512
import redis.clients.jedis.JedisCluster;
613
import redis.clients.jedis.JedisCommands;
714

15+
import java.io.IOException;
816
import java.util.Collections;
917

1018
/**
@@ -15,7 +23,7 @@
1523
* @since JDK 1.8
1624
*/
1725
public 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) {

src/test/java/com/crossoverjie/distributed/lock/RedisClusterLockTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,51 @@
11
package com.crossoverjie.distributed.lock;
22

3+
import com.crossoverjie.distributed.constant.RedisToolsConstant;
34
import junit.framework.Assert;
45
import org.junit.Before;
56
import org.junit.Test;
67
import org.mockito.InjectMocks;
78
import org.mockito.Mock;
89
import org.mockito.Mockito;
910
import org.mockito.MockitoAnnotations;
11+
import org.springframework.data.redis.connection.RedisClusterConnection;
12+
import org.springframework.data.redis.connection.jedis.JedisClusterConnection;
13+
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
1014
import redis.clients.jedis.JedisCluster;
1115

1216

1317
import java.util.UUID;
1418

15-
1619
public class RedisClusterLockTest {
1720

1821

1922
private RedisLock redisLock;
2023

24+
@Mock
25+
private JedisConnectionFactory jedisConnectionFactory ;
26+
2127
@Mock
2228
private JedisCluster jedisCluster;
2329

2430
@Before
2531
public void setBefore() {
2632
MockitoAnnotations.initMocks(this);
2733

28-
redisLock = new RedisLock.Builder(jedisCluster)
29-
.lockPrefix("lock_test")
34+
redisLock = new RedisLock.Builder(jedisConnectionFactory, RedisToolsConstant.CLUSTER)
35+
.lockPrefix("lock_")
3036
.sleepTime(100)
3137
.build();
3238

33-
//redisLock = new RedisLock();
34-
//HostAndPort hostAndPort = new HostAndPort("10.19.13.51", 7000);
35-
//JedisCluster jedisCluster = new JedisCluster(hostAndPort);
39+
40+
RedisClusterConnection clusterConnection = new JedisClusterConnection(jedisCluster);
41+
Mockito.when(jedisConnectionFactory.getClusterConnection()).thenReturn(clusterConnection);
42+
jedisCluster = (JedisCluster)clusterConnection.getNativeConnection();
43+
3644
}
3745

3846
@Test
3947
public void tryLock() throws Exception {
48+
4049
String key = "test";
4150
String request = UUID.randomUUID().toString();
4251
Mockito.when(jedisCluster.set(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),

0 commit comments

Comments
 (0)