[Feature] add for new
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>lock</artifactId>
|
||||
<groupId>com.github.houbb</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lock-redis</artifactId>
|
||||
<description>The lock depends on redis.</description>
|
||||
|
||||
<dependencies>
|
||||
<!--============================== SELF ==============================-->
|
||||
<dependency>
|
||||
<groupId>com.github.houbb</groupId>
|
||||
<artifactId>lock-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--============================== OTHER ==============================-->
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.github.houbb.lock.redis.bs;
|
||||
|
||||
/**
|
||||
* 引导类
|
||||
* @author binbin.hou
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class LockRedisBs {
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.github.houbb.lock.redis.constant;
|
||||
|
||||
/**
|
||||
* redis 锁常量
|
||||
*
|
||||
* @author binbin.hou
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public final class LockRedisConst {
|
||||
|
||||
private LockRedisConst() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 加锁成功
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static final String LOCK_SUCCESS = "OK";
|
||||
|
||||
/**
|
||||
* 如果不存在则设置值
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static final String SET_IF_NOT_EXIST = "NX";
|
||||
|
||||
/**
|
||||
* 设置过期时间
|
||||
*
|
||||
* 单位:milliseconds
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static final String SET_WITH_EXPIRE_TIME = "PX";
|
||||
|
||||
/**
|
||||
* 解锁成功
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static final Long RELEASE_SUCCESS = 1L;
|
||||
|
||||
|
||||
/**
|
||||
* 默认的失效时间
|
||||
*
|
||||
* 暂时定为 30min
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static final long DEFAULT_EXPIRE_MILLS = 1000 * 60 * 30L;
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.github.houbb.lock.redis.core;
|
||||
|
||||
import com.github.houbb.lock.api.core.ILock;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
|
||||
/**
|
||||
* 抽象实现
|
||||
* @author binbin.hou
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public class AbstractLockRedis implements ILock {
|
||||
|
||||
@Override
|
||||
public void lock() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lockInterruptibly() throws InterruptedException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryLock() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unlock() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Condition newCondition() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.github.houbb.lock.redis.core;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
|
||||
/**
|
||||
* @author binbin.hou
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class LockRedis extends AbstractLockRedis {
|
||||
|
||||
@Override
|
||||
public void lock() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lockInterruptibly() throws InterruptedException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryLock() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
|
||||
final long timeMills = unit.toMillis(time);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unlock() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Condition newCondition() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package com.github.houbb.lock.redis;
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.github.houbb.lock.redis.support.id;
|
||||
|
||||
/**
|
||||
* @author binbin.hou
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface IId {
|
||||
|
||||
/**
|
||||
* 返回唯一标识
|
||||
* @return 唯一标识
|
||||
*/
|
||||
String id();
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.github.houbb.lock.redis.support.id.impl;
|
||||
|
||||
import com.github.houbb.lock.redis.support.id.IId;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author binbin.hou
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class IdUUID implements IId {
|
||||
|
||||
/**
|
||||
* 返回唯一标识
|
||||
*
|
||||
* @return 唯一标识
|
||||
*/
|
||||
@Override
|
||||
public String id() {
|
||||
return UUID.randomUUID().toString().replace("-","");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package com.github.houbb.lock.redis.support;
|
||||
@@ -1,29 +0,0 @@
|
||||
package com.github.houbb.lock.redis.support.redis;
|
||||
|
||||
/**
|
||||
* Redis 客户端
|
||||
* @author binbin.hou
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface IRedisOperator {
|
||||
|
||||
/**
|
||||
* 尝试获取分布式锁
|
||||
*
|
||||
* @param lockKey 锁
|
||||
* @param requestId 请求标识
|
||||
* @param expireTimeMills 超期时间
|
||||
* @return 是否获取成功
|
||||
*/
|
||||
boolean tryLock(String lockKey, String requestId, int expireTimeMills);
|
||||
|
||||
/**
|
||||
* 解锁
|
||||
* @param lockKey 锁 key
|
||||
* @param requestId 请求标识
|
||||
* @return 结果
|
||||
* @since 0.0.1
|
||||
*/
|
||||
boolean unlock(String lockKey, String requestId);
|
||||
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.github.houbb.lock.redis.support.redis.impl;
|
||||
|
||||
import com.github.houbb.lock.redis.constant.LockRedisConst;
|
||||
import com.github.houbb.lock.redis.support.redis.IRedisOperator;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* Redis 客户端
|
||||
* @author binbin.hou
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public class JedisOperator implements IRedisOperator {
|
||||
|
||||
/**
|
||||
* jedis 客户端
|
||||
* @since 0.0.1
|
||||
*/
|
||||
private final Jedis jedis;
|
||||
|
||||
public JedisOperator(Jedis jedis) {
|
||||
this.jedis = jedis;
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试获取分布式锁
|
||||
*
|
||||
* expireTimeMills 保证当前进程挂掉,也能释放锁
|
||||
*
|
||||
* requestId 保证解锁的是当前进程(锁的持有者)
|
||||
*
|
||||
* @param lockKey 锁
|
||||
* @param requestId 请求标识
|
||||
* @param expireTimeMills 超期时间
|
||||
* @return 是否获取成功
|
||||
* @since 0.0.1
|
||||
*/
|
||||
@Override
|
||||
public boolean tryLock(String lockKey, String requestId, int expireTimeMills) {
|
||||
String result = jedis.set(lockKey, requestId, LockRedisConst.SET_IF_NOT_EXIST, LockRedisConst.SET_WITH_EXPIRE_TIME, expireTimeMills);
|
||||
return LockRedisConst.LOCK_SUCCESS.equals(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解锁
|
||||
*
|
||||
* (1)使用 requestId,保证为当前锁的持有者
|
||||
* (2)使用 lua 脚本,保证执行的原子性。
|
||||
*
|
||||
* @param lockKey 锁 key
|
||||
* @param requestId 请求标识
|
||||
* @return 结果
|
||||
* @since 0.0.1
|
||||
*/
|
||||
@Override
|
||||
public boolean unlock(String lockKey, String requestId) {
|
||||
String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
|
||||
Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId));
|
||||
return LockRedisConst.RELEASE_SUCCESS.equals(result);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user