|
| 1 | +package code.distribution.lock; |
| 2 | + |
| 3 | +import lombok.Data; |
| 4 | + |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.UUID; |
| 8 | +import java.util.concurrent.atomic.AtomicInteger; |
| 9 | + |
| 10 | +/** |
| 11 | + * 〈Redis锁〉<p> |
| 12 | + * 〈功能详细描述〉 |
| 13 | + * |
| 14 | + * @author zixiao |
| 15 | + * @date 2019/8/26 |
| 16 | + */ |
| 17 | +public class RedisLock implements DistributedLock { |
| 18 | + |
| 19 | + private RedisClient redisClient = new RedisClient(); |
| 20 | + |
| 21 | + @Override |
| 22 | + public void lock(String key) { |
| 23 | + long expired = 3000; |
| 24 | + long threadId = Thread.currentThread().getId(); |
| 25 | + Long ttl = tryAcquire(key, threadId, expired); |
| 26 | + if(ttl == null){ |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + while (true){ |
| 31 | + ttl = tryAcquire(key, threadId, expired); |
| 32 | + if(ttl == null){ |
| 33 | + return; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private Long tryAcquire(String key, long threadId, long expired){ |
| 39 | + return redisClient.tryLockInner(key, String.valueOf(threadId), expired); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void unlock(String key) { |
| 44 | + tryRelease(key, Thread.currentThread().getId()); |
| 45 | + } |
| 46 | + |
| 47 | + private void tryRelease(String key, long threadId){ |
| 48 | + redisClient.tryUnlockInner(key, String.valueOf(threadId)); |
| 49 | + } |
| 50 | + |
| 51 | + class RedisClient{ |
| 52 | + |
| 53 | + private Map<String/*lockKey*/, Map<String/*lockOwnerId*/, AtomicInteger>> lockHolder = new HashMap<>(); |
| 54 | + |
| 55 | + private Map<String, ExpiredObject> expiredMap = new HashMap<>(); |
| 56 | + |
| 57 | + public Long tryLockInner(String lockKey, String threadId, long expiredMills){ |
| 58 | + String threadUk = UUID.randomUUID().toString() + threadId; |
| 59 | + //lua保证张一个事务里面 |
| 60 | + synchronized (lockHolder){ |
| 61 | + if(!exist(lockKey)){ |
| 62 | + hset(lockKey, threadUk, 1); |
| 63 | + pexpire(lockKey, expiredMills); |
| 64 | + System.out.println("Lock success, I'm the first guy."); |
| 65 | + return null; |
| 66 | + } |
| 67 | + else if(hexist(lockKey, threadUk)){ |
| 68 | + int locks = hincrby(lockKey, threadUk, 1); |
| 69 | + pexpire(lockKey, expiredMills); |
| 70 | + System.out.println("Locked by me(reentrant), locks="+locks); |
| 71 | + return null; |
| 72 | + } |
| 73 | + System.out.println("Locked by another guy."); |
| 74 | + return pttl(lockKey); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public void tryUnlockInner(String lockKey, String threadId){ |
| 79 | + String threadUk = UUID.randomUUID().toString() + threadId; |
| 80 | + if(!exist(lockKey)){ |
| 81 | + System.out.println("no lock existed."); |
| 82 | + return; |
| 83 | + } |
| 84 | + else if(hexist(lockKey, threadUk)){ |
| 85 | + int locks = hdecrby(lockKey, threadUk, 1); |
| 86 | + System.out.println("Unlock by me(reentrant), remain locks="+locks); |
| 87 | + return; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public boolean exist(String key){ |
| 92 | + return lockHolder.containsKey(key) ; |
| 93 | + } |
| 94 | + |
| 95 | + public void hset(String key, String subKey, int value){ |
| 96 | + Map<String, AtomicInteger> map = new HashMap<>(); |
| 97 | + map.put(subKey, new AtomicInteger(Integer.valueOf(value))); |
| 98 | + lockHolder.put(key, map); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * ms为单位 |
| 103 | + */ |
| 104 | + public void pexpire(String key, long expireMills){ |
| 105 | + //expire |
| 106 | + expiredMap.put(key, new ExpiredObject(expireMills)); |
| 107 | + } |
| 108 | + |
| 109 | + public boolean hexist(String key, String subKey){ |
| 110 | + Map<String, AtomicInteger> map = lockHolder.get(key); |
| 111 | + if( map != null && map.containsKey(subKey)){ |
| 112 | + return true; |
| 113 | + } |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + public int hincrby(String key, String subKey, int delta){ |
| 118 | + Map<String, AtomicInteger> map = lockHolder.get(key); |
| 119 | + return map.get(subKey).addAndGet(delta); |
| 120 | + } |
| 121 | + |
| 122 | + public int hdecrby(String key, String subKey, int delta){ |
| 123 | + Map<String, AtomicInteger> map = lockHolder.get(key); |
| 124 | + return map.get(subKey).addAndGet(-1 * delta); |
| 125 | + } |
| 126 | + |
| 127 | + public Long pttl(String key){ |
| 128 | + ExpiredObject expiredObject = expiredMap.get(key); |
| 129 | + long esapled = System.currentTimeMillis() - expiredObject.start; |
| 130 | + if(esapled >= expiredObject.getExpiredMills()){ |
| 131 | + return null; |
| 132 | + }else{ |
| 133 | + return expiredObject.getExpiredMills() - esapled; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + @Data |
| 140 | + class ExpiredObject{ |
| 141 | + |
| 142 | + private long start; |
| 143 | + |
| 144 | + private long expiredMills; |
| 145 | + |
| 146 | + public ExpiredObject(long expiredMills) { |
| 147 | + this.start = System.currentTimeMillis(); |
| 148 | + this.expiredMills = expiredMills; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments