1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| public class RedisLock {
private final Logger log = LoggerFactory.getLogger(RedisLock.class); private static final Long RELEASE_SUCCESS = 1L; private String lockKey; private RedisConnectionFactory connectionFactory; public RedisLock(RedisConnectionFactory connectionFactory, String lockKey) { this.connectionFactory = connectionFactory; this.lockKey = lockKey; }
public synchronized long lock(long milliTimeout) { log.debug("开始执行加锁 ==> {}",lockKey); try (Jedis jedis = getJedis()) {
long lockValue = new Random(System.currentTimeMillis()).nextLong(); String result = jedis.set(lockKey, String.valueOf(lockValue), "NX", "PX", milliTimeout); if("OK".equals(result)) { return lockValue; } } catch (Exception e) { throw new RuntimeException(e); } return -1; }
public synchronized boolean unlock(Long lockValue) { boolean unlockSuccess = false; log.debug("执行解锁 ==> {}",lockKey); try (Jedis jedis = getJedis()) { 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, 1, lockKey.toString(), lockValue.toString()); if (RELEASE_SUCCESS.equals(result)) { log.debug("解锁成功 ==> {}",lockKey); unlockSuccess = true; } } catch (Exception e) { log.error(e.getMessage(),e); throw new RuntimeException(e); } return unlockSuccess; }
private Jedis getJedis() { Field jedisField = ReflectionUtils.findField(JedisConnection.class, "jedis"); ReflectionUtils.makeAccessible(jedisField); Jedis jedis = (Jedis) ReflectionUtils.getField(jedisField, connectionFactory.getConnection()); return jedis; } }
|