Skip to content

Commit 8d6207d

Browse files
author
谢炳庭
committed
feat: JedisConnectionFactory类型改成RedisConnectionFactory,以兼容LettuceConnectionFactory
1 parent 17344e4 commit 8d6207d

File tree

12 files changed

+142
-162
lines changed

12 files changed

+142
-162
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
.settings/
55
*.bak
66
.idea/
7+
*.iml

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ public class RedisLockConfig {
9191

9292

9393
@Autowired
94-
private JedisConnectionFactory jedisConnectionFactory;
94+
private RedisConnectionFactory redisConnectionFactory;
9595

9696
@Bean
9797
public RedisLock build() {
98-
RedisLock redisLock = new RedisLock.Builder(jedisConnectionFactory,RedisToolsConstant.SINGLE)
98+
RedisLock redisLock = new RedisLock.Builder(redisConnectionFactory,RedisToolsConstant.SINGLE)
9999
.lockPrefix("lock_")
100100
.sleepTime(100)
101101
.build();
@@ -181,11 +181,11 @@ public class RedisLimitConfig {
181181
private int limit;
182182

183183
@Autowired
184-
private JedisConnectionFactory jedisConnectionFactory;
184+
private RedisConnectionFactory redisConnectionFactory;
185185

186186
@Bean
187187
public RedisLimit build() {
188-
RedisLimit redisLimit = new RedisLimit.Builder(jedisConnectionFactory, RedisToolsConstant.SINGLE)
188+
RedisLimit redisLimit = new RedisLimit.Builder(redisConnectionFactory, RedisToolsConstant.SINGLE)
189189
.limit(limit)
190190
.build();
191191
return redisLimit;

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

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.crossoverjie.distributed.limit;
22

33
import com.crossoverjie.distributed.constant.RedisToolsConstant;
4-
import com.crossoverjie.distributed.intercept.SpringMVCIntercept;
54
import com.crossoverjie.distributed.util.ScriptUtil;
65
import org.slf4j.Logger;
76
import org.slf4j.LoggerFactory;
87
import org.springframework.data.redis.connection.RedisClusterConnection;
98
import org.springframework.data.redis.connection.RedisConnection;
10-
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
9+
import org.springframework.data.redis.connection.RedisConnectionFactory;
1110
import redis.clients.jedis.Jedis;
1211
import redis.clients.jedis.JedisCluster;
13-
import redis.clients.jedis.JedisPool;
1412

1513
import java.io.IOException;
1614
import java.util.Collections;
@@ -19,16 +17,16 @@
1917
* Function: limit util
2018
*
2119
* @author crossoverJie
22-
* Date: 22/04/2018 15:54
20+
* Date: 22/04/2018 15:54
2321
* @since JDK 1.8
2422
*/
2523
public class RedisLimit {
2624

2725
private static Logger logger = LoggerFactory.getLogger(RedisLimit.class);
2826

2927

30-
private JedisConnectionFactory jedisConnectionFactory;
31-
private int type ;
28+
private RedisConnectionFactory redisConnectionFactory;
29+
private int type;
3230
private int limit = 200;
3331

3432
private static final int FAIL_CODE = 0;
@@ -39,15 +37,16 @@ public class RedisLimit {
3937
private String script;
4038

4139
private RedisLimit(Builder builder) {
42-
this.limit = builder.limit ;
43-
this.jedisConnectionFactory = builder.jedisConnectionFactory;
44-
this.type = builder.type ;
40+
this.limit = builder.limit;
41+
this.redisConnectionFactory = builder.redisConnectionFactory;
42+
this.type = builder.type;
4543
buildScript();
4644
}
4745

4846

4947
/**
5048
* limit traffic
49+
*
5150
* @return if true
5251
*/
5352
public boolean limit() {
@@ -67,32 +66,33 @@ public boolean limit() {
6766
private Object limitRequest(Object connection) {
6867
Object result = null;
6968
String key = String.valueOf(System.currentTimeMillis() / 1000);
70-
if (connection instanceof Jedis){
71-
result = ((Jedis)connection).eval(script, Collections.singletonList(key), Collections.singletonList(String.valueOf(limit)));
69+
if (connection instanceof Jedis) {
70+
result = ((Jedis) connection).eval(script, Collections.singletonList(key), Collections.singletonList(String.valueOf(limit)));
7271
((Jedis) connection).close();
73-
}else {
72+
} else {
7473
result = ((JedisCluster) connection).eval(script, Collections.singletonList(key), Collections.singletonList(String.valueOf(limit)));
7574
try {
7675
((JedisCluster) connection).close();
7776
} catch (IOException e) {
78-
logger.error("IOException",e);
77+
logger.error("IOException", e);
7978
}
8079
}
8180
return result;
8281
}
8382

8483
/**
8584
* get Redis connection
85+
*
8686
* @return
8787
*/
8888
private Object getConnection() {
89-
Object connection ;
90-
if (type == RedisToolsConstant.SINGLE){
91-
RedisConnection redisConnection = jedisConnectionFactory.getConnection();
89+
Object connection;
90+
if (type == RedisToolsConstant.SINGLE) {
91+
RedisConnection redisConnection = redisConnectionFactory.getConnection();
9292
connection = redisConnection.getNativeConnection();
93-
}else {
94-
RedisClusterConnection clusterConnection = jedisConnectionFactory.getClusterConnection();
95-
connection = clusterConnection.getNativeConnection() ;
93+
} else {
94+
RedisClusterConnection clusterConnection = redisConnectionFactory.getClusterConnection();
95+
connection = clusterConnection.getNativeConnection();
9696
}
9797
return connection;
9898
}
@@ -107,27 +107,27 @@ private void buildScript() {
107107

108108

109109
/**
110-
* the builder
110+
* the builder
111111
*/
112-
public static class Builder{
113-
private JedisConnectionFactory jedisConnectionFactory = null ;
112+
public static class Builder {
113+
private RedisConnectionFactory redisConnectionFactory = null;
114114

115115
private int limit = 200;
116-
private int type ;
116+
private int type;
117117

118118

119-
public Builder(JedisConnectionFactory jedisConnectionFactory,int type){
120-
this.jedisConnectionFactory = jedisConnectionFactory;
121-
this.type = type ;
119+
public Builder(RedisConnectionFactory redisConnectionFactory, int type) {
120+
this.redisConnectionFactory = redisConnectionFactory;
121+
this.type = type;
122122
}
123123

124-
public Builder limit(int limit){
125-
this.limit = limit ;
124+
public Builder limit(int limit) {
125+
this.limit = limit;
126126
return this;
127127
}
128128

129-
public RedisLimit build(){
130-
return new RedisLimit(this) ;
129+
public RedisLimit build() {
130+
return new RedisLimit(this);
131131
}
132132

133133
}

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

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import org.slf4j.LoggerFactory;
77
import org.springframework.data.redis.connection.RedisClusterConnection;
88
import org.springframework.data.redis.connection.RedisConnection;
9-
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
9+
import org.springframework.data.redis.connection.RedisConnectionFactory;
1010
import redis.clients.jedis.Jedis;
1111
import redis.clients.jedis.JedisCluster;
1212

@@ -16,7 +16,7 @@
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
*/
2222
public 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

src/test/java/com/crossoverjie/distributed/limit/RedisLimitJedisClusterJunitTest.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@
88
import org.mockito.Mockito;
99
import org.mockito.MockitoAnnotations;
1010
import org.springframework.data.redis.connection.RedisClusterConnection;
11-
import org.springframework.data.redis.connection.RedisConnection;
11+
import org.springframework.data.redis.connection.RedisConnectionFactory;
1212
import org.springframework.data.redis.connection.jedis.JedisClusterConnection;
13-
import org.springframework.data.redis.connection.jedis.JedisConnection;
14-
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
15-
import redis.clients.jedis.Jedis;
1613
import redis.clients.jedis.JedisCluster;
1714

1815
/**
1916
* Function:
2017
*
2118
* @author crossoverJie
22-
* Date: 27/04/2018 17:19
19+
* Date: 27/04/2018 17:19
2320
* @since JDK 1.8
2421
*/
2522
public class RedisLimitJedisClusterJunitTest {
@@ -31,13 +28,13 @@ public class RedisLimitJedisClusterJunitTest {
3128

3229

3330
@Mock
34-
private JedisConnectionFactory jedisConnectionFactory ;
31+
private RedisConnectionFactory redisConnectionFactory;
3532

3633
@Before
3734
public void setBefore() {
3835
MockitoAnnotations.initMocks(this);
3936

40-
redisLimit = new RedisLimit.Builder(jedisConnectionFactory, RedisToolsConstant.CLUSTER)
37+
redisLimit = new RedisLimit.Builder(redisConnectionFactory, RedisToolsConstant.CLUSTER)
4138
.limit(100)
4239
.build();
4340

@@ -46,10 +43,10 @@ public void setBefore() {
4643
@Test
4744
public void limit() {
4845
RedisClusterConnection clusterConnection = new JedisClusterConnection(jedisCluster);
49-
Mockito.when(jedisConnectionFactory.getClusterConnection()).thenReturn(clusterConnection);
46+
Mockito.when(redisConnectionFactory.getClusterConnection()).thenReturn(clusterConnection);
5047

51-
jedisCluster = (JedisCluster)clusterConnection.getNativeConnection();
52-
Mockito.when(jedisCluster.eval(Mockito.anyString(), Mockito.anyList(), Mockito.anyList())).thenReturn(0L) ;
48+
jedisCluster = (JedisCluster) clusterConnection.getNativeConnection();
49+
Mockito.when(jedisCluster.eval(Mockito.anyString(), Mockito.anyList(), Mockito.anyList())).thenReturn(0L);
5350

5451
boolean limit = redisLimit.limit();
5552
System.out.println("limit=" + limit);
@@ -62,10 +59,10 @@ public void limit() {
6259
public void limitTrue() {
6360

6461
RedisClusterConnection clusterConnection = new JedisClusterConnection(jedisCluster);
65-
Mockito.when(jedisConnectionFactory.getClusterConnection()).thenReturn(clusterConnection);
62+
Mockito.when(redisConnectionFactory.getClusterConnection()).thenReturn(clusterConnection);
6663

67-
jedisCluster = (JedisCluster)clusterConnection.getNativeConnection();
68-
Mockito.when(jedisCluster.eval(Mockito.anyString(), Mockito.anyList(), Mockito.anyList())).thenReturn(1L) ;
64+
jedisCluster = (JedisCluster) clusterConnection.getNativeConnection();
65+
Mockito.when(jedisCluster.eval(Mockito.anyString(), Mockito.anyList(), Mockito.anyList())).thenReturn(1L);
6966

7067
boolean limit = redisLimit.limit();
7168
System.out.println("limit=" + limit);

0 commit comments

Comments
 (0)