0
点赞
收藏
分享

微信扫一扫

软件工程导论期末急救包(上)

勇敢的趙迦禾 2023-06-26 阅读 57
分布式

目录

入口org.redisson.api.RedissonClient

@Resource
private RedissonClient redissonClient;
...

RLock rLock = redissonClient.getLock(lockName);

进入org.redisson.Redisson.getLock

    @Override
    public RLock getLock(String name) {
        return new RedissonLock(commandExecutor, name);
    }

进入org.redisson.RedissonLock

直接进行构建方法里面的super(commandExecutor, name);

    public RedissonBaseLock(CommandAsyncExecutor commandExecutor, String name) {
        super(commandExecutor, name);
        this.id = getServiceManager().getId();
        this.internalLockLeaseTime = getServiceManager().getCfg().getLockWatchdogTimeout();
        this.entryName = id + ":" + name;
    }

rLock.tryLock(60, TimeUnit.SECONDS);

尝试获取锁

进入org.redisson.RedissonLock

    <T> RFuture<T> tryLockInnerAsync(long waitTime, long leaseTime, TimeUnit unit, long threadId, RedisStrictCommand<T> command) {
        return commandExecutor.syncedEval(getRawName(), LongCodec.INSTANCE, command,
                "if ((redis.call('exists', KEYS[1]) == 0) " +
                            "or (redis.call('hexists', KEYS[1], ARGV[2]) == 1)) then " +
                        "redis.call('hincrby', KEYS[1], ARGV[2], 1); " +
                        "redis.call('pexpire', KEYS[1], ARGV[1]); " +
                        "return nil; " +
                    "end; " +
                    "return redis.call('pttl', KEYS[1]);",
                Collections.singletonList(getRawName()), unit.toMillis(leaseTime), getLockName(threadId));
    }
        Long ttl = tryAcquire(waitTime, leaseTime, unit, threadId);
        // lock acquired
        if (ttl == null) {
            return true;
        }

进入Lua脚本的第2个参数getLockName

    protected String getLockName(long threadId) {
        return id + ":" + threadId;
    }
举报

相关推荐

0 条评论