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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| package com.example.lock;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.script.DefaultRedisScript; import org.springframework.stereotype.Component; import java.util.Collections; import java.util.UUID; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport;
@Component public class RedisReentrantLock implements DistributedLock {
private static final String LOCK_PREFIX = "lock:";
private static final String LOCK_SCRIPT = """ if (redis.call('exists', KEYS[1]) == 0) then redis.call('hset', KEYS[1], ARGV[1], 1) redis.call('pexpire', KEYS[1], ARGV[2]) return 1 end if (redis.call('hexists', KEYS[1], ARGV[1]) == 1) then redis.call('hincrby', KEYS[1], ARGV[1], 1) redis.call('pexpire', KEYS[1], ARGV[2]) return 1 end return 0 """;
private static final String UNLOCK_SCRIPT = """ if (redis.call('hexists', KEYS[1], ARGV[1]) == 0) then return 0 end local count = redis.call('hincrby', KEYS[1], ARGV[1], -1) if (count > 0) then return 1 else redis.call('hdel', KEYS[1], ARGV[1]) if (redis.call('hlen', KEYS[1]) == 0) then redis.call('del', KEYS[1]) end return 1 end """;
private final StringRedisTemplate redisTemplate; private final RedisLockWatchDog watchDog; private final String uuid = UUID.randomUUID().toString();
public RedisReentrantLock(StringRedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; watchDog = new RedisLockWatchDog(redisTemplate); }
private String ownerId() { return uuid + ":" + Thread.currentThread().getId(); }
public boolean tryLock(String key, long waitTime, long leaseTime, TimeUnit unit) { long deadline = System.currentTimeMillis() + unit.toMillis(waitTime); String lockKey = LOCK_PREFIX + key;
while (System.currentTimeMillis() < deadline) { Boolean success = redisTemplate.execute( new DefaultRedisScript<>(LOCK_SCRIPT, Boolean.class), Collections.singletonList(lockKey), ownerId(), String.valueOf(unit.toMillis(leaseTime)) ); if (success) { watchDog.startRenew( lockKey, ownerId(), unit.toMillis(leaseTime) ); return true; } else { LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(50)); } } return false; }
@Override public boolean lock(String key, long leaseTime, TimeUnit unit) { String lockKey = LOCK_PREFIX + key; boolean success = redisTemplate.execute( new DefaultRedisScript<>(LOCK_SCRIPT, Boolean.class), Collections.singletonList(lockKey), ownerId(), String.valueOf(unit.toMillis(leaseTime)) ); if (success) { watchDog.startRenew( lockKey, ownerId(), unit.toMillis(leaseTime) ); return true; } else { return false; } }
@Override public void unlock(String key) { String lockKey = LOCK_PREFIX + key; watchDog.stopRenew(lockKey); redisTemplate.execute( new DefaultRedisScript<>(UNLOCK_SCRIPT, Long.class), Collections.singletonList(lockKey), ownerId() ); } }
|