Authored by LUOXC

refactor

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