Skip to content

feat: JedisConnectionFactory类型改成RedisConnectionFactory,以兼容LettuceConnectionFactory #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.settings/
*.bak
.idea/
*.iml
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public class RedisLockConfig {


@Autowired
private JedisConnectionFactory jedisConnectionFactory;
private RedisConnectionFactory redisConnectionFactory;

@Bean
public RedisLock build() {
RedisLock redisLock = new RedisLock.Builder(jedisConnectionFactory,RedisToolsConstant.SINGLE)
RedisLock redisLock = new RedisLock.Builder(redisConnectionFactory,RedisToolsConstant.SINGLE)
.lockPrefix("lock_")
.sleepTime(100)
.build();
Expand Down Expand Up @@ -181,11 +181,11 @@ public class RedisLimitConfig {
private int limit;

@Autowired
private JedisConnectionFactory jedisConnectionFactory;
private RedisConnectionFactory redisConnectionFactory;

@Bean
public RedisLimit build() {
RedisLimit redisLimit = new RedisLimit.Builder(jedisConnectionFactory, RedisToolsConstant.SINGLE)
RedisLimit redisLimit = new RedisLimit.Builder(redisConnectionFactory, RedisToolsConstant.SINGLE)
.limit(limit)
.build();
return redisLimit;
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<properties>
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.7</slf4j.version>
<spring.version>4.2.1.RELEASE</spring.version>
<spring.version>5.2.1.RELEASE</spring.version>
</properties>


Expand Down
60 changes: 30 additions & 30 deletions src/main/java/com/crossoverjie/distributed/limit/RedisLimit.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.crossoverjie.distributed.limit;

import com.crossoverjie.distributed.constant.RedisToolsConstant;
import com.crossoverjie.distributed.intercept.SpringMVCIntercept;
import com.crossoverjie.distributed.util.ScriptUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.Collections;
Expand All @@ -19,16 +17,16 @@
* Function: limit util
*
* @author crossoverJie
* Date: 22/04/2018 15:54
* Date: 22/04/2018 15:54
* @since JDK 1.8
*/
public class RedisLimit {

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


private JedisConnectionFactory jedisConnectionFactory;
private int type ;
private RedisConnectionFactory redisConnectionFactory;
private int type;
private int limit = 200;

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

private RedisLimit(Builder builder) {
this.limit = builder.limit ;
this.jedisConnectionFactory = builder.jedisConnectionFactory;
this.type = builder.type ;
this.limit = builder.limit;
this.redisConnectionFactory = builder.redisConnectionFactory;
this.type = builder.type;
buildScript();
}


/**
* limit traffic
*
* @return if true
*/
public boolean limit() {
Expand All @@ -67,32 +66,33 @@ public boolean limit() {
private Object limitRequest(Object connection) {
Object result = null;
String key = String.valueOf(System.currentTimeMillis() / 1000);
if (connection instanceof Jedis){
result = ((Jedis)connection).eval(script, Collections.singletonList(key), Collections.singletonList(String.valueOf(limit)));
if (connection instanceof Jedis) {
result = ((Jedis) connection).eval(script, Collections.singletonList(key), Collections.singletonList(String.valueOf(limit)));
((Jedis) connection).close();
}else {
} else {
result = ((JedisCluster) connection).eval(script, Collections.singletonList(key), Collections.singletonList(String.valueOf(limit)));
try {
((JedisCluster) connection).close();
} catch (IOException e) {
logger.error("IOException",e);
logger.error("IOException", e);
}
}
return result;
}

/**
* get Redis connection
*
* @return
*/
private Object getConnection() {
Object connection ;
if (type == RedisToolsConstant.SINGLE){
RedisConnection redisConnection = jedisConnectionFactory.getConnection();
Object connection;
if (type == RedisToolsConstant.SINGLE) {
RedisConnection redisConnection = redisConnectionFactory.getConnection();
connection = redisConnection.getNativeConnection();
}else {
RedisClusterConnection clusterConnection = jedisConnectionFactory.getClusterConnection();
connection = clusterConnection.getNativeConnection() ;
} else {
RedisClusterConnection clusterConnection = redisConnectionFactory.getClusterConnection();
connection = clusterConnection.getNativeConnection();
}
return connection;
}
Expand All @@ -107,27 +107,27 @@ private void buildScript() {


/**
* the builder
* the builder
*/
public static class Builder{
private JedisConnectionFactory jedisConnectionFactory = null ;
public static class Builder {
private RedisConnectionFactory redisConnectionFactory = null;

private int limit = 200;
private int type ;
private int type;


public Builder(JedisConnectionFactory jedisConnectionFactory,int type){
this.jedisConnectionFactory = jedisConnectionFactory;
this.type = type ;
public Builder(RedisConnectionFactory redisConnectionFactory, int type) {
this.redisConnectionFactory = redisConnectionFactory;
this.type = type;
}

public Builder limit(int limit){
this.limit = limit ;
public Builder limit(int limit) {
this.limit = limit;
return this;
}

public RedisLimit build(){
return new RedisLimit(this) ;
public RedisLimit build() {
return new RedisLimit(this);
}

}
Expand Down
67 changes: 34 additions & 33 deletions src/main/java/com/crossoverjie/distributed/lock/RedisLock.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

Expand All @@ -16,7 +16,7 @@
* Function: distributed lock
*
* @author crossoverJie
* Date: 26/03/2018 11:09
* Date: 26/03/2018 11:09
* @since JDK 1.8
*/
public class RedisLock {
Expand All @@ -34,8 +34,8 @@ public class RedisLock {

private int sleepTime;

private JedisConnectionFactory jedisConnectionFactory;
private int type ;
private RedisConnectionFactory redisConnectionFactory;
private int type;

/**
* time millisecond
Expand All @@ -48,8 +48,8 @@ public class RedisLock {
private String script;

private RedisLock(Builder builder) {
this.jedisConnectionFactory = builder.jedisConnectionFactory;
this.type = builder.type ;
this.redisConnectionFactory = builder.redisConnectionFactory;
this.type = builder.type;
this.lockPrefix = builder.lockPrefix;
this.sleepTime = builder.sleepTime;

Expand All @@ -59,16 +59,17 @@ private RedisLock(Builder builder) {

/**
* get Redis connection
*
* @return
*/
private Object getConnection() {
Object connection ;
if (type == RedisToolsConstant.SINGLE){
RedisConnection redisConnection = jedisConnectionFactory.getConnection();
Object connection;
if (type == RedisToolsConstant.SINGLE) {
RedisConnection redisConnection = redisConnectionFactory.getConnection();
connection = redisConnection.getNativeConnection();
}else {
RedisClusterConnection clusterConnection = jedisConnectionFactory.getClusterConnection();
connection = clusterConnection.getNativeConnection() ;
} else {
RedisClusterConnection clusterConnection = redisConnectionFactory.getClusterConnection();
connection = clusterConnection.getNativeConnection();
}
return connection;
}
Expand All @@ -82,7 +83,7 @@ private Object getConnection() {
* false lock fail
*/
public boolean tryLock(String key, String request) {
return tryLock(key,request,10*TIME);
return tryLock(key, request, 10 * TIME);
}

/**
Expand All @@ -94,15 +95,15 @@ public boolean tryLock(String key, String request) {
public void lock(String key, String request) throws InterruptedException {
//get connection
Object connection = getConnection();
String result ;
for (; ;) {
if (connection instanceof Jedis){
result = ((Jedis)connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);
if (LOCK_MSG.equals(result)){
String result;
for (; ; ) {
if (connection instanceof Jedis) {
result = ((Jedis) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);
if (LOCK_MSG.equals(result)) {
((Jedis) connection).close();
}
}else {
result = ((JedisCluster)connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);
} else {
result = ((JedisCluster) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);
}

if (LOCK_MSG.equals(result)) {
Expand All @@ -127,15 +128,15 @@ public boolean lock(String key, String request, int blockTime) throws Interrupte

//get connection
Object connection = getConnection();
String result ;
String result;
while (blockTime >= 0) {
if (connection instanceof Jedis){
result = ((Jedis) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME) ;
if (LOCK_MSG.equals(result)){
if (connection instanceof Jedis) {
result = ((Jedis) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);
if (LOCK_MSG.equals(result)) {
((Jedis) connection).close();
}
}else {
result = ((JedisCluster) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME) ;
} else {
result = ((JedisCluster) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);
}
if (LOCK_MSG.equals(result)) {
return true;
Expand All @@ -160,12 +161,12 @@ public boolean lock(String key, String request, int blockTime) throws Interrupte
public boolean tryLock(String key, String request, int expireTime) {
//get connection
Object connection = getConnection();
String result ;
String result;

if (connection instanceof Jedis){
if (connection instanceof Jedis) {
result = ((Jedis) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime);
((Jedis) connection).close();
}else {
} else {
result = ((JedisCluster) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime);
}

Expand Down Expand Up @@ -225,15 +226,15 @@ public static class Builder {
*/
private static final int DEFAULT_SLEEP_TIME = 100;

private JedisConnectionFactory jedisConnectionFactory = null ;
private RedisConnectionFactory redisConnectionFactory = null;

private int type ;
private int type;

private String lockPrefix = DEFAULT_LOCK_PREFIX;
private int sleepTime = DEFAULT_SLEEP_TIME;

public Builder(JedisConnectionFactory jedisConnectionFactory, int type) {
this.jedisConnectionFactory = jedisConnectionFactory;
public Builder(RedisConnectionFactory redisConnectionFactory, int type) {
this.redisConnectionFactory = redisConnectionFactory;
this.type = type;
}

Expand Down
Loading