Skip to content

Commit f94a2fe

Browse files
committed
Optimizations
1 parent f7b1fde commit f94a2fe

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

pom.xml

+7-6
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@
2121
<name>dlock</name>
2222

2323
<dependencies>
24-
<dependency>
25-
<groupId>redis.clients</groupId>
26-
<artifactId>jedis</artifactId>
27-
<version>${jedis.version}</version>
28-
</dependency>
29-
3024
<dependency>
3125
<groupId>org.slf4j</groupId>
3226
<artifactId>slf4j-api</artifactId>
3327
<version>${slf4j.version}</version>
3428
</dependency>
3529

30+
<dependency>
31+
<groupId>redis.clients</groupId>
32+
<artifactId>jedis</artifactId>
33+
<version>${jedis.version}</version>
34+
<optional>true</optional>
35+
</dependency>
36+
3637
<dependency>
3738
<groupId>org.projectlombok</groupId>
3839
<artifactId>lombok</artifactId>

src/main/java/com/github/verils/dlock/redis/RedisReentrantLock.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class RedisReentrantLock implements DistributedLock {
1717

1818
private final String key;
1919
private final int expireInSeconds;
20-
private final int waitSeconds;
20+
private final int sleepMilliseconds;
2121

2222
private String value;
2323
private int state;
@@ -33,11 +33,11 @@ public RedisReentrantLock(RedisClient redis, String key, int expireInSeconds) {
3333
this(redis, key, expireInSeconds, 30);
3434
}
3535

36-
public RedisReentrantLock(RedisClient redis, String key, int expireInSeconds, int waitSeconds) {
36+
public RedisReentrantLock(RedisClient redis, String key, int expireInSeconds, int sleepMilliseconds) {
3737
this.redis = redis;
3838
this.key = key;
3939
this.expireInSeconds = expireInSeconds;
40-
this.waitSeconds = waitSeconds;
40+
this.sleepMilliseconds = sleepMilliseconds;
4141
}
4242

4343
@Override
@@ -122,7 +122,7 @@ private void acquire() throws InterruptedException {
122122
}
123123
String lock = newLock();
124124
while (!redis.tryAcquire(key, lock, expireInSeconds)) {
125-
Thread.sleep(waitSeconds);
125+
Thread.sleep(sleepMilliseconds);
126126
}
127127
value = lock;
128128
state += 1;

src/main/java/com/github/verils/dlock/redis/client/JedisClient.java

+9
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,17 @@ public class JedisClient implements RedisClient {
1111
private static final String STATUS_OK = "OK";
1212

1313
private static final String NX = "NX";
14+
15+
/**
16+
* 单位:秒
17+
*/
1418
private static final String EX = "EX";
1519

20+
/**
21+
* 单位:毫秒
22+
*/
23+
private static final String PX = "PX";
24+
1625
private final JedisPool jedisPool;
1726

1827
public JedisClient(JedisPool jedisPool) {

0 commit comments

Comments
 (0)