yangys
2024-03-30 871c0fce344b24c8046ec01173eca79b9e60c1d7
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
package com.qianwen.core.redis.lock;
 
import java.util.concurrent.TimeUnit;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qianwen.core.tool.function.CheckedSupplier;
import com.qianwen.core.tool.utils.Exceptions;
/* loaded from: blade-starter-redis-9.3.0.0-SNAPSHOT.jar:org/springblade/core/redis/lock/RedisLockClientImpl.class */
public class RedisLockClientImpl implements RedisLockClient {
    private static final Logger log = LoggerFactory.getLogger(RedisLockClientImpl.class);
    private final RedissonClient redissonClient;
 
    public RedisLockClientImpl(final RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }
 
    @Override // org.springblade.core.redis.lock.RedisLockClient
    public boolean tryLock(String lockName, LockType lockType, long waitTime, long leaseTime, TimeUnit timeUnit) throws InterruptedException {
        RLock lock = getLock(lockName, lockType);
        return lock.tryLock(waitTime, leaseTime, timeUnit);
    }
 
    @Override // org.springblade.core.redis.lock.RedisLockClient
    public void unLock(String lockName, LockType lockType) {
        RLock lock = getLock(lockName, lockType);
        if (lock.isLocked() && lock.isHeldByCurrentThread()) {
            lock.unlock();
        }
    }
 
    private RLock getLock(String lockName, LockType lockType) {
        RLock lock;
        if (LockType.REENTRANT == lockType) {
            lock = this.redissonClient.getLock(lockName);
        } else {
            lock = this.redissonClient.getFairLock(lockName);
        }
        return lock;
    }
 
    @Override // org.springblade.core.redis.lock.RedisLockClient
    public <T> T lock(String lockName, LockType lockType, long waitTime, long leaseTime, TimeUnit timeUnit, CheckedSupplier<T> supplier) {
 
        try {
            boolean result = tryLock(lockName, lockType, waitTime, leaseTime, timeUnit);
            if (result)
                return (T) supplier.get();
        } catch (Throwable e) {
            throw Exceptions.unchecked(e);
        } finally {
            unLock(lockName, lockType);
        }
        return null;
    }
}