Authored by LUOXC

refactor

... ... @@ -20,8 +20,6 @@ public class RedisLock {
private static final Long RELEASE_SUCCESS = 1L;
private static final String ACQUIRE_SUCCESS = "OK";
private static final String SET_IF_NOT_EXIST = "NX";
private static final String SET_WITH_EXPIRE_TIME = "PX";
@Redis("gwNoSyncRedis")
private YHRedisTemplate redis;
... ... @@ -31,11 +29,11 @@ public class RedisLock {
public boolean acquire(RedisKeyBuilder keyBuilder, String value, final long timeout, final TimeUnit unit) {
try {
String script = "return redis.call('set', KEYS[1],ARGV[1],ARGV[2],ARGV[3],ARGV[4])";
RedisScript<String> redisScript = new DefaultRedisScript(script, String.class);
String script = "return redis.call('SET', KEYS[1], ARGV[1], 'NX', 'PX', ARGV[2]) ";
RedisScript<String> redisScript = new DefaultRedisScript<>(script, String.class);
String key = keyBuilder.getKey();
String result = redis.getStringRedisTemplate().execute(redisScript, Lists.newArrayList(key),
value, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, unit.toMillis(timeout));
value, String.valueOf(unit.toMillis(timeout)));
return ACQUIRE_SUCCESS.equals(result);
} catch (Exception e) {
return false;
... ...